wacrm/src/app/(dashboard)/broadcasts/[id]/page.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

260 lines
9.1 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import { useParams, useRouter } from 'next/navigation';
import { createClient } from '@/lib/supabase/client';
import { Broadcast, BroadcastRecipient } from '@/types';
import { Button } from '@/components/ui/button';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import {
ArrowLeft,
Loader2,
Users,
Send,
CheckCheck,
Eye,
AlertCircle,
} from 'lucide-react';
const statusConfig: Record<string, { label: string; classes: string }> = {
draft: { label: 'Draft', classes: 'bg-slate-500/10 text-slate-400 border-slate-500/20' },
scheduled: { label: 'Scheduled', classes: 'bg-blue-500/10 text-blue-400 border-blue-500/20' },
sending: { label: 'Sending', classes: 'bg-yellow-500/10 text-yellow-400 border-yellow-500/20' },
sent: { label: 'Sent', classes: 'bg-emerald-500/10 text-emerald-400 border-emerald-500/20' },
failed: { label: 'Failed', classes: 'bg-red-500/10 text-red-400 border-red-500/20' },
};
const recipientStatusConfig: Record<string, { label: string; classes: string }> = {
pending: { label: 'Pending', classes: 'bg-slate-500/10 text-slate-400 border-slate-500/20' },
sent: { label: 'Sent', classes: 'bg-blue-500/10 text-blue-400 border-blue-500/20' },
delivered: { label: 'Delivered', classes: 'bg-emerald-500/10 text-emerald-400 border-emerald-500/20' },
read: { label: 'Read', classes: 'bg-emerald-500/10 text-emerald-300 border-emerald-500/20' },
replied: { label: 'Replied', classes: 'bg-purple-500/10 text-purple-400 border-purple-500/20' },
failed: { label: 'Failed', classes: 'bg-red-500/10 text-red-400 border-red-500/20' },
};
interface StatCardProps {
label: string;
value: number;
total: number;
icon: React.ReactNode;
color: string;
}
function StatCard({ label, value, total, icon, color }: StatCardProps) {
const pct = total > 0 ? Math.round((value / total) * 100) : 0;
return (
<div className="rounded-xl border border-slate-800 bg-slate-900 p-4">
<div className="flex items-center justify-between">
<div className={`flex h-8 w-8 items-center justify-center rounded-lg ${color}`}>
{icon}
</div>
<span className="text-xs text-slate-500">{pct}%</span>
</div>
<p className="mt-3 text-2xl font-bold text-white">{value.toLocaleString()}</p>
<p className="text-xs text-slate-400">{label}</p>
</div>
);
}
export default function BroadcastDetailPage() {
const params = useParams();
const router = useRouter();
const broadcastId = params.id as string;
const [broadcast, setBroadcast] = useState<Broadcast | null>(null);
const [recipients, setRecipients] = useState<BroadcastRecipient[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
async function fetchData() {
try {
const supabase = createClient();
const { data: bc, error: bcError } = await supabase
.from('broadcasts')
.select('*')
.eq('id', broadcastId)
.single();
if (bcError) throw bcError;
setBroadcast(bc);
const { data: recs, error: recsError } = await supabase
.from('broadcast_recipients')
.select('*, contact:contacts(*)')
.eq('broadcast_id', broadcastId)
.order('created_at', { ascending: false });
if (recsError) throw recsError;
setRecipients(recs ?? []);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load broadcast');
} finally {
setLoading(false);
}
}
fetchData();
}, [broadcastId]);
if (loading) {
return (
<div className="flex h-64 items-center justify-center">
<Loader2 className="h-6 w-6 animate-spin text-emerald-500" />
</div>
);
}
if (error || !broadcast) {
return (
<div className="flex h-64 flex-col items-center justify-center gap-2">
<p className="text-sm text-red-400">{error ?? 'Broadcast not found'}</p>
<Button variant="outline" onClick={() => router.push('/broadcasts')}>
Back to Broadcasts
</Button>
</div>
);
}
const status = statusConfig[broadcast.status] ?? statusConfig.draft;
return (
<div className="space-y-6">
{/* Header */}
<div className="flex items-start justify-between">
<div className="flex items-center gap-4">
<Button
variant="outline"
size="icon"
onClick={() => router.push('/broadcasts')}
className="border-slate-700"
>
<ArrowLeft className="h-4 w-4" />
</Button>
<div>
<div className="flex items-center gap-3">
<h1 className="text-2xl font-bold text-white">{broadcast.name}</h1>
<span
className={`inline-flex items-center rounded-full border px-2 py-0.5 text-xs font-medium ${status.classes}`}
>
{status.label}
</span>
</div>
<div className="mt-1 flex items-center gap-3 text-sm text-slate-400">
<span>Template: {broadcast.template_name}</span>
<span>-</span>
<span>Created {new Date(broadcast.created_at).toLocaleDateString()}</span>
</div>
</div>
</div>
</div>
{/* Stats */}
<div className="grid grid-cols-2 gap-3 sm:grid-cols-3 lg:grid-cols-5">
<StatCard
label="Total Recipients"
value={broadcast.total_recipients}
total={broadcast.total_recipients}
icon={<Users className="h-4 w-4" />}
color="bg-slate-800 text-slate-300"
/>
<StatCard
label="Sent"
value={broadcast.sent_count}
total={broadcast.total_recipients}
icon={<Send className="h-4 w-4" />}
color="bg-blue-500/10 text-blue-400"
/>
<StatCard
label="Delivered"
value={broadcast.delivered_count}
total={broadcast.total_recipients}
icon={<CheckCheck className="h-4 w-4" />}
color="bg-emerald-500/10 text-emerald-400"
/>
<StatCard
label="Read"
value={broadcast.read_count}
total={broadcast.total_recipients}
icon={<Eye className="h-4 w-4" />}
color="bg-emerald-500/10 text-emerald-300"
/>
<StatCard
label="Failed"
value={broadcast.failed_count}
total={broadcast.total_recipients}
icon={<AlertCircle className="h-4 w-4" />}
color="bg-red-500/10 text-red-400"
/>
</div>
{/* Recipients Table */}
<div className="rounded-xl border border-slate-800 bg-slate-900">
<div className="border-b border-slate-800 px-4 py-3">
<h2 className="text-sm font-medium text-white">
Recipients ({recipients.length})
</h2>
</div>
{recipients.length === 0 ? (
<div className="flex h-32 items-center justify-center">
<p className="text-sm text-slate-400">No recipients found.</p>
</div>
) : (
<Table>
<TableHeader>
<TableRow className="border-slate-800 hover:bg-transparent">
<TableHead className="text-slate-400">Contact</TableHead>
<TableHead className="text-slate-400">Phone</TableHead>
<TableHead className="text-slate-400">Status</TableHead>
<TableHead className="text-slate-400">Sent At</TableHead>
<TableHead className="text-slate-400">Error</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{recipients.map((recipient) => {
const rStatus =
recipientStatusConfig[recipient.status] ?? recipientStatusConfig.pending;
return (
<TableRow key={recipient.id} className="border-slate-800">
<TableCell className="font-medium text-white">
{recipient.contact?.name ?? 'Unknown'}
</TableCell>
<TableCell className="text-slate-300">
{recipient.contact?.phone ?? '-'}
</TableCell>
<TableCell>
<span
className={`inline-flex items-center rounded-full border px-2 py-0.5 text-xs font-medium ${rStatus.classes}`}
>
{rStatus.label}
</span>
</TableCell>
<TableCell className="text-slate-400">
{recipient.sent_at
? new Date(recipient.sent_at).toLocaleString()
: '-'}
</TableCell>
<TableCell className="max-w-xs truncate text-xs text-red-400">
{recipient.error_message ?? '-'}
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
)}
</div>
</div>
);
}