'use client' import { useState, useEffect } from 'react' import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, DropdownMenuSeparator, DropdownMenuLabel, } from '@/components/ui/dropdown-menu' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Filter, Check } from 'lucide-react' import { cn } from '@/lib/utils' import { useLabels } from '@/context/LabelContext' import { LabelBadge } from './label-badge' interface LabelFilterProps { selectedLabels: string[] onFilterChange: (labels: string[]) => void } export function LabelFilter({ selectedLabels, onFilterChange }: LabelFilterProps) { const { labels, loading } = useLabels() const [allLabelNames, setAllLabelNames] = useState([]) useEffect(() => { // Extract label names from labels array setAllLabelNames(labels.map((l: any) => l.name).sort()) }, [labels]) const handleToggleLabel = (label: string) => { if (selectedLabels.includes(label)) { onFilterChange(selectedLabels.filter((l: string) => l !== label)) } else { onFilterChange([...selectedLabels, label]) } } const handleClearAll = () => { onFilterChange([]) } if (loading || allLabelNames.length === 0) return null return (
Filter by Labels {selectedLabels.length > 0 && ( )} {/* Label Filters */}
{!loading && allLabelNames.map((labelName: string) => { const isSelected = selectedLabels.includes(labelName) return (
{ e.preventDefault() handleToggleLabel(labelName) }} className={cn( "flex items-center gap-2 p-2 rounded-sm cursor-pointer text-sm hover:bg-accent hover:text-accent-foreground" )} >
) })}
{/* Active filters display */} {!loading && selectedLabels.length > 0 && (
{selectedLabels.map((labelName: string) => ( handleToggleLabel(labelName)} /> ))}
)}
) }