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