1
1
import { Pagination , States , StatesIcon , StatesTitle , StatesActions , StatesAction } from '@rocket.chat/fuselage' ;
2
2
import { useMediaQuery , useDebouncedValue } from '@rocket.chat/fuselage-hooks' ;
3
- import type { OptionProp } from '@rocket.chat/ui-client' ;
4
3
import { useEndpoint , useTranslation } from '@rocket.chat/ui-contexts' ;
5
4
import { useQuery } from '@tanstack/react-query' ;
6
5
import type { ReactElement , MutableRefObject } from 'react' ;
7
- import React , { useRef , useState , useEffect , useMemo } from 'react' ;
6
+ import React , { useRef , useEffect , useMemo , useContext } from 'react' ;
8
7
9
8
import GenericNoResults from '../../../components/GenericNoResults' ;
10
9
import {
@@ -16,41 +15,43 @@ import {
16
15
} from '../../../components/GenericTable' ;
17
16
import { usePagination } from '../../../components/GenericTable/hooks/usePagination' ;
18
17
import { useSort } from '../../../components/GenericTable/hooks/useSort' ;
18
+ import type { SearchFilters } from '../../../contexts/SearchFilterContext' ;
19
+ import { SearchFilterContext } from '../../../contexts/SearchFilterContext' ;
19
20
import RoomRow from './RoomRow' ;
20
21
import RoomsTableFilters from './RoomsTableFilters' ;
21
22
22
- type RoomFilters = {
23
- searchText : string ;
24
- types : OptionProp [ ] ;
25
- } ;
26
-
27
23
const DEFAULT_TYPES = [ 'd' , 'p' , 'c' , 'l' , 'discussions' , 'teams' ] ;
28
24
29
25
const RoomsTable = ( { reload } : { reload : MutableRefObject < ( ) => void > } ) : ReactElement => {
30
26
const t = useTranslation ( ) ;
31
27
const mediaQuery = useMediaQuery ( '(min-width: 1024px)' ) ;
32
28
33
- const [ roomFilters , setRoomFilters ] = useState < RoomFilters > ( { searchText : '' , types : [ ] } ) ;
29
+ const { searchFilters } = useContext ( SearchFilterContext ) ;
34
30
35
- const prevRoomFilterText = useRef < string > ( roomFilters . searchText ) ;
31
+ const prevRoomFilters = useRef < SearchFilters > ( searchFilters ) ;
36
32
37
33
const { sortBy, sortDirection, setSort } = useSort < 'name' | 't' | 'usersCount' | 'msgs' | 'default' | 'featured' > ( 'name' ) ;
38
34
const { current, itemsPerPage, setItemsPerPage, setCurrent, ...paginationProps } = usePagination ( ) ;
39
- const searchText = useDebouncedValue ( roomFilters . searchText , 500 ) ;
35
+ const searchText = useDebouncedValue ( searchFilters . searchText , 500 ) ;
40
36
41
37
const query = useDebouncedValue (
42
38
useMemo ( ( ) => {
43
- if ( searchText !== prevRoomFilterText . current ) {
39
+ const filtersChanged =
40
+ searchText !== prevRoomFilters . current . searchText ||
41
+ JSON . stringify ( searchFilters . types ) !== JSON . stringify ( prevRoomFilters . current . types ) ;
42
+
43
+ if ( filtersChanged ) {
44
44
setCurrent ( 0 ) ;
45
45
}
46
+
46
47
return {
47
48
filter : searchText || '' ,
48
49
sort : `{ "${ sortBy } ": ${ sortDirection === 'asc' ? 1 : - 1 } }` ,
49
50
count : itemsPerPage ,
50
- offset : searchText === prevRoomFilterText . current ? current : 0 ,
51
- types : roomFilters . types . length ? [ ...roomFilters . types . map ( ( roomType ) => roomType . id ) ] : DEFAULT_TYPES ,
51
+ offset : filtersChanged ? 0 : current ,
52
+ types : searchFilters . types . length ? [ ...searchFilters . types . map ( ( roomType ) => roomType . id ) ] : DEFAULT_TYPES ,
52
53
} ;
53
- } , [ searchText , sortBy , sortDirection , itemsPerPage , current , roomFilters . types , setCurrent ] ) ,
54
+ } , [ searchText , sortBy , sortDirection , itemsPerPage , current , searchFilters . types , setCurrent ] ) ,
54
55
500 ,
55
56
) ;
56
57
@@ -63,8 +64,8 @@ const RoomsTable = ({ reload }: { reload: MutableRefObject<() => void> }): React
63
64
} , [ reload , refetch ] ) ;
64
65
65
66
useEffect ( ( ) => {
66
- prevRoomFilterText . current = searchText ;
67
- } , [ searchText ] ) ;
67
+ prevRoomFilters . current = searchFilters ;
68
+ } , [ searchFilters ] ) ;
68
69
69
70
const headers = (
70
71
< >
@@ -116,7 +117,7 @@ const RoomsTable = ({ reload }: { reload: MutableRefObject<() => void> }): React
116
117
117
118
return (
118
119
< >
119
- < RoomsTableFilters setFilters = { setRoomFilters } />
120
+ < RoomsTableFilters />
120
121
{ isLoading && (
121
122
< GenericTable >
122
123
< GenericTableHeader > { headers } </ GenericTableHeader >
0 commit comments