'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { useOrgAdmin } from '@/app/_lib/org-admin-context'; const MATCH_TYPES = ['HASHTAG', 'PREFIX', 'REACTION_EMOJI']; const ACTIONS = ['FLAG', 'AUTO_APPROVE', 'SKIP', 'REJECT', 'P1']; export default function OrgRulesPage() { const { orgAdmin, loading } = useOrgAdmin(); const router = useRouter(); const [rules, setRules] = useState([]); const [showAdd, setShowAdd] = useState(false); const [form, setForm] = useState({ matchType: 'HASHTAG', matchValue: '', action: 'FLAG', priority: 0 }); const [saving, setSaving] = useState(false); const [error, setError] = useState(''); async function load() { const res = await fetch('/api/org/rules'); if (res.ok) setRules(await res.json()); } useEffect(() => { if (loading) return; if (!orgAdmin) { router.replace('/org/login'); return; } void load(); }, [orgAdmin, loading, router]); async function handleAdd(e: React.FormEvent) { e.preventDefault(); setError(''); setSaving(true); try { const res = await fetch('/api/org/rules', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(form), }); if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error(err.message ?? 'Failed'); } setShowAdd(false); setForm({ matchType: 'HASHTAG', matchValue: '', action: 'FLAG', priority: 0 }); void load(); } catch (err) { setError(err instanceof Error ? err.message : 'Error'); } finally { setSaving(false); } } async function toggleActive(rule: any) { await fetch(`/api/org/rules/${rule.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ isActive: !rule.isActive }), }); void load(); } async function deleteRule(id: string) { if (!confirm('Delete this rule?')) return; await fetch(`/api/org/rules/${id}`, { method: 'DELETE' }); void load(); } if (loading || !orgAdmin) return

Loading...

; return (

Org-wide Rules

These rules apply to all chapters and take priority over chapter-specific rules.

{showAdd && (
void handleAdd(e)} className="bg-white rounded-xl border p-5 mb-6 flex flex-col gap-4">

New Org Rule

setForm({ ...form, matchValue: e.target.value })} className="w-full border rounded-lg px-3 py-2 text-sm" placeholder="e.g. #emergency" required />
setForm({ ...form, priority: Number(e.target.value) })} className="w-full border rounded-lg px-3 py-2 text-sm" />
{error &&

{error}

}
)}
{rules.map((r: any) => ( ))}
Match type Match value Action Priority Active
{r.matchType} {r.matchValue} {r.action} {r.priority}
{rules.length === 0 &&

No org rules yet.

}
); }