|
| 1 | +import{css}from"@emotion/css"; |
| 2 | +importtype{Interpolation,Theme}from"@emotion/react"; |
| 3 | +importKeyboardArrowRightfrom"@mui/icons-material/KeyboardArrowRight"; |
| 4 | +importPersonAddfrom"@mui/icons-material/PersonAdd"; |
| 5 | +import{LoadingButton}from"@mui/lab"; |
| 6 | +import{Table,TableBody,TableContainer,TextField}from"@mui/material"; |
| 7 | +importAutocomplete,{createFilterOptions}from"@mui/material/Autocomplete"; |
| 8 | +importAvatarGroupfrom"@mui/material/AvatarGroup"; |
| 9 | +importSkeletonfrom"@mui/material/Skeleton"; |
| 10 | +importTableCellfrom"@mui/material/TableCell"; |
| 11 | +importTableRowfrom"@mui/material/TableRow"; |
| 12 | +import{useState,typeFC}from"react"; |
| 13 | +import{LinkasRouterLink,useNavigate}from"react-router-dom"; |
| 14 | +import{RBACResourceActions}from"api/rbacresources_gen"; |
| 15 | +importtype{Group,Role}from"api/typesGenerated"; |
| 16 | +import{AvatarData}from"components/AvatarData/AvatarData"; |
| 17 | +import{AvatarDataSkeleton}from"components/AvatarData/AvatarDataSkeleton"; |
| 18 | +import{ChooseOne,Cond}from"components/Conditionals/ChooseOne"; |
| 19 | +import{EmptyState}from"components/EmptyState/EmptyState"; |
| 20 | +import{GroupAvatar}from"components/GroupAvatar/GroupAvatar"; |
| 21 | +import{Paywall}from"components/Paywall/Paywall"; |
| 22 | +import{Stack}from"components/Stack/Stack"; |
| 23 | +import{ |
| 24 | +TableLoader, |
| 25 | +TableLoaderSkeleton, |
| 26 | +TableRowSkeleton, |
| 27 | +}from"components/TableLoader/TableLoader"; |
| 28 | +import{UserAvatar}from"components/UserAvatar/UserAvatar"; |
| 29 | +import{permissionsToCheck}from"contexts/auth/permissions"; |
| 30 | +import{useClickableTableRow}from"hooks"; |
| 31 | +import{docs}from"utils/docs"; |
| 32 | + |
| 33 | +exporttypeCustomRolesPageViewProps={ |
| 34 | +roles:Role[]|undefined; |
| 35 | +canCreateGroup:boolean; |
| 36 | +isTemplateRBACEnabled:boolean; |
| 37 | +}; |
| 38 | + |
| 39 | +constfilter=createFilterOptions<Role>(); |
| 40 | + |
| 41 | +exportconstCustomRolesPageView:FC<CustomRolesPageViewProps>=({ |
| 42 | + roles, |
| 43 | + canCreateGroup, |
| 44 | + isTemplateRBACEnabled, |
| 45 | +})=>{ |
| 46 | +constisLoading=Boolean(roles===undefined); |
| 47 | +constisEmpty=Boolean(roles&&roles.length===0); |
| 48 | +const[selectedRole,setSelectedRole]=useState<Role|null>(null); |
| 49 | +console.log({ selectedRole}); |
| 50 | + |
| 51 | +return( |
| 52 | +<> |
| 53 | +<ChooseOne> |
| 54 | +<Condcondition={!isTemplateRBACEnabled}> |
| 55 | +<Paywall |
| 56 | +message="Custom Roles" |
| 57 | +description="Organize users into groups with restricted access to templates. You need an Enterprise license to use this feature." |
| 58 | +documentationLink={docs("/admin/groups")} |
| 59 | +/> |
| 60 | +</Cond> |
| 61 | +<Cond> |
| 62 | +<Stack |
| 63 | +direction="row" |
| 64 | +alignItems="center" |
| 65 | +spacing={1} |
| 66 | +css={styles.rolesDropdown} |
| 67 | +> |
| 68 | +<Autocomplete |
| 69 | +value={selectedRole} |
| 70 | +onChange={(_,newValue)=>{ |
| 71 | +console.log("onChange: ",newValue); |
| 72 | +if(typeofnewValue==="string"){ |
| 73 | +console.log("0"); |
| 74 | +setSelectedRole({ |
| 75 | +name:newValue, |
| 76 | +display_name:newValue, |
| 77 | +site_permissions:[], |
| 78 | +organization_permissions:[], |
| 79 | +user_permissions:[], |
| 80 | +}); |
| 81 | +}elseif(newValue&&newValue.display_name){ |
| 82 | +console.log("1"); |
| 83 | +// Create a new value from the user input |
| 84 | +// setSelectedRole({ ...newValue, display_name: newValue.name }); |
| 85 | +setSelectedRole(newValue); |
| 86 | +}else{ |
| 87 | +console.log("2"); |
| 88 | +setSelectedRole(newValue); |
| 89 | +} |
| 90 | +}} |
| 91 | +isOptionEqualToValue={(option:Role,value:Role)=> |
| 92 | +option.name===value.name |
| 93 | +} |
| 94 | +filterOptions={(options,params)=>{ |
| 95 | +constfiltered=filter(options,params); |
| 96 | + |
| 97 | +const{ inputValue}=params; |
| 98 | +// Suggest the creation of a new value |
| 99 | +constisExisting=options.some( |
| 100 | +(option)=>inputValue===option.display_name, |
| 101 | +); |
| 102 | +if(inputValue!==""&&!isExisting){ |
| 103 | +filtered.push({ |
| 104 | +name:inputValue, |
| 105 | +display_name:`Add${inputValue}`, |
| 106 | +site_permissions:[], |
| 107 | +organization_permissions:[], |
| 108 | +user_permissions:[], |
| 109 | +}); |
| 110 | +} |
| 111 | + |
| 112 | +returnfiltered; |
| 113 | +}} |
| 114 | +selectOnFocus |
| 115 | +clearOnBlur |
| 116 | +handleHomeEndKeys |
| 117 | +id="custom-role" |
| 118 | +options={roles||[]} |
| 119 | +getOptionLabel={(option)=>{ |
| 120 | +// console.log("getOptionLabel: ", option); |
| 121 | +// Value selected with enter, right from the input |
| 122 | +if(typeofoption==="string"){ |
| 123 | +returnoption; |
| 124 | +} |
| 125 | +// Add "xxx" option created dynamically |
| 126 | +if(option.name){ |
| 127 | +returnoption.name; |
| 128 | +} |
| 129 | +// Regular option |
| 130 | +returnoption.display_name; |
| 131 | +}} |
| 132 | +renderOption={(props,option)=>{ |
| 133 | +const{ key, ...optionProps}=props; |
| 134 | +return( |
| 135 | +<likey={key}{...optionProps}> |
| 136 | +{option.display_name} |
| 137 | +</li> |
| 138 | +); |
| 139 | +}} |
| 140 | +sx={{width:300}} |
| 141 | +renderInput={(params)=>( |
| 142 | +<TextField |
| 143 | +{...params} |
| 144 | +label="Display Name" |
| 145 | +InputLabelProps={{ |
| 146 | +shrink:true, |
| 147 | +}} |
| 148 | +/> |
| 149 | +)} |
| 150 | +/> |
| 151 | + |
| 152 | +<LoadingButton |
| 153 | +loadingPosition="start" |
| 154 | +// disabled={!selectedUser} |
| 155 | +type="submit" |
| 156 | +startIcon={<PersonAdd/>} |
| 157 | +loading={isLoading} |
| 158 | +> |
| 159 | + Save Custom Role |
| 160 | +</LoadingButton> |
| 161 | +</Stack> |
| 162 | + |
| 163 | +<TableContainer> |
| 164 | +<Table> |
| 165 | +<TableBody> |
| 166 | +<ChooseOne> |
| 167 | +<Condcondition={isLoading}> |
| 168 | +<TableLoader/> |
| 169 | +</Cond> |
| 170 | + |
| 171 | +<Condcondition={isEmpty}> |
| 172 | +<TableRow> |
| 173 | +<TableCellcolSpan={999}> |
| 174 | +<EmptyState |
| 175 | +message="No custom roles yet" |
| 176 | +description={ |
| 177 | +canCreateGroup |
| 178 | + ?"Create your first custom role" |
| 179 | + :"You don't have permission to create a custom role" |
| 180 | +} |
| 181 | +/> |
| 182 | +</TableCell> |
| 183 | +</TableRow> |
| 184 | +</Cond> |
| 185 | + |
| 186 | +<Cond> |
| 187 | +{Object.entries(RBACResourceActions).map(([key,value])=>{ |
| 188 | +return( |
| 189 | +<TableRowkey={key}> |
| 190 | +<TableCell> |
| 191 | +<likey={key}css={styles.checkBoxes}> |
| 192 | +<inputtype="checkbox"/>{key} |
| 193 | +<ulcss={styles.checkBoxes}> |
| 194 | +{Object.entries(value).map(([key,value])=>{ |
| 195 | +return( |
| 196 | +<likey={key}> |
| 197 | +<spancss={styles.actionText}> |
| 198 | +<inputtype="checkbox"/>{key} |
| 199 | +</span>{" "} |
| 200 | + -{" "} |
| 201 | +<spancss={styles.actionDescription}> |
| 202 | +{value} |
| 203 | +</span> |
| 204 | +</li> |
| 205 | +); |
| 206 | +})} |
| 207 | +</ul> |
| 208 | +</li> |
| 209 | +</TableCell> |
| 210 | +</TableRow> |
| 211 | +); |
| 212 | +})} |
| 213 | +</Cond> |
| 214 | +</ChooseOne> |
| 215 | +</TableBody> |
| 216 | +</Table> |
| 217 | +</TableContainer> |
| 218 | +</Cond> |
| 219 | +</ChooseOne> |
| 220 | +</> |
| 221 | +); |
| 222 | +}; |
| 223 | + |
| 224 | +conststyles={ |
| 225 | +rolesDropdown:{ |
| 226 | +marginBottom:20, |
| 227 | +}, |
| 228 | +checkBoxes:{ |
| 229 | +margin:0, |
| 230 | +listStyleType:"none", |
| 231 | +}, |
| 232 | +actionText:(theme)=>({ |
| 233 | +color:theme.palette.text.primary, |
| 234 | +}), |
| 235 | +actionDescription:(theme)=>({ |
| 236 | +color:theme.palette.text.secondary, |
| 237 | +}), |
| 238 | +}satisfiesRecord<string,Interpolation<Theme>>; |
| 239 | + |
| 240 | +exportdefaultCustomRolesPageView; |