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,6 +1,6 @@
interface SortIconProps { interface SortIconProps {
column: string column: string
sort: string sort: string | null
order: string order: string
} }

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 [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) => { const handleSort = useCallback((column: string) => {
userClicked.current = true
setSort(prev => { setSort(prev => {
if (prev === column) { if (prev === column) {
setOrder(o => (o === 'asc' ? 'desc' : 'asc')) setOrder(o => (o === 'asc' ? 'desc' : 'asc'))
return column return prev
} }
setOrder('desc') setOrder('desc')
return column return column
}) })
}, []) }, [])
return { sort, order, handleSort, activeSort: sort } const activeSort = userClicked.current ? sort : null
return { sort, order, handleSort, activeSort }
} }