Skip to content

Commit 119e134

Browse files
x0sinaImMohammad20000
authored andcommitted
fix(admins-table): table behavior desktop
1 parent b0a5ae1 commit 119e134

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

dashboard/src/components/admins/filters.tsx

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Pagination, PaginationContent, PaginationEllipsis, PaginationItem, Pagi
44
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
55
import useDirDetection from '@/hooks/use-dir-detection'
66
import { cn } from '@/lib/utils'
7-
import { useDebouncedSearch } from '@/hooks/use-debounced-search'
7+
import { debounce } from 'es-toolkit'
88
import { RefreshCw, SearchIcon, X } from 'lucide-react'
9-
import { useEffect } from 'react'
9+
import { useCallback, useEffect, useRef, useState } from 'react'
1010
import { useTranslation } from 'react-i18next'
1111
import { useGetAdmins } from '@/service/api'
1212
import { LoaderCircle } from 'lucide-react'
@@ -29,25 +29,41 @@ export function Filters<T extends BaseFilters>({ filters, onFilterChange }: Filt
2929
const { t } = useTranslation()
3030
const dir = useDirDetection()
3131
const { refetch } = useGetAdmins(filters)
32-
const { search, debouncedSearch, setSearch } = useDebouncedSearch(filters.username || '', 300)
32+
const [search, setSearch] = useState(filters.username || '')
33+
const onFilterChangeRef = useRef(onFilterChange)
34+
35+
// Keep the ref in sync with the prop
36+
onFilterChangeRef.current = onFilterChange
37+
38+
// Debounced search function
39+
const setSearchField = useCallback(
40+
debounce((value: string) => {
41+
onFilterChangeRef.current({
42+
username: value || undefined,
43+
offset: 0, // Reset to first page when search is updated
44+
} as Partial<T>)
45+
}, 300),
46+
[],
47+
)
3348

34-
// Update filters when debounced search changes
49+
// Cleanup on unmount
3550
useEffect(() => {
36-
onFilterChange({
37-
username: debouncedSearch || undefined,
38-
offset: 0,
39-
} as Partial<T>)
40-
}, [debouncedSearch, onFilterChange])
51+
return () => {
52+
setSearchField.cancel()
53+
}
54+
}, [setSearchField])
4155

4256
// Handle input change
4357
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
4458
setSearch(e.target.value)
59+
setSearchField(e.target.value)
4560
}
4661

4762
// Clear search field
4863
const clearSearch = () => {
4964
setSearch('')
50-
onFilterChange({
65+
setSearchField.cancel()
66+
onFilterChangeRef.current({
5167
username: undefined,
5268
offset: 0,
5369
} as Partial<T>)

0 commit comments

Comments
 (0)