|
1 | | -import { useEffect } from 'react'; |
| 1 | +import { useMemo, useState } from 'react'; |
2 | 2 | import { useSearchParams } from 'react-router-dom'; |
3 | 3 |
|
4 | | -import { SelectCSDProps } from 'components'; |
| 4 | +import type { PropertyFilterProps } from 'components'; |
5 | 5 |
|
6 | | -import { useLocalStorageState } from 'hooks/useLocalStorageState'; |
7 | 6 | import { useProjectFilter } from 'hooks/useProjectFilter'; |
8 | 7 |
|
9 | 8 | type Args = { |
10 | 9 | localStorePrefix: string; |
11 | | - projectSearchKey?: string; |
12 | | - selectedProject?: string; |
13 | 10 | }; |
14 | 11 |
|
15 | | -export const useFilters = ({ localStorePrefix, projectSearchKey }: Args) => { |
16 | | - const [searchParams] = useSearchParams(); |
17 | | - const { selectedProject, setSelectedProject, projectOptions } = useProjectFilter({ localStorePrefix }); |
18 | | - const [onlyActive, setOnlyActive] = useLocalStorageState<boolean>(`${localStorePrefix}-is-active`, false); |
| 12 | +type RequestParamsKeys = keyof Pick<TRunsRequestParams, 'only_active' | 'project_name'>; |
19 | 13 |
|
20 | | - const setSelectedOptionFromParams = ( |
21 | | - searchKey: string, |
22 | | - options: SelectCSDProps.Options | null, |
23 | | - set: (option: SelectCSDProps.Option) => void, |
24 | | - ) => { |
25 | | - const searchValue = searchParams.get(searchKey); |
| 14 | +const FilterKeys: Record<string, RequestParamsKeys> = { |
| 15 | + PROJECT_NAME: 'project_name', |
| 16 | + ACTIVE: 'only_active', |
| 17 | +}; |
| 18 | + |
| 19 | +const EMPTY_QUERY: PropertyFilterProps.Query = { |
| 20 | + tokens: [], |
| 21 | + operation: 'and', |
| 22 | +}; |
26 | 23 |
|
27 | | - if (!searchValue || !options?.length) return; |
| 24 | +const tokensToRequestParams = (tokens: PropertyFilterProps.Query['tokens']) => { |
| 25 | + return tokens.reduce((acc, token) => { |
| 26 | + if (token.propertyKey) { |
| 27 | + acc[token.propertyKey as RequestParamsKeys] = token.value; |
| 28 | + } |
| 29 | + |
| 30 | + return acc; |
| 31 | + }, {} as Record<RequestParamsKeys, string>); |
| 32 | +}; |
| 33 | + |
| 34 | +export const useFilters = ({ localStorePrefix }: Args) => { |
| 35 | + const [searchParams, setSearchParams] = useSearchParams(); |
| 36 | + const { projectOptions } = useProjectFilter({ localStorePrefix }); |
| 37 | + |
| 38 | + const [propertyFilterQuery, setPropertyFilterQuery] = useState<PropertyFilterProps.Query>(() => { |
| 39 | + const tokens = []; |
28 | 40 |
|
29 | 41 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
30 | 42 | // @ts-ignore |
31 | | - const selectedOption = options.find((option) => option?.value === searchValue); |
| 43 | + for (const [paramKey, paramValue] of searchParams.entries()) { |
| 44 | + if (Object.values(FilterKeys).includes(paramKey)) { |
| 45 | + tokens.push({ propertyKey: paramKey, operator: '=', value: paramValue }); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (!tokens.length) { |
| 50 | + return EMPTY_QUERY; |
| 51 | + } |
| 52 | + |
| 53 | + return { |
| 54 | + ...EMPTY_QUERY, |
| 55 | + tokens, |
| 56 | + }; |
| 57 | + }); |
32 | 58 |
|
33 | | - if (selectedOption) set(selectedOption); |
| 59 | + const clearFilter = () => { |
| 60 | + setSearchParams({}); |
| 61 | + setPropertyFilterQuery(EMPTY_QUERY); |
34 | 62 | }; |
35 | 63 |
|
36 | | - useEffect(() => { |
37 | | - if (!projectSearchKey) return; |
| 64 | + const filteringOptions = useMemo(() => { |
| 65 | + const options: PropertyFilterProps.FilteringOption[] = []; |
38 | 66 |
|
39 | | - setSelectedOptionFromParams(projectSearchKey, projectOptions, setSelectedProject); |
40 | | - }, [searchParams, projectSearchKey, projectOptions]); |
| 67 | + projectOptions.forEach(({ value }) => { |
| 68 | + if (value) |
| 69 | + options.push({ |
| 70 | + propertyKey: FilterKeys.PROJECT_NAME, |
| 71 | + value, |
| 72 | + }); |
| 73 | + }); |
41 | 74 |
|
42 | | - const clearSelected = () => { |
43 | | - setSelectedProject(null); |
44 | | - setOnlyActive(false); |
45 | | - }; |
| 75 | + options.push({ |
| 76 | + propertyKey: FilterKeys.ACTIVE, |
| 77 | + value: 'True', |
| 78 | + }); |
| 79 | + |
| 80 | + return options; |
| 81 | + }, [projectOptions]); |
| 82 | + |
| 83 | + const filteringProperties = [ |
| 84 | + { |
| 85 | + key: FilterKeys.PROJECT_NAME, |
| 86 | + operators: ['='], |
| 87 | + propertyLabel: 'Project', |
| 88 | + groupValuesLabel: 'Project values', |
| 89 | + }, |
| 90 | + { |
| 91 | + key: FilterKeys.ACTIVE, |
| 92 | + operators: ['='], |
| 93 | + propertyLabel: 'Only active', |
| 94 | + groupValuesLabel: 'Active values', |
| 95 | + }, |
| 96 | + ]; |
| 97 | + |
| 98 | + const onChangePropertyFilter: PropertyFilterProps['onChange'] = ({ detail }) => { |
| 99 | + const { tokens, operation } = detail; |
46 | 100 |
|
47 | | - const setSelectedProjectHandle = (project: SelectCSDProps.Option | null) => { |
48 | | - setSelectedProject(project); |
49 | | - setOnlyActive(false); |
| 101 | + const filteredTokens = tokens.filter((token, tokenIndex) => { |
| 102 | + return !tokens.some((item, index) => token.propertyKey === item.propertyKey && index > tokenIndex); |
| 103 | + }); |
| 104 | + |
| 105 | + setSearchParams(tokensToRequestParams(filteredTokens)); |
| 106 | + |
| 107 | + setPropertyFilterQuery({ |
| 108 | + operation, |
| 109 | + tokens: filteredTokens, |
| 110 | + }); |
50 | 111 | }; |
51 | 112 |
|
| 113 | + const filteringRequestParams = useMemo(() => { |
| 114 | + const params = tokensToRequestParams(propertyFilterQuery.tokens); |
| 115 | + |
| 116 | + return { |
| 117 | + ...params, |
| 118 | + only_active: params.only_active === 'True', |
| 119 | + }; |
| 120 | + }, [propertyFilterQuery]); |
| 121 | + |
52 | 122 | return { |
53 | | - projectOptions, |
54 | | - selectedProject, |
55 | | - setSelectedProject: setSelectedProjectHandle, |
56 | | - onlyActive, |
57 | | - setOnlyActive, |
58 | | - clearSelected, |
| 123 | + filteringRequestParams, |
| 124 | + clearFilter, |
| 125 | + propertyFilterQuery, |
| 126 | + onChangePropertyFilter, |
| 127 | + filteringOptions, |
| 128 | + filteringProperties, |
59 | 129 | } as const; |
60 | 130 | }; |
0 commit comments