wacrm/src/components/settings/template-manager.tsx
Arnas Donauskas 658d070811
Initial scaffold: WhatsApp CRM with Next.js + Supabase
Complete WhatsApp CRM application built with Next.js 14 (App Router),
Supabase (PostgreSQL + Auth + Realtime), and shadcn/ui.

Features:
- Auth: Login, signup, forgot password with Supabase Auth
- Dashboard: Analytics overview with stats, recent activity
- Inbox: Real-time WhatsApp conversations with message threading
- Contacts: CRUD with tags, custom fields, notes, CSV import
- Broadcasts: 4-step wizard with audience selection, template sending
- Pipelines: Kanban board with deal management
- Settings: WhatsApp API config, templates, tags
- WhatsApp API: Webhook (verify + receive), send, broadcast, media proxy
- Database: 16 tables with RLS policies, indexes, realtime subscriptions
- Dark theme throughout with emerald accents

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 18:17:31 +03:00

363 lines
12 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import { toast } from 'sonner';
import { Plus, Trash2, Loader2 } from 'lucide-react';
import { createClient } from '@/lib/supabase/client';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import type { MessageTemplate } from '@/types';
const CATEGORIES = ['Marketing', 'Utility', 'Authentication'] as const;
const HEADER_TYPES = ['text', 'image', 'video', 'document'] as const;
const categoryColors: Record<string, string> = {
Marketing: 'bg-purple-600/20 text-purple-400 border-purple-600/30',
Utility: 'bg-blue-600/20 text-blue-400 border-blue-600/30',
Authentication: 'bg-amber-600/20 text-amber-400 border-amber-600/30',
};
const statusColors: Record<string, string> = {
Draft: 'bg-slate-600/20 text-slate-400 border-slate-600/30',
Pending: 'bg-yellow-600/20 text-yellow-400 border-yellow-600/30',
Approved: 'bg-emerald-600/20 text-emerald-400 border-emerald-600/30',
Rejected: 'bg-red-600/20 text-red-400 border-red-600/30',
};
interface TemplateFormData {
name: string;
category: MessageTemplate['category'];
language: string;
body_text: string;
header_type: string;
footer_text: string;
}
const emptyForm: TemplateFormData = {
name: '',
category: 'Marketing',
language: 'en',
body_text: '',
header_type: '',
footer_text: '',
};
export function TemplateManager() {
const supabase = createClient();
const [loading, setLoading] = useState(true);
const [templates, setTemplates] = useState<MessageTemplate[]>([]);
const [dialogOpen, setDialogOpen] = useState(false);
const [saving, setSaving] = useState(false);
const [form, setForm] = useState<TemplateFormData>(emptyForm);
useEffect(() => {
fetchTemplates();
}, []);
async function fetchTemplates() {
try {
setLoading(true);
const { data: { user } } = await supabase.auth.getUser();
if (!user) return;
const { data, error } = await supabase
.from('message_templates')
.select('*')
.eq('user_id', user.id)
.order('created_at', { ascending: false });
if (error) throw error;
setTemplates(data || []);
} catch (err) {
console.error('Failed to fetch templates:', err);
toast.error('Failed to load templates');
} finally {
setLoading(false);
}
}
async function handleSave() {
if (!form.name.trim()) {
toast.error('Template name is required');
return;
}
if (!form.body_text.trim()) {
toast.error('Body text is required');
return;
}
try {
setSaving(true);
const { data: { user } } = await supabase.auth.getUser();
if (!user) {
toast.error('Not authenticated');
return;
}
const payload = {
user_id: user.id,
name: form.name.trim(),
category: form.category,
language: form.language.trim() || 'en',
body_text: form.body_text.trim(),
header_type: form.header_type || null,
footer_text: form.footer_text.trim() || null,
status: 'Draft' as const,
};
const { error } = await supabase
.from('message_templates')
.insert(payload);
if (error) throw error;
toast.success('Template created successfully');
setDialogOpen(false);
setForm(emptyForm);
await fetchTemplates();
} catch (err) {
console.error('Save error:', err);
toast.error('Failed to create template');
} finally {
setSaving(false);
}
}
async function handleDelete(id: string) {
try {
const { error } = await supabase
.from('message_templates')
.delete()
.eq('id', id);
if (error) throw error;
toast.success('Template deleted');
setTemplates((prev) => prev.filter((t) => t.id !== id));
} catch (err) {
console.error('Delete error:', err);
toast.error('Failed to delete template');
}
}
if (loading) {
return (
<div className="flex items-center justify-center py-12">
<Loader2 className="size-6 animate-spin text-emerald-500" />
</div>
);
}
return (
<div className="space-y-4 mt-4">
<div className="flex items-center justify-between">
<div>
<h2 className="text-lg font-semibold text-white">Message Templates</h2>
<p className="text-sm text-slate-400">Create and manage your WhatsApp message templates.</p>
</div>
<Button
onClick={() => {
setForm(emptyForm);
setDialogOpen(true);
}}
className="bg-emerald-600 hover:bg-emerald-700 text-white"
>
<Plus className="size-4" />
New Template
</Button>
</div>
{templates.length === 0 ? (
<Card className="bg-slate-900 border-slate-700 ring-0 ring-transparent">
<CardContent className="flex flex-col items-center justify-center py-12 text-center">
<p className="text-slate-400 text-sm">No templates yet.</p>
<p className="text-slate-500 text-xs mt-1">Create your first message template to get started.</p>
</CardContent>
</Card>
) : (
<div className="grid gap-3">
{templates.map((template) => (
<Card key={template.id} className="bg-slate-900 border-slate-700 ring-0 ring-transparent">
<CardContent className="flex items-start justify-between pt-4">
<div className="space-y-2 min-w-0 flex-1">
<div className="flex items-center gap-2 flex-wrap">
<h3 className="font-medium text-white">{template.name}</h3>
<Badge
className={`text-xs border ${categoryColors[template.category] || ''}`}
>
{template.category}
</Badge>
<Badge
className={`text-xs border ${statusColors[template.status || 'Draft'] || ''}`}
>
{template.status || 'Draft'}
</Badge>
{template.language && (
<span className="text-xs text-slate-500 uppercase">{template.language}</span>
)}
</div>
<p className="text-sm text-slate-400 line-clamp-2">{template.body_text}</p>
{template.footer_text && (
<p className="text-xs text-slate-500 italic">{template.footer_text}</p>
)}
</div>
<Button
variant="ghost"
size="icon"
onClick={() => handleDelete(template.id)}
className="text-slate-400 hover:text-red-400 hover:bg-red-950/30 shrink-0 ml-2"
>
<Trash2 className="size-4" />
</Button>
</CardContent>
</Card>
))}
</div>
)}
{/* New Template Dialog */}
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogContent className="bg-slate-900 border-slate-700 sm:max-w-lg">
<DialogHeader>
<DialogTitle className="text-white">New Message Template</DialogTitle>
<DialogDescription className="text-slate-400">
Create a new WhatsApp message template.
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-2">
<div className="space-y-2">
<Label className="text-slate-300">Template Name</Label>
<Input
placeholder="e.g. order_confirmation"
value={form.name}
onChange={(e) => setForm({ ...form, name: e.target.value })}
className="bg-slate-800 border-slate-700 text-white placeholder:text-slate-500"
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label className="text-slate-300">Category</Label>
<Select
value={form.category}
onValueChange={(val) =>
setForm({ ...form, category: val as MessageTemplate['category'] })
}
>
<SelectTrigger className="w-full bg-slate-800 border-slate-700 text-white">
<SelectValue />
</SelectTrigger>
<SelectContent className="bg-slate-800 border-slate-700">
{CATEGORIES.map((cat) => (
<SelectItem key={cat} value={cat} className="text-white focus:bg-slate-700 focus:text-white">
{cat}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label className="text-slate-300">Language</Label>
<Input
placeholder="en"
value={form.language}
onChange={(e) => setForm({ ...form, language: e.target.value })}
className="bg-slate-800 border-slate-700 text-white placeholder:text-slate-500"
/>
</div>
</div>
<div className="space-y-2">
<Label className="text-slate-300">Header Type</Label>
<Select
value={form.header_type}
onValueChange={(val) => setForm({ ...form, header_type: val || '' })}
>
<SelectTrigger className="w-full bg-slate-800 border-slate-700 text-white">
<SelectValue placeholder="None" />
</SelectTrigger>
<SelectContent className="bg-slate-800 border-slate-700">
<SelectItem value="none" className="text-white focus:bg-slate-700 focus:text-white">
None
</SelectItem>
{HEADER_TYPES.map((type) => (
<SelectItem key={type} value={type} className="text-white focus:bg-slate-700 focus:text-white">
{type.charAt(0).toUpperCase() + type.slice(1)}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label className="text-slate-300">Body Text</Label>
<Textarea
placeholder="Enter your template message body. Use {{1}}, {{2}} for variables."
value={form.body_text}
onChange={(e) => setForm({ ...form, body_text: e.target.value })}
rows={4}
className="bg-slate-800 border-slate-700 text-white placeholder:text-slate-500 resize-none"
/>
</div>
<div className="space-y-2">
<Label className="text-slate-300">Footer Text</Label>
<Input
placeholder="Optional footer text"
value={form.footer_text}
onChange={(e) => setForm({ ...form, footer_text: e.target.value })}
className="bg-slate-800 border-slate-700 text-white placeholder:text-slate-500"
/>
</div>
</div>
<DialogFooter className="bg-slate-900 border-slate-700">
<Button
variant="outline"
onClick={() => setDialogOpen(false)}
className="border-slate-700 text-slate-300 hover:bg-slate-800"
>
Cancel
</Button>
<Button
onClick={handleSave}
disabled={saving}
className="bg-emerald-600 hover:bg-emerald-700 text-white"
>
{saving ? (
<>
<Loader2 className="size-4 animate-spin" />
Creating...
</>
) : (
'Create Template'
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}