Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dashboard project filtering (#6259)
Adds the same project selector Autocomplete as we use in the playground. Implements the filtering Closes: # [1-2036](https://linear.app/unleash/issue/1-2036/api-project-filtering) <img width="1508" alt="Screenshot 2024-02-16 at 15 57 24" src="https://github.com/Unleash/unleash/assets/104830839/4490e43c-17db-41b6-ba75-e7b0f2df0522"> --------- Signed-off-by: andreas-unleash <andreas@getunleash.ai>
- Loading branch information
1 parent
9b980bb
commit f71badd
Showing
2 changed files
with
124 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
frontend/src/component/executiveDashboard/ProjectSelect/ProjectSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { ComponentProps, Dispatch, SetStateAction, VFC } from 'react'; | ||
import { Autocomplete, Box, styled, TextField } from '@mui/material'; | ||
import { renderOption } from '../../playground/Playground/PlaygroundForm/renderOption'; | ||
import useProjects from '../../../hooks/api/getters/useProjects/useProjects'; | ||
|
||
const StyledBox = styled(Box)(({ theme }) => ({ | ||
width: '25%', | ||
marginLeft: '75%', | ||
marginBottom: theme.spacing(4), | ||
marginTop: theme.spacing(4), | ||
[theme.breakpoints.down('lg')]: { | ||
width: '100%', | ||
marginLeft: 0, | ||
}, | ||
})); | ||
|
||
interface IOption { | ||
label: string; | ||
id: string; | ||
} | ||
|
||
export const allOption = { label: 'ALL', id: '*' }; | ||
|
||
interface IProjectSelectProps { | ||
selectedProjects: string[]; | ||
onChange: Dispatch<SetStateAction<string[]>>; | ||
} | ||
|
||
export const ProjectSelect: VFC<IProjectSelectProps> = ({ | ||
selectedProjects, | ||
onChange, | ||
}) => { | ||
const { projects: availableProjects } = useProjects(); | ||
|
||
const projectsOptions = [ | ||
allOption, | ||
...availableProjects.map(({ name: label, id }) => ({ | ||
label, | ||
id, | ||
})), | ||
]; | ||
|
||
const isAllProjects = | ||
selectedProjects && | ||
(selectedProjects.length === 0 || | ||
(selectedProjects.length === 1 && selectedProjects[0] === '*')); | ||
|
||
const onProjectsChange: ComponentProps<typeof Autocomplete>['onChange'] = ( | ||
event, | ||
value, | ||
reason, | ||
) => { | ||
const newProjects = value as IOption | IOption[]; | ||
if (reason === 'clear' || newProjects === null) { | ||
return onChange([allOption.id]); | ||
} | ||
if (Array.isArray(newProjects)) { | ||
if (newProjects.length === 0) { | ||
return onChange([allOption.id]); | ||
} | ||
if ( | ||
newProjects.find(({ id }) => id === allOption.id) !== undefined | ||
) { | ||
return onChange([allOption.id]); | ||
} | ||
return onChange(newProjects.map(({ id }) => id)); | ||
} | ||
if (newProjects.id === allOption.id) { | ||
return onChange([allOption.id]); | ||
} | ||
|
||
return onChange([newProjects.id]); | ||
}; | ||
|
||
return ( | ||
<StyledBox> | ||
<Autocomplete | ||
disablePortal | ||
id='projects' | ||
limitTags={3} | ||
multiple={!isAllProjects} | ||
options={projectsOptions} | ||
sx={{ flex: 1 }} | ||
renderInput={(params) => ( | ||
<TextField {...params} label='Projects' /> | ||
)} | ||
renderOption={renderOption} | ||
getOptionLabel={({ label }) => label} | ||
disableCloseOnSelect | ||
size='small' | ||
value={ | ||
isAllProjects | ||
? allOption | ||
: projectsOptions.filter(({ id }) => | ||
selectedProjects.includes(id), | ||
) | ||
} | ||
onChange={onProjectsChange} | ||
data-testid={'DASHBOARD_PROJECT_SELECT'} | ||
/> | ||
</StyledBox> | ||
); | ||
}; |