import { useState, useEffect, useRef } from 'react' import { cn } from '@/lib/utils' import { ArrowUp, ArrowDown } from '@phosphor-icons/react' import { type SortOption, type SortDirection, DEFAULT_DIRECTIONS, SORT_OPTIONS } from '../utils/noteListHelpers' export function SortDropdown({ groupLabel, current, direction, onChange }: { groupLabel: string current: SortOption direction: SortDirection 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 return (
{open && (
{SORT_OPTIONS.map((opt) => { const isActive = opt.value === current return (
{ e.stopPropagation(); handleSelect(opt.value, isActive ? direction : DEFAULT_DIRECTIONS[opt.value]) }} > {opt.label}
) })}
)}
) }