1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-13 13:48:59 +02:00

refactor: project archive sort

This commit is contained in:
Tymoteusz Czech 2024-08-30 12:13:51 +02:00
parent 2a35976081
commit 037e525165
No known key found for this signature in database
GPG Key ID: 133555230D88D75F
5 changed files with 64 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import { type FC, useEffect, useMemo, useState } from 'react'; import { type FC, useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom'; import { useSearchParams } from 'react-router-dom';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { PageContent } from 'component/common/PageContent/PageContent'; import { PageContent } from 'component/common/PageContent/PageContent';
@ -16,7 +16,7 @@ import useProjects from 'hooks/api/getters/useProjects/useProjects';
import { ReviveProjectDialog } from './ReviveProjectDialog/ReviveProjectDialog'; import { ReviveProjectDialog } from './ReviveProjectDialog/ReviveProjectDialog';
import { DeleteProjectDialogue } from '../Project/DeleteProject/DeleteProjectDialogue'; import { DeleteProjectDialogue } from '../Project/DeleteProject/DeleteProjectDialogue';
import { SearchHighlightProvider } from 'component/common/Table/SearchHighlightContext/SearchHighlightContext'; import { SearchHighlightProvider } from 'component/common/Table/SearchHighlightContext/SearchHighlightContext';
import { safeRegExp } from '@server/util/escape-regex'; import { useProjectsSearchAndSort } from './hooks/useProjectsSearchAndSort';
const StyledApiError = styled(ApiError)(({ theme }) => ({ const StyledApiError = styled(ApiError)(({ theme }) => ({
maxWidth: '500px', maxWidth: '500px',
@ -86,14 +86,10 @@ export const ArchiveProjectList: FC = () => {
/> />
); );
const filteredProjects = useMemo( const filteredProjects = useProjectsSearchAndSort(
() => projects,
searchValue searchValue,
? projects.filter((project) => 'archived',
safeRegExp(searchValue, 'i').test(project.name),
)
: projects,
[projects, searchValue],
); );
return ( return (

View File

@ -13,7 +13,13 @@ const StyledContainer = styled('div')(() => ({
width: '100%', width: '100%',
})); }));
export const sortKeys = ['name', 'created', 'updated', 'seen'] as const; export const sortKeys = [
'name',
'created',
'updated',
'seen',
'archived',
] as const;
const options: Array<{ const options: Array<{
key: (typeof sortKeys)[number]; key: (typeof sortKeys)[number];

View File

@ -250,4 +250,46 @@ describe('useProjectsSearchAndSort', () => {
'Project A', 'Project A',
]); ]);
}); });
it('should not put favorites first if sorting by `archived`', () => {
const { result } = renderHook(() =>
useProjectsSearchAndSort(
[
{
name: 'A',
id: '1',
createdAt: '2024-01-01',
favorite: false,
archivedAt: '2024-02-03',
},
{
name: 'B',
id: '2',
createdAt: '2024-01-02',
archivedAt: '2024-02-01',
},
{
name: 'C',
id: '3',
createdAt: '2024-01-04',
archivedAt: '2024-02-02',
favorite: true,
},
],
undefined,
'archived',
),
);
expect(
result.current.map(
(project) =>
`${project.name} - ${project.archivedAt}${project.favorite ? ' - favorite' : ''}`,
),
).toEqual([
'A - 2024-02-03',
'C - 2024-02-02 - favorite',
'B - 2024-02-01',
]);
});
}); });

View File

@ -39,9 +39,17 @@ export const useProjectsSearchAndSort = (
return bVal?.getTime() - aVal?.getTime(); return bVal?.getTime() - aVal?.getTime();
} }
if (sortBy === 'archived') {
const aVal = new Date(a.archivedAt || 0);
const bVal = new Date(b.archivedAt || 0);
return bVal?.getTime() - aVal?.getTime();
}
return 0; return 0;
}) })
.sort((a, b) => { .sort((a, b) => {
if (sortBy === 'archived') return 0;
if (a?.favorite && !b?.favorite) { if (a?.favorite && !b?.favorite) {
return -1; return -1;
} }

View File

@ -7,6 +7,7 @@ export interface IProjectCard {
name: string; name: string;
id: string; id: string;
createdAt: string | Date; createdAt: string | Date;
archivedAt?: string | Date;
health?: number; health?: number;
description?: string; description?: string;
featureCount?: number; featureCount?: number;