arkitel / frontend /src /components /TemplatePickerDropdown.tsx
CBV01
fix(campaigns): fix template dropdown overflow clipping and place time beside calendar
866c437
Raw History Blame Contribute Delete
10.1 kB
"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<HTMLDivElement>(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 (
<div ref={dropdownRef} className="relative inline-block shrink-0">
{/* Presentable Dropdown Trigger Button */}
<button
type="button"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
setIsOpen(prev => !prev);
}}
className={`px-2.5 py-1 rounded-md text-[10px] font-bold border transition-all flex items-center gap-1.5 cursor-pointer active:scale-95 shrink-0 ${
isOpen
? 'bg-indigo-500/20 text-indigo-300 border-indigo-500/40 shadow-xs'
: 'bg-indigo-500/10 hover:bg-indigo-500/15 text-indigo-400 border-indigo-500/25 hover:border-indigo-500/40'
}`}
title="Select from your saved message templates"
>
<FileText size={10} className="text-indigo-400 shrink-0" />
<span>Templates</span>
<span className="px-1.5 py-0.2 rounded-full bg-indigo-500/25 text-indigo-300 text-[9px] font-extrabold border border-indigo-500/30">
{templateList.length}
</span>
<ChevronDown size={10} className={`text-indigo-400/60 transition-transform duration-200 ${isOpen ? 'rotate-180 text-indigo-300' : ''}`} />
</button>
{/* Custom Floating Templates Menu */}
{isOpen && (
<div
onClick={(e) => 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 */}
<div className="absolute top-0 left-1/4 right-1/4 h-[2px] bg-gradient-to-r from-transparent via-indigo-500 to-transparent pointer-events-none" />
{/* Header */}
<div className="flex items-center justify-between pb-2 mb-2 border-b border-white/[0.06]">
<div className="flex items-center gap-1.5">
<FileText size={12} className="text-indigo-400" />
<span className="text-xs font-bold text-white tracking-tight">Select Template</span>
</div>
<span className="text-[10px] text-foreground/40 font-mono font-medium">
{filtered.length} of {templateList.length}
</span>
</div>
{/* Search Input (if > 2 templates) */}
{templateList.length > 2 && (
<div className="relative mb-2.5">
<Search size={11} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-foreground/40" />
<input
type="text"
value={searchQuery}
onChange={(e) => 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 && (
<button
type="button"
onClick={() => setSearchQuery('')}
className="absolute right-2 top-1/2 -translate-y-1/2 text-foreground/40 hover:text-white"
>
<X size={11} />
</button>
)}
</div>
)}
{/* Template Cards List */}
<div className="max-h-60 overflow-y-auto space-y-1.5 custom-scrollbar pr-0.5">
{templateList.length === 0 ? (
<div className="py-7 px-3 text-center space-y-2">
<div className="w-8 h-8 rounded-full bg-indigo-500/10 text-indigo-400 flex items-center justify-center mx-auto">
<FileText size={16} />
</div>
<p className="text-xs font-semibold text-foreground/70">No saved templates</p>
<p className="text-[10px] text-foreground/40 leading-relaxed">
Create templates in the Templates section to reuse them here in one click.
</p>
</div>
) : filtered.length === 0 ? (
<div className="py-6 text-center text-foreground/40 text-xs">
No templates match &quot;{searchQuery}&quot;
</div>
) : (
filtered.map((t) => {
const hasSpintax = (t.content || '').includes('{') && (t.content || '').includes('|');
const hasMedia = !!t.media_url;
return (
<div
key={t.id}
onClick={() => {
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"
>
<div className="flex items-center justify-between gap-1.5">
<h5 className="text-xs font-bold text-white group-hover/item:text-indigo-300 transition-colors truncate">
{t.title || t.name || "Untitled Template"}
</h5>
<div className="flex items-center gap-1 shrink-0">
{hasSpintax && (
<span className="text-[8px] font-bold px-1.5 py-0.5 rounded bg-emerald-500/10 text-emerald-400 border border-emerald-500/20 uppercase tracking-wider">
Spintax
</span>
)}
{hasMedia && (
<span className="text-[8px] font-bold px-1.5 py-0.5 rounded bg-purple-500/10 text-purple-400 border border-purple-500/20 flex items-center gap-0.5">
<ImageIcon size={8} /> Media
</span>
)}
<ArrowRight size={10} className="text-foreground/30 group-hover/item:text-indigo-400 group-hover/item:translate-x-0.5 transition-all ml-0.5" />
</div>
</div>
<p className="text-[11px] text-foreground/50 line-clamp-2 leading-relaxed font-normal group-hover/item:text-foreground/75 transition-colors">
{t.content || "Empty content"}
</p>
</div>
);
})
)}
</div>
</div>
)}
</div>
);
}