mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
feat: allow you to filter for "my projects" (#6855)
This change adds filtering functionality to the project list filter buttons. In this case, "my projects" is defined as any project that is marked as a favorite OR (inclusive or) that you are a part of, as defined by your user profile.
This commit is contained in:
parent
9aee1a7c42
commit
3d60c2acd0
@ -31,6 +31,8 @@ import { ReactComponent as ProPlanIconLight } from 'assets/icons/pro-enterprise-
|
||||
import { safeRegExp } from '@server/util/escape-regex';
|
||||
import { ThemeMode } from 'component/common/ThemeMode/ThemeMode';
|
||||
import { useUiFlag } from 'hooks/useUiFlag';
|
||||
import { useProfile } from 'hooks/api/getters/useProfile/useProfile';
|
||||
import { shouldDisplayInMyProjects } from './should-display-in-my-projects';
|
||||
|
||||
const StyledDivContainer = styled('div')(({ theme }) => ({
|
||||
display: 'flex',
|
||||
@ -139,6 +141,7 @@ export const ProjectListNew = () => {
|
||||
const showProjectFilterButtons = useUiFlag('projectListFilterMyProjects');
|
||||
const filters = ['All projects', 'My projects'];
|
||||
const [filter, setFilter] = useState(filters[0]);
|
||||
const myProjects = new Set(useProfile().profile?.projects || []);
|
||||
|
||||
useEffect(() => {
|
||||
const tableState: PageQueryType = {};
|
||||
@ -152,11 +155,18 @@ export const ProjectListNew = () => {
|
||||
}, [searchValue, setSearchParams]);
|
||||
|
||||
const filteredProjects = useMemo(() => {
|
||||
const preFilteredProjects =
|
||||
showProjectFilterButtons && filter === 'My projects'
|
||||
? projects.filter(shouldDisplayInMyProjects(myProjects))
|
||||
: projects;
|
||||
|
||||
const regExp = safeRegExp(searchValue, 'i');
|
||||
return (
|
||||
searchValue
|
||||
? projects.filter((project) => regExp.test(project.name))
|
||||
: projects
|
||||
? preFilteredProjects.filter((project) =>
|
||||
regExp.test(project.name),
|
||||
)
|
||||
: preFilteredProjects
|
||||
).sort((a, b) => {
|
||||
if (a?.favorite && !b?.favorite) {
|
||||
return -1;
|
||||
@ -166,7 +176,7 @@ export const ProjectListNew = () => {
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}, [projects, searchValue]);
|
||||
}, [projects, searchValue, filter, myProjects, showProjectFilterButtons]);
|
||||
|
||||
const handleHover = (projectId: string) => {
|
||||
if (fetchedProjects[projectId]) {
|
||||
|
@ -0,0 +1,34 @@
|
||||
import type { IProjectCard } from 'interfaces/project';
|
||||
import { shouldDisplayInMyProjects } from './should-display-in-my-projects';
|
||||
|
||||
test('should check that the project is a user project OR that it is a favorite', () => {
|
||||
const myProjects = new Set(['my1', 'my2', 'my3']);
|
||||
|
||||
const projects: IProjectCard[] = [
|
||||
{ id: 'my1', favorite: true },
|
||||
{ id: 'my2', favorite: false },
|
||||
{ id: 'my3' },
|
||||
{ id: 'fave-but-not-mine', favorite: true },
|
||||
{ id: 'not-mine-not-fave', favorite: false },
|
||||
{ id: 'not-mine-undefined-fave' },
|
||||
].map(({ id, favorite }) => ({
|
||||
name: 'name',
|
||||
id,
|
||||
createdAt: '2024-04-15T11:09:52+02:00',
|
||||
health: 100,
|
||||
description: 'description',
|
||||
featureCount: 100,
|
||||
mode: 'open',
|
||||
memberCount: 10,
|
||||
favorite,
|
||||
}));
|
||||
|
||||
const filtered = projects.filter(shouldDisplayInMyProjects(myProjects));
|
||||
|
||||
expect(filtered).toMatchObject([
|
||||
{ id: 'my1' },
|
||||
{ id: 'my2' },
|
||||
{ id: 'my3' },
|
||||
{ id: 'fave-but-not-mine' },
|
||||
]);
|
||||
});
|
@ -0,0 +1,6 @@
|
||||
import type { IProjectCard } from 'interfaces/project';
|
||||
|
||||
export const shouldDisplayInMyProjects =
|
||||
(myProjectIds: Set<string>) =>
|
||||
(project: IProjectCard): boolean =>
|
||||
project.favorite || myProjectIds.has(project.id);
|
Loading…
Reference in New Issue
Block a user