"use client"; import { Button } from "@app/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@app/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@app/components/ui/popover"; import { cn } from "@app/lib/cn"; import { dataTableFilterPopoverContentClassName } from "@app/lib/dataTableFilterPopover"; import { CheckIcon, Funnel } from "lucide-react"; import { useTranslations } from "next-intl"; import { useMemo, useState } from "react"; import { orgQueries } from "@app/lib/queries"; import { useQuery } from "@tanstack/react-query"; import { useDebounce } from "use-debounce"; import { LabelBadge } from "./label-badge"; import { LabelOverflowBadge } from "./label-overflow-badge"; import { LABEL_COLORS } from "./labels-selector"; function areSelectionsEqual(a: string[], b: string[]) { if (a.length !== b.length) { return false; } const setB = new Set(b); return a.every((value) => setB.has(value)); } type LabelColumnFilterButtonProps = { selectedValues: string[]; onSelectedValuesChange: (values: string[]) => void; className?: string; label: string; orgId: string; }; export function LabelColumnFilterButton({ selectedValues, onSelectedValuesChange, className, label, orgId }: LabelColumnFilterButtonProps) { const [open, setOpen] = useState(false); const [draftValues, setDraftValues] = useState(selectedValues); const t = useTranslations(); const [labelSearchQuery, setlabelsSearchQuery] = useState(""); const [debouncedQuery] = useDebounce(labelSearchQuery, 150); const { data: labels = [] } = useQuery( orgQueries.labels({ orgId, query: debouncedQuery, perPage: 500 }) ); const draftSet = useMemo(() => new Set(draftValues), [draftValues]); const selectedLabels = useMemo( () => selectedValues.map((name) => { const foundLabel = labels.find((label) => label.name === name); return { name, color: foundLabel?.color ?? LABEL_COLORS.gray }; }), [selectedValues, labels] ); const summary = useMemo(() => { if (selectedLabels.length === 0) { return null; } if (selectedLabels.length === 1) { const label = selectedLabels[0]; return ( ); } return ( ); }, [selectedLabels]); function toggle(value: string) { setDraftValues((current) => current.includes(value) ? current.filter((v) => v !== value) : [...current, value] ); } function handleOpenChange(nextOpen: boolean) { if (nextOpen) { setDraftValues(selectedValues); setOpen(true); return; } setOpen(false); if (!areSelectionsEqual(draftValues, selectedValues)) { onSelectedValuesChange(draftValues); } } return (
{t("labelsNotFound")} {draftValues.length > 0 && ( { setDraftValues([]); }} className="text-muted-foreground" > {t("accessLabelFilterClear")} )} {labels.map((label) => ( { toggle(label.name); }} className="flex items-center gap-2" >
{label.name} ))}
); }