"use client"; import { dataTableFilterPopoverContentClassName } from "@app/lib/dataTableFilterPopover"; import type { Measurable } from "@radix-ui/rect"; import { PlusIcon } from "lucide-react"; import { useTranslations } from "next-intl"; import { useRef, useState } from "react"; import { LabelBadge } from "./label-badge"; import { LabelOverflowBadge } from "./label-overflow-badge"; import { LabelsSelector, type SelectedLabel } from "./labels-selector"; import { Button } from "./ui/button"; import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "./ui/popover"; const MAX_VISIBLE_LABELS = 4; const MAX_VISIBLE_BEFORE_OVERFLOW = MAX_VISIBLE_LABELS - 1; type TableLabelsCellProps = { orgId: string; localLabels: SelectedLabel[]; toggleLabel: (label: SelectedLabel, action: "attach" | "detach") => void; }; export function TableLabelsCell({ orgId, localLabels, toggleLabel }: TableLabelsCellProps) { const t = useTranslations(); const [isPopoverOpen, setIsPopoverOpen] = useState(false); const triggerRef = useRef(null); const frozenAnchorRef = useRef({ getBoundingClientRect: () => new DOMRect() }); const hasOverflow = localLabels.length > MAX_VISIBLE_LABELS; const visibleLabels = localLabels.slice( 0, hasOverflow ? MAX_VISIBLE_BEFORE_OVERFLOW : MAX_VISIBLE_LABELS ); const overflowLabels = hasOverflow ? localLabels.slice(MAX_VISIBLE_BEFORE_OVERFLOW) : []; function handleOpenChange(open: boolean) { if (open && triggerRef.current) { const rect = triggerRef.current.getBoundingClientRect(); frozenAnchorRef.current = { getBoundingClientRect: () => rect }; } setIsPopoverOpen(open); } return (
handleOpenChange(false)} />
{visibleLabels.map((label) => ( handleOpenChange(true)} {...label} /> ))} {overflowLabels.length > 0 && ( handleOpenChange(true)} /> )}
); }