Skip to content

Commit c085e9c

Browse files
fix(users-table): prevent rapid sorting clicks and improve sort handling logic
1 parent be4a201 commit c085e9c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

dashboard/src/components/users/users-table.tsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const UsersTable = memo(() => {
2424
const [isEditModalOpen, setEditModalOpen] = useState(false)
2525
const [selectedUser, setSelectedUser] = useState<UserResponse | null>(null)
2626
const [isAdvanceSearchOpen, setIsAdvanceSearchOpen] = useState(false)
27+
const [isSorting, setIsSorting] = useState(false)
2728
const { admin } = useAdmin()
2829
const isSudo = admin?.is_sudo || false
2930

@@ -126,19 +127,33 @@ const UsersTable = memo(() => {
126127

127128
const handleSort = useCallback(
128129
(column: string) => {
130+
// Prevent rapid clicking
131+
if (isSorting) return
132+
133+
setIsSorting(true)
134+
129135
let newSort: string
130136

131-
if (filters.sort === column) {
132-
newSort = '-' + column
133-
} else if (filters.sort === '-' + column) {
134-
newSort = '-created_at'
137+
// Clean the column name in case it comes with prefix
138+
const cleanColumn = column.startsWith('-') ? column.slice(1) : column
139+
140+
if (filters.sort === cleanColumn) {
141+
// If currently ascending, make it descending
142+
newSort = '-' + cleanColumn
143+
} else if (filters.sort === '-' + cleanColumn) {
144+
// If currently descending, make it ascending (toggle back)
145+
newSort = cleanColumn
135146
} else {
136-
newSort = column
147+
// If different column or default, make it ascending
148+
newSort = cleanColumn
137149
}
138150

139151
setFilters(prev => ({ ...prev, sort: newSort }))
152+
153+
// Release the lock after a short delay
154+
setTimeout(() => setIsSorting(false), 100)
140155
},
141-
[filters.sort],
156+
[filters.sort, isSorting],
142157
)
143158

144159
const handleStatusFilter = useCallback((value: any) => {

0 commit comments

Comments
 (0)