"use client"; import * as React from "react"; import { Check, PlusCircle } from "lucide-react"; import { cn } from "@/lib/utils"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Separator } from "@/components/ui/separator"; export interface FacetedFilterOption { label: string; value: string; icon?: React.ComponentType<{ className?: string }>; } export interface DataTableServerFacetedFilterLabels { clear: string; empty: string; selected: (count: number) => string; } const DEFAULT_LABELS: DataTableServerFacetedFilterLabels = { clear: "Clear", empty: "No results found.", selected: (count) => `${count} selected`, }; interface DataTableServerFacetedFilterProps { title: string; options: FacetedFilterOption[]; value: TFilter | ""; onFilterChange: (value: TFilter | "") => void; labels?: Partial; multi?: boolean; } export function DataTableServerFacetedFilter({ title, options, value, onFilterChange, labels, multi = true, }: DataTableServerFacetedFilterProps) { const t = { ...DEFAULT_LABELS, ...labels }; const selected = React.useMemo>(() => { if (!value) return new Set(); return multi ? new Set(String(value).split(",").filter(Boolean)) : new Set([String(value)]); }, [multi, value]); const selectedCount = selected.size; const toggleMulti = (nextValue: string) => { const next = new Set(selected); if (next.has(nextValue)) { next.delete(nextValue); } else { next.add(nextValue); } onFilterChange((Array.from(next).join(",") as TFilter) || ""); }; const chooseSingle = (nextValue: string) => { const current = selected.values().next().value as string | undefined; onFilterChange(current === nextValue ? "" : (nextValue as TFilter)); }; return ( {t.empty} {options.map((option) => { const isSelected = selected.has(option.value); return ( { if (multi) { toggleMulti(option.value); } else { chooseSingle(option.value); } }} >
{option.icon ? ( ) : null} {option.label}
); })}
{selectedCount > 0 ? ( <> onFilterChange("")} className="justify-center"> {t.clear} ) : null}
); }