1
0
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:
Thomas Heartman 2024-04-15 13:17:53 +02:00 committed by GitHub
parent 9aee1a7c42
commit 3d60c2acd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 3 deletions

View File

@ -31,6 +31,8 @@ import { ReactComponent as ProPlanIconLight } from 'assets/icons/pro-enterprise-
import { safeRegExp } from '@server/util/escape-regex'; import { safeRegExp } from '@server/util/escape-regex';
import { ThemeMode } from 'component/common/ThemeMode/ThemeMode'; import { ThemeMode } from 'component/common/ThemeMode/ThemeMode';
import { useUiFlag } from 'hooks/useUiFlag'; 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 }) => ({ const StyledDivContainer = styled('div')(({ theme }) => ({
display: 'flex', display: 'flex',
@ -139,6 +141,7 @@ export const ProjectListNew = () => {
const showProjectFilterButtons = useUiFlag('projectListFilterMyProjects'); const showProjectFilterButtons = useUiFlag('projectListFilterMyProjects');
const filters = ['All projects', 'My projects']; const filters = ['All projects', 'My projects'];
const [filter, setFilter] = useState(filters[0]); const [filter, setFilter] = useState(filters[0]);
const myProjects = new Set(useProfile().profile?.projects || []);
useEffect(() => { useEffect(() => {
const tableState: PageQueryType = {}; const tableState: PageQueryType = {};
@ -152,11 +155,18 @@ export const ProjectListNew = () => {
}, [searchValue, setSearchParams]); }, [searchValue, setSearchParams]);
const filteredProjects = useMemo(() => { const filteredProjects = useMemo(() => {
const preFilteredProjects =
showProjectFilterButtons && filter === 'My projects'
? projects.filter(shouldDisplayInMyProjects(myProjects))
: projects;
const regExp = safeRegExp(searchValue, 'i'); const regExp = safeRegExp(searchValue, 'i');
return ( return (
searchValue searchValue
? projects.filter((project) => regExp.test(project.name)) ? preFilteredProjects.filter((project) =>
: projects regExp.test(project.name),
)
: preFilteredProjects
).sort((a, b) => { ).sort((a, b) => {
if (a?.favorite && !b?.favorite) { if (a?.favorite && !b?.favorite) {
return -1; return -1;
@ -166,7 +176,7 @@ export const ProjectListNew = () => {
} }
return 0; return 0;
}); });
}, [projects, searchValue]); }, [projects, searchValue, filter, myProjects, showProjectFilterButtons]);
const handleHover = (projectId: string) => { const handleHover = (projectId: string) => {
if (fetchedProjects[projectId]) { if (fetchedProjects[projectId]) {

View File

@ -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' },
]);
});

View File

@ -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);