'use client'; import { useEffect, useState } from 'react'; interface RouteForMessage { routeId: string; targetGroupId: string; targetGroupName: string; approvalMode: 'MANUAL' | 'AUTO'; forwardFormat: 'THREADED' | 'DIGEST' | 'SUMMARY'; withAttachment: boolean; frequency: string; ruleDecision: 'FORWARD' | 'BLOCK' | 'NONE'; } export function ForwardTargetDialog({ messageId, onApproved, onClose, }: { messageId: string; onApproved: () => void; onClose: () => void; }) { const [routes, setRoutes] = useState([]); const [selected, setSelected] = useState>(new Set()); const [loading, setLoading] = useState(true); const [busy, setBusy] = useState(false); const [search, setSearch] = useState(''); useEffect(() => { fetch(`/api/messages/${messageId}/routes`) .then((r) => r.json()) .then((data: RouteForMessage[]) => { setRoutes(Array.isArray(data) ? data : []); // Pre-select: FORWARD decision, or AUTO approvalMode with no BLOCK const preSelected = new Set( (Array.isArray(data) ? data : []) .filter((r) => r.ruleDecision === 'FORWARD' || (r.ruleDecision === 'NONE' && r.approvalMode === 'AUTO')) .map((r) => r.targetGroupId), ); setSelected(preSelected); }) .catch(() => {}) .finally(() => setLoading(false)); }, [messageId]); function toggleGroup(groupId: string) { setSelected((prev) => { const next = new Set(prev); if (next.has(groupId)) next.delete(groupId); else next.add(groupId); return next; }); } async function confirm() { setBusy(true); try { const res = await fetch(`/api/messages/${messageId}/approve`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ targetGroupIds: [...selected] }), }); if (res.ok) { onApproved(); onClose(); } } finally { setBusy(false); } } const filtered = routes.filter((r) => r.targetGroupName.toLowerCase().includes(search.toLowerCase()), ); return (

Approve & Forward

setSearch(e.target.value)} placeholder="Search groups…" className="w-full border border-gray-200 rounded-lg px-3 py-2 text-sm" />
{loading ? (

Loading routes…

) : filtered.length === 0 ? (

No target groups found.

) : (
    {filtered.map((r) => { const isBlocked = r.ruleDecision === 'BLOCK'; const isChecked = selected.has(r.targetGroupId); return (
  • ); })}
)}
{selected.size} group{selected.size !== 1 ? 's' : ''} selected
); }