import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover" import { useSearchContext } from "./context" import { Button } from "@/components/ui/button" import { cn } from "@/lib/utils" import { Check, ChevronDown, Mail, X } from "lucide-react" import React from "react" import { useContacts } from "@/hooks/use-contacts" import { useTranslation } from 'react-i18next' import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" export function MailFilterPopover() { const { t } = useTranslation() const { filter, setFilter } = useSearchContext() const fields = ['from', 'to', 'cc', 'bcc'] as const const activeCount = fields.filter(k => !!filter[k]).length const updateFilter = (field: string, email: string | undefined) => { setFilter(prev => ({ ...prev, [field]: email })) } const resetAll = () => { setFilter(prev => { const next = { ...prev } fields.forEach(k => delete next[k]) return next }) } return (
{fields.map((field) => ( updateFilter(field, email)} onReset={() => updateFilter(field, undefined)} /> ))}
{activeCount > 0 && (
)}
) } function ContactSelectorField({ label, value, onSelect, onReset }: { label: string value?: string onSelect: (email: string | undefined) => void onReset: () => void }) { const { t } = useTranslation() const [searchTerm, setSearchTerm] = React.useState("") const { contacts, isLoading } = useContacts(searchTerm) const handleToggle = (email: string) => { if (value === email) { onReset() } else { onSelect(email) } } return ( )} {value && (
)} {isLoading && (
{t('search_contacts.loading')}
)} {t('search_contacts.no_contact_found')} {contacts.slice(0, 100).map((email) => ( handleToggle(email)} className="flex items-center justify-between py-2.5 px-3 cursor-pointer whitespace-nowrap gap-4 text-xs" >
{email.split('@')[0]} {email}
{value === email && ( )}
))} {contacts.length > 100 && (
{t('search_contacts.showing_limit', { total: contacts.length })}
)}
) }