fix: align useTableSort with PHP version — userClicked ref, nullable activeSort

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-03-23 10:53:26 +01:00
parent 1a175e805b
commit 56065c381b
2 changed files with 10 additions and 6 deletions

View File

@@ -1,19 +1,23 @@
import { useState, useCallback } from 'react'
import { useState, useCallback, useRef } from 'react'
export default function useTableSort(defaultSort = 'id') {
export default function useTableSort(defaultSort = 'id', defaultOrder: 'asc' | 'desc' = 'desc') {
const [sort, setSort] = useState(defaultSort)
const [order, setOrder] = useState<'asc' | 'desc'>('desc')
const [order, setOrder] = useState<'asc' | 'desc'>(defaultOrder)
const userClicked = useRef(false)
const handleSort = useCallback((column: string) => {
userClicked.current = true
setSort(prev => {
if (prev === column) {
setOrder(o => (o === 'asc' ? 'desc' : 'asc'))
return column
return prev
}
setOrder('desc')
return column
})
}, [])
return { sort, order, handleSort, activeSort: sort }
const activeSort = userClicked.current ? sort : null
return { sort, order, handleSort, activeSort }
}