"use client"; import { useCallback, useRef, useState, useTransition } from "react"; import { Check, ChevronDown } from "lucide-react"; import { setDefaultSort } from "@/app/actions/sort"; import { SORT_OPTIONS, labelFor, type SortKey } from "@/lib/sort"; import { useClickOutside } from "@/lib/hooks/useClickOutside"; import { cn } from "@/lib/utils"; export function DefaultSortSelect({ initial }: { initial: SortKey }) { const [value, setValue] = useState(initial); const [open, setOpen] = useState(false); const [saved, setSaved] = useState(false); const [pending, start] = useTransition(); const ref = useRef(null); useClickOutside(ref, useCallback(() => setOpen(false), []), open); const choose = (next: SortKey) => { setOpen(false); if (next === value) return; setValue(next); start(async () => { await setDefaultSort(next); setSaved(true); setTimeout(() => setSaved(false), 1400); }); }; return (
Default Sort
Used on every grid page when no sort is chosen. Persisted on the server.
{saved && ( Saved )}
{open && (
{SORT_OPTIONS.map((o) => { const active = o.value === value; return ( ); })}
)}
); }