"use client"; import { useCallback, useEffect, useRef } from "react"; import { useTranslations } from "next-intl"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Paperclip, Star, Mail, MailOpen, X, RotateCcw, } from "lucide-react"; import { cn } from "@/lib/utils"; import { SearchFilters } from "@/lib/jmap/search-utils"; interface AdvancedSearchPanelProps { filters: SearchFilters; isOpen: boolean; onFiltersChange: (filters: Partial) => void; onClear: () => void; onSearch: () => void; onClose: () => void; } export function AdvancedSearchPanel({ filters, isOpen, onFiltersChange, onClear, onSearch, onClose, }: AdvancedSearchPanelProps) { const t = useTranslations("advanced_search"); const debounceRef = useRef(null); const debouncedSearch = useCallback(() => { if (debounceRef.current) { clearTimeout(debounceRef.current); } debounceRef.current = setTimeout(() => { onSearch(); }, 300); }, [onSearch]); useEffect(() => { return () => { if (debounceRef.current) { clearTimeout(debounceRef.current); } }; }, []); const handleTextChange = (field: keyof SearchFilters, value: string) => { onFiltersChange({ [field]: value }); debouncedSearch(); }; const handleToggle = (field: "hasAttachment" | "isUnread" | "isStarred", current: boolean | null) => { const next = current === null ? true : current === true ? false : null; onFiltersChange({ [field]: next }); onSearch(); }; const handleDateChange = (field: "dateAfter" | "dateBefore", value: string) => { onFiltersChange({ [field]: value }); onSearch(); }; const handleClear = () => { if (debounceRef.current) { clearTimeout(debounceRef.current); } onClear(); }; if (!isOpen) return null; return (
{t("title")}
handleTextChange("from", e.target.value)} placeholder={t("from_placeholder")} className="h-8 text-sm" />
handleTextChange("to", e.target.value)} placeholder={t("to_placeholder")} className="h-8 text-sm" />
handleTextChange("subject", e.target.value)} placeholder={t("subject_placeholder")} className="h-8 text-sm" />
handleDateChange("dateAfter", e.target.value)} className="h-8 text-sm" />
handleDateChange("dateBefore", e.target.value)} className="h-8 text-sm" />
} label={t("has_attachment")} value={filters.hasAttachment} onClick={() => handleToggle("hasAttachment", filters.hasAttachment)} /> } label={t("starred")} value={filters.isStarred} onClick={() => handleToggle("isStarred", filters.isStarred)} /> : } label={filters.isUnread === false ? t("read") : t("unread")} value={filters.isUnread} onClick={() => handleToggle("isUnread", filters.isUnread)} />
); } function ToggleFilterButton({ icon, label, value, onClick, }: { icon: React.ReactNode; label: string; value: boolean | null; onClick: () => void; }) { return ( ); }