import type { SortingState } from '@tanstack/react-table' import { ArrowDownIcon, ArrowUpDownIcon, ArrowUpIcon, ChevronDownIcon, ChevronUpIcon, XIcon, } from 'lucide-react' import { cn } from '../lib/cn' import { Button } from '../ui/button' import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover' export type SortHierarchyPickerProps = { /** The shared multi-column sort, in priority order. */ sorting: SortingState /** Readable label for a column id, resolved across all tabs. */ resolveLabel: (columnId: string) => string /** Replaces the whole sort with `next` (priority = array order). */ onChange: (next: SortingState) => void className?: string } /** * Toolbar popover that exposes the shared multi-column sort hierarchy: flip a * column's direction, remove it, or reorder its priority. Uses the popover * primitive (not the dropdown menu) so it stays open across edits. */ export function SortHierarchyPicker({ sorting, resolveLabel, onChange, className, }: SortHierarchyPickerProps) { const count = sorting.length const flip = (index: number) => onChange(sorting.map((s, i) => (i === index ? { ...s, desc: !s.desc } : s))) const remove = (index: number) => onChange(sorting.filter((_, i) => i !== index)) const move = (index: number, delta: -1 | 1) => { const target = index + delta if (target < 0 || target >= sorting.length) return const next = sorting.slice() const [item] = next.splice(index, 1) next.splice(target, 0, item!) onChange(next) } return (
Sort priority
{count === 0 ? (
No active sort. Click a column header to sort.
) : (
    {sorting.map((entry, index) => { const label = resolveLabel(entry.id) const dir = entry.desc ? 'descending' : 'ascending' return (
  1. {index + 1} {label}
  2. ) })}
)}

Tip: Shift-click a column header to add it to the sort.

) }