From 30336537846696323400202d07e4e15da9b8152d Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:27:05 +0200 Subject: [PATCH] refactor: simplify project filters --- .../cypress/integration/projects/list.spec.ts | 5 +- .../project/ProjectList/ProjectList.tsx | 55 ++++++++++--------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/frontend/cypress/integration/projects/list.spec.ts b/frontend/cypress/integration/projects/list.spec.ts index a1b5186f69..05290c0860 100644 --- a/frontend/cypress/integration/projects/list.spec.ts +++ b/frontend/cypress/integration/projects/list.spec.ts @@ -1,8 +1,5 @@ /// -import { - SEARCH_INPUT, - //@ts-ignore -} from '../../../src/utils/testIds'; +import { SEARCH_INPUT } from '../../../src/utils/testIds'; describe('project overview', () => { const randomId = String(Math.random()).split('.')[1]; diff --git a/frontend/src/component/project/ProjectList/ProjectList.tsx b/frontend/src/component/project/ProjectList/ProjectList.tsx index 16201038f7..592497e7fa 100644 --- a/frontend/src/component/project/ProjectList/ProjectList.tsx +++ b/frontend/src/component/project/ProjectList/ProjectList.tsx @@ -1,6 +1,6 @@ import { useContext, useMemo, useState } from 'react'; import { Link, useNavigate } from 'react-router-dom'; -import { BooleanParam, StringParam, withDefault } from 'use-query-params'; +import { StringParam, withDefault } from 'use-query-params'; import { mutate } from 'swr'; import { getProjectFetcher } from 'hooks/api/getters/useProject/getProjectFetcher'; import useProjects from 'hooks/api/getters/useProjects/useProjects'; @@ -125,6 +125,19 @@ function resolveCreateButtonData( } } +const filterOptions = [ + { + label: 'All projects', + value: 'all', + }, + { + label: 'My projects', + value: 'my_projects', + }, +] as const; + +// type FilterOptions = (typeof filterOptions)[number]['value']; + export const ProjectListNew = () => { const { hasAccess } = useContext(AccessContext); const navigate = useNavigate(); @@ -135,30 +148,18 @@ export const ProjectListNew = () => { const showProjectFilterButtons = useUiFlag('projectListFilterMyProjects'); const myProjects = new Set(useProfile().profile?.projects || []); - const filterOptions = [ - { - label: 'All projects', - value: false, - }, - { - label: 'My projects', - value: true, - }, - ]; - - const stateConfig = { - myProjects: withDefault(BooleanParam, false), - search: withDefault(StringParam, ''), - }; const [filterState, setFilterState] = usePersistentTableState( `projects-list`, - stateConfig, + { + filter: withDefault(StringParam, 'all'), + search: withDefault(StringParam, ''), + }, ); const searchValue = filterState.search; const filteredProjects = useMemo(() => { const preFilteredProjects = - showProjectFilterButtons && filterState.myProjects + showProjectFilterButtons && filterState.filter === 'my_projects' ? projects.filter(shouldDisplayInMyProjects(myProjects)) : projects; @@ -181,7 +182,7 @@ export const ProjectListNew = () => { }, [ projects, searchValue, - filterState.myProjects, + filterState.filter, myProjects, showProjectFilterButtons, ]); @@ -226,23 +227,23 @@ export const ProjectListNew = () => { aria-label='project list filter' size='small' color='primary' - value={filterState.myProjects} + value={filterState.filter} exclusive - onChange={(event, value) => { - if (value !== null) { + onChange={(event, filter) => { + if (filter !== null) { setFilterState({ - myProjects: value, + filter, }); } }} > - {filterOptions.map((filter) => { + {filterOptions.map((option) => { return ( - {filter.label} + {option.label} ); })}