import { useState, useEffect, useRef } from 'react' import { cn } from '@/lib/utils' import { ArrowUp, ArrowDown } from '@phosphor-icons/react' import { type SortOption, type SortDirection, getDefaultDirection, SORT_OPTIONS, getSortOptionLabel } from '../utils/noteListHelpers' export function SortDropdown({ groupLabel, current, direction, customProperties, onChange }: { groupLabel: string current: SortOption direction: SortDirection customProperties?: string[] onChange: (groupLabel: string, option: SortOption, direction: SortDirection) => void }) { const [open, setOpen] = useState(false) const ref = useRef(null) useEffect(() => { if (!open) return function handleClick(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false) } document.addEventListener('mousedown', handleClick) return () => document.removeEventListener('mousedown', handleClick) }, [open]) const handleSelect = (opt: SortOption, dir: SortDirection) => { onChange(groupLabel, opt, dir) setOpen(false) } const DirectionIcon = direction === 'asc' ? ArrowUp : ArrowDown const hasCustom = customProperties && customProperties.length > 0 return (
{open && (
{SORT_OPTIONS.map((opt) => ( ))} {hasCustom && ( <>
{customProperties.map((key) => { const value: SortOption = `property:${key}` return })} )}
)}
) } function SortRow({ value, label, current, direction, onSelect }: { value: SortOption label: string current: SortOption direction: SortDirection onSelect: (opt: SortOption, dir: SortDirection) => void }) { const isActive = value === current return (
{ e.stopPropagation(); onSelect(value, isActive ? direction : getDefaultDirection(value)) }} > {label}
) }