11import { useMemo , useState } from 'react' ;
22import { useSearchParams } from 'react-router-dom' ;
3+ import { ToggleProps } from '@cloudscape-design/components' ;
34
45import type { PropertyFilterProps } from 'components' ;
56
67import { useProjectFilter } from 'hooks/useProjectFilter' ;
78
9+ import { useGetUserListQuery } from '../../../../services/user' ;
10+
811type Args = {
912 localStorePrefix : string ;
1013} ;
@@ -14,27 +17,34 @@ type RequestParamsKeys = keyof Pick<TRunsRequestParams, 'only_active' | 'project
1417const FilterKeys : Record < string , RequestParamsKeys > = {
1518 PROJECT_NAME : 'project_name' ,
1619 USER_NAME : 'username' ,
17- ACTIVE : 'only_active' ,
1820} ;
1921
2022const EMPTY_QUERY : PropertyFilterProps . Query = {
2123 tokens : [ ] ,
2224 operation : 'and' ,
2325} ;
2426
25- const tokensToRequestParams = ( tokens : PropertyFilterProps . Query [ 'tokens' ] ) => {
26- return tokens . reduce ( ( acc , token ) => {
27+ const tokensToRequestParams = ( tokens : PropertyFilterProps . Query [ 'tokens' ] , onlyActive ?: boolean ) => {
28+ const params = tokens . reduce ( ( acc , token ) => {
2729 if ( token . propertyKey ) {
2830 acc [ token . propertyKey as RequestParamsKeys ] = token . value ;
2931 }
3032
3133 return acc ;
3234 } , { } as Record < RequestParamsKeys , string > ) ;
35+
36+ if ( onlyActive ) {
37+ params [ 'only_active' ] = 'true' ;
38+ }
39+
40+ return params ;
3341} ;
3442
3543export const useFilters = ( { localStorePrefix } : Args ) => {
3644 const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
45+ const [ onlyActive , setOnlyActive ] = useState ( ( ) => searchParams . get ( 'only_active' ) === 'true' ) ;
3746 const { projectOptions } = useProjectFilter ( { localStorePrefix } ) ;
47+ const { data : usersData } = useGetUserListQuery ( ) ;
3848
3949 const [ propertyFilterQuery , setPropertyFilterQuery ] = useState < PropertyFilterProps . Query > ( ( ) => {
4050 const tokens = [ ] ;
@@ -59,6 +69,7 @@ export const useFilters = ({ localStorePrefix }: Args) => {
5969
6070 const clearFilter = ( ) => {
6171 setSearchParams ( { } ) ;
72+ setOnlyActive ( false ) ;
6273 setPropertyFilterQuery ( EMPTY_QUERY ) ;
6374 } ;
6475
@@ -73,13 +84,15 @@ export const useFilters = ({ localStorePrefix }: Args) => {
7384 } ) ;
7485 } ) ;
7586
76- options . push ( {
77- propertyKey : FilterKeys . ACTIVE ,
78- value : 'True' ,
87+ usersData ?. forEach ( ( { username } ) => {
88+ options . push ( {
89+ propertyKey : FilterKeys . USER_NAME ,
90+ value : username ,
91+ } ) ;
7992 } ) ;
8093
8194 return options ;
82- } , [ projectOptions ] ) ;
95+ } , [ projectOptions , usersData ] ) ;
8396
8497 const filteringProperties = [
8598 {
@@ -93,12 +106,6 @@ export const useFilters = ({ localStorePrefix }: Args) => {
93106 operators : [ '=' ] ,
94107 propertyLabel : 'User' ,
95108 } ,
96- {
97- key : FilterKeys . ACTIVE ,
98- operators : [ '=' ] ,
99- propertyLabel : 'Only active' ,
100- groupValuesLabel : 'Active values' ,
101- } ,
102109 ] ;
103110
104111 const onChangePropertyFilter : PropertyFilterProps [ 'onChange' ] = ( { detail } ) => {
@@ -108,20 +115,26 @@ export const useFilters = ({ localStorePrefix }: Args) => {
108115 return ! tokens . some ( ( item , index ) => token . propertyKey === item . propertyKey && index > tokenIndex ) ;
109116 } ) ;
110117
111- setSearchParams ( tokensToRequestParams ( filteredTokens ) ) ;
118+ setSearchParams ( tokensToRequestParams ( filteredTokens , onlyActive ) ) ;
112119
113120 setPropertyFilterQuery ( {
114121 operation,
115122 tokens : filteredTokens ,
116123 } ) ;
117124 } ;
118125
126+ const onChangeOnlyActive : ToggleProps [ 'onChange' ] = ( { detail } ) => {
127+ setOnlyActive ( detail . checked ) ;
128+
129+ setSearchParams ( tokensToRequestParams ( propertyFilterQuery . tokens , detail . checked ) ) ;
130+ } ;
131+
119132 const filteringRequestParams = useMemo ( ( ) => {
120133 const params = tokensToRequestParams ( propertyFilterQuery . tokens ) ;
121134
122135 return {
123136 ...params ,
124- only_active : params . only_active === 'True' ,
137+ only_active : onlyActive ,
125138 } ;
126139 } , [ propertyFilterQuery ] ) ;
127140
@@ -132,5 +145,7 @@ export const useFilters = ({ localStorePrefix }: Args) => {
132145 onChangePropertyFilter,
133146 filteringOptions,
134147 filteringProperties,
148+ onlyActive,
149+ onChangeOnlyActive,
135150 } as const ;
136151} ;
0 commit comments