@@ -4,13 +4,10 @@ import { ToggleProps } from '@cloudscape-design/components';
44
55import type { PropertyFilterProps } from 'components' ;
66
7- import { useProjectFilter } from 'hooks/useProjectFilter ' ;
7+ import { useLocalStorageState } from 'hooks' ;
88import { EMPTY_QUERY , requestParamsToTokens , tokensToRequestParams , tokensToSearchParams } from 'libs/filters' ;
9- import { useGetUserListQuery } from 'services/user' ;
10-
11- type Args = {
12- localStorePrefix : string ;
13- } ;
9+ import { useLazyGetProjectsQuery } from 'services/project' ;
10+ import { useLazyGetUserListQuery } from 'services/user' ;
1411
1512type RequestParamsKeys = keyof Pick < TRunsRequestParams , 'only_active' | 'project_name' | 'username' > ;
1613
@@ -19,56 +16,40 @@ const filterKeys: Record<string, RequestParamsKeys> = {
1916 USER_NAME : 'username' ,
2017} ;
2118
22- export const useFilters = ( { localStorePrefix } : Args ) => {
19+ export const useFilters = ( ) => {
2320 const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
24- const [ onlyActive , setOnlyActive ] = useState ( ( ) => searchParams . get ( 'only_active' ) === 'true' ) ;
25- const { projectOptions } = useProjectFilter ( { localStorePrefix } ) ;
26- const { data : usersData } = useGetUserListQuery ( { } ) ;
21+ const [ onlyActive , setOnlyActive ] = useLocalStorageState ( 'run-list-filter-only-active' , true ) ;
22+ const [ filteringOptions , setFilteringOptions ] = useState < PropertyFilterProps . FilteringOption [ ] > ( [ ] ) ;
23+ const [ filteringStatusType , setFilteringStatusType ] = useState < PropertyFilterProps . StatusType | undefined > ( ) ;
24+ const [ getProjects ] = useLazyGetProjectsQuery ( ) ;
25+ const [ getUsers ] = useLazyGetUserListQuery ( ) ;
2726
2827 const [ propertyFilterQuery , setPropertyFilterQuery ] = useState < PropertyFilterProps . Query > ( ( ) =>
2928 requestParamsToTokens < RequestParamsKeys > ( { searchParams, filterKeys } ) ,
3029 ) ;
3130
3231 const clearFilter = ( ) => {
3332 setSearchParams ( { } ) ;
34- setOnlyActive ( false ) ;
3533 setPropertyFilterQuery ( EMPTY_QUERY ) ;
3634 } ;
3735
38- const filteringOptions = useMemo ( ( ) => {
39- const options : PropertyFilterProps . FilteringOption [ ] = [ ] ;
40-
41- projectOptions . forEach ( ( { value } ) => {
42- if ( value )
43- options . push ( {
44- propertyKey : filterKeys . PROJECT_NAME ,
45- value,
46- } ) ;
47- } ) ;
48-
49- usersData ?. data ?. forEach ( ( { username } ) => {
50- options . push ( {
51- propertyKey : filterKeys . USER_NAME ,
52- value : username ,
53- } ) ;
54- } ) ;
55-
56- return options ;
57- } , [ projectOptions , usersData ] ) ;
58-
59- const filteringProperties = [
60- {
61- key : filterKeys . PROJECT_NAME ,
62- operators : [ '=' ] ,
63- propertyLabel : 'Project' ,
64- groupValuesLabel : 'Project values' ,
65- } ,
66- {
67- key : filterKeys . USER_NAME ,
68- operators : [ '=' ] ,
69- propertyLabel : 'User' ,
70- } ,
71- ] ;
36+ const filteringProperties = useMemo < PropertyFilterProps . FilteringProperty [ ] > (
37+ ( ) => [
38+ {
39+ key : filterKeys . PROJECT_NAME ,
40+ operators : [ '=' ] ,
41+ propertyLabel : 'Project' ,
42+ groupValuesLabel : 'Project values' ,
43+ } ,
44+ {
45+ key : filterKeys . USER_NAME ,
46+ operators : [ '=' ] ,
47+ propertyLabel : 'User' ,
48+ groupValuesLabel : 'User values' ,
49+ } ,
50+ ] ,
51+ [ ] ,
52+ ) ;
7253
7354 const onChangePropertyFilter : PropertyFilterProps [ 'onChange' ] = ( { detail } ) => {
7455 const { tokens, operation } = detail ;
@@ -87,8 +68,6 @@ export const useFilters = ({ localStorePrefix }: Args) => {
8768
8869 const onChangeOnlyActive : ToggleProps [ 'onChange' ] = ( { detail } ) => {
8970 setOnlyActive ( detail . checked ) ;
90-
91- setSearchParams ( tokensToSearchParams < RequestParamsKeys > ( propertyFilterQuery . tokens , detail . checked ) ) ;
9271 } ;
9372
9473 const filteringRequestParams = useMemo ( ( ) => {
@@ -102,6 +81,46 @@ export const useFilters = ({ localStorePrefix }: Args) => {
10281 } as Partial < TRunsRequestParams > ;
10382 } , [ propertyFilterQuery , onlyActive ] ) ;
10483
84+ const handleLoadItems : PropertyFilterProps [ 'onLoadItems' ] = async ( {
85+ detail : { filteringProperty, filteringText, firstPage, ...other } ,
86+ } ) => {
87+ console . log ( { filteringProperty, filteringText, firstPage, other } ) ;
88+
89+ setFilteringOptions ( [ ] ) ;
90+
91+ if ( ! filteringText . length ) {
92+ return Promise . resolve ( ) ;
93+ }
94+
95+ setFilteringStatusType ( 'loading' ) ;
96+
97+ if ( filteringProperty ?. key === filterKeys . PROJECT_NAME ) {
98+ await getProjects ( { name_pattern : filteringText } )
99+ . unwrap ( )
100+ . then ( ( { data } ) =>
101+ data . map ( ( { project_name } ) => ( {
102+ propertyKey : filterKeys . PROJECT_NAME ,
103+ value : project_name ,
104+ } ) ) ,
105+ )
106+ . then ( setFilteringOptions ) ;
107+ }
108+
109+ if ( filteringProperty ?. key === filterKeys . USER_NAME ) {
110+ await getUsers ( { name_pattern : filteringText } )
111+ . unwrap ( )
112+ . then ( ( { data } ) =>
113+ data . map ( ( { username } ) => ( {
114+ propertyKey : filterKeys . USER_NAME ,
115+ value : username ,
116+ } ) ) ,
117+ )
118+ . then ( setFilteringOptions ) ;
119+ }
120+
121+ setFilteringStatusType ( undefined ) ;
122+ } ;
123+
105124 return {
106125 filteringRequestParams,
107126 clearFilter,
@@ -111,5 +130,7 @@ export const useFilters = ({ localStorePrefix }: Args) => {
111130 filteringProperties,
112131 onlyActive,
113132 onChangeOnlyActive,
133+ filteringStatusType,
134+ handleLoadItems,
114135 } as const ;
115136} ;
0 commit comments