"use client"; import * as Popover from "@radix-ui/react-popover"; import * as Select from "@radix-ui/react-select"; import { ArrowDown, ArrowDownAZ, ArrowUpAZ, Plus, X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Separator } from "@/components/ui/separator"; import type { ViewSort } from "@/lib/hooks/use-view-data"; import { useViewStore } from "@/lib/stores/view-store"; import { cn } from "@/lib/utils"; const SORT_FIELDS = [ { value: "title", label: "Title" }, { value: "status", label: "Status" }, { value: "sortOrder", label: "Order" }, { value: "createdAt", label: "Created" }, { value: "updatedAt", label: "Updated" }, ] as const; const selectTriggerClass = cn( "flex h-8 min-w-[5.5rem] shrink-0 items-center justify-between gap-1 rounded-md border border-input bg-background px-2 text-xs", "focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:ring-offset-background", ); const selectContentClass = cn( "z-[60] max-h-[min(60vh,20rem)] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md", "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0", ); const selectItemClass = "relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-xs outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50"; function SortSelect({ value, options, onChange, placeholder, className, }: { value: string; options: { value: string; label: string }[]; onChange: (v: string) => void; placeholder?: string; className?: string; }) { return ( {options.map((o) => ( {o.label} ))} ); } function defaultSort(): ViewSort { return { field: "sortOrder", direction: "asc" }; } function SortRow({ sort, index, onChange, onRemove, }: { sort: ViewSort; index: number; onChange: (next: ViewSort) => void; onRemove: () => void; }) { const field = SORT_FIELDS.some((f) => f.value === sort.field) ? sort.field : "sortOrder"; const direction = sort.direction === "desc" ? "desc" : "asc"; return (
onChange({ ...sort, field: v })} options={SORT_FIELDS.map((f) => ({ value: f.value, label: f.label }))} placeholder="Field" className="min-w-[7rem] flex-1" />
); } export function SortConfig() { const sorts = useViewStore((s) => s.config.sorts); const addSort = useViewStore((s) => s.addSort); const removeSort = useViewStore((s) => s.removeSort); const updateSort = useViewStore((s) => s.updateSort); return (

Sort by

{sorts.map((s, i) => ( updateSort(i, next)} onRemove={() => removeSort(i)} /> ))}
); }