"use client"; import React, { useState, useRef, useEffect } from 'react'; import { FileText, ChevronDown, Search, Image as ImageIcon, Sparkles, ArrowRight, Check, X, Plus } from 'lucide-react'; export interface TemplateItem { id: number | string; title?: string; name?: string; content: string; media_url?: string | null; } interface TemplatePickerDropdownProps { templates: TemplateItem[]; onSelectTemplate: (template: TemplateItem) => void; } export function TemplatePickerDropdown({ templates = [], onSelectTemplate }: TemplatePickerDropdownProps) { const [isOpen, setIsOpen] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const dropdownRef = useRef(null); // Close on click outside useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) { setIsOpen(false); } }; if (isOpen) { document.addEventListener('mousedown', handleClickOutside); } return () => document.removeEventListener('mousedown', handleClickOutside); }, [isOpen]); const templateList = templates || []; const filtered = templateList.filter(t => { const titleStr = t.title || t.name || ''; const contentStr = t.content || ''; return titleStr.toLowerCase().includes(searchQuery.toLowerCase()) || contentStr.toLowerCase().includes(searchQuery.toLowerCase()); }); return (
{/* Presentable Dropdown Trigger Button */} {/* Custom Floating Templates Menu */} {isOpen && (
e.stopPropagation()} className="absolute right-0 top-full mt-2 z-[200] w-72 sm:w-84 bg-[#0f1117]/95 backdrop-blur-2xl border border-white/[0.12] rounded-2xl shadow-[0_25px_60px_-15px_rgba(0,0,0,0.9)] p-3 animate-in fade-in zoom-in-95 duration-150 flex flex-col overflow-hidden select-none" style={{ minWidth: '290px' }} > {/* Glowing Top Beam */}
{/* Header */}
Select Template
{filtered.length} of {templateList.length}
{/* Search Input (if > 2 templates) */} {templateList.length > 2 && (
setSearchQuery(e.target.value)} placeholder="Search templates..." className="w-full bg-[#151922] border border-white/[0.07] hover:border-indigo-500/30 focus:border-indigo-500/60 text-white rounded-lg pl-7 pr-7 py-1 text-[11px] outline-none transition-all placeholder:text-foreground/30 font-medium" autoFocus /> {searchQuery && ( )}
)} {/* Template Cards List */}
{templateList.length === 0 ? (

No saved templates

Create templates in the Templates section to reuse them here in one click.

) : filtered.length === 0 ? (
No templates match "{searchQuery}"
) : ( filtered.map((t) => { const hasSpintax = (t.content || '').includes('{') && (t.content || '').includes('|'); const hasMedia = !!t.media_url; return (
{ onSelectTemplate(t); setIsOpen(false); }} className="p-2.5 rounded-xl bg-white/[0.02] hover:bg-indigo-500/[0.08] border border-white/[0.05] hover:border-indigo-500/40 cursor-pointer transition-all group/item space-y-1" >
{t.title || t.name || "Untitled Template"}
{hasSpintax && ( Spintax )} {hasMedia && ( Media )}

{t.content || "Empty content"}

); }) )}
)}
); }