'use client'; import { useState, useEffect, useCallback } from 'react'; import Link from 'next/link'; import { ArrowLeft, Plus, Trash2, Zap, RefreshCw, Send, Globe } from 'lucide-react'; import { listSubscriptions, createSubscription, deleteSubscription, testWebhook, getEventTypes, type WebhookSubscription, type EventType, } from '@/lib/webhook-client'; export default function WebhooksPage() { const [subs, setSubs] = useState([]); const [eventTypes, setEventTypes] = useState([]); const [loading, setLoading] = useState(true); const [showCreate, setShowCreate] = useState(false); const [newUrl, setNewUrl] = useState(''); const [newEvents, setNewEvents] = useState([]); const [newDesc, setNewDesc] = useState(''); const [error, setError] = useState(null); const loadData = useCallback(async () => { setLoading(true); try { const [subsData, typesData] = await Promise.all([listSubscriptions(), getEventTypes()]); setSubs(subsData); setEventTypes(typesData.eventTypes); } catch (e) { setError(e instanceof Error ? e.message : 'Failed to load'); } setLoading(false); }, []); useEffect(() => { loadData(); }, [loadData]); const handleCreate = async () => { if (!newUrl.trim() || newEvents.length === 0) { setError('URL and at least one event type are required'); return; } try { await createSubscription({ url: newUrl.trim(), events: newEvents, description: newDesc.trim() || undefined }); setShowCreate(false); setNewUrl(''); setNewEvents([]); setNewDesc(''); loadData(); } catch (e) { setError(e instanceof Error ? e.message : 'Failed to create'); } }; const handleDelete = async (id: string) => { try { await deleteSubscription(id); loadData(); } catch (e) { setError(e instanceof Error ? e.message : 'Failed to delete'); } }; const handleTest = async (id: string) => { try { await testWebhook(id); setError(null); } catch (e) { setError(e instanceof Error ? e.message : 'Test failed'); } }; const toggleEvent = (type: string) => { setNewEvents(prev => prev.includes(type) ? prev.filter(t => t !== type) : [...prev, type]); }; return (
{/* Header */}

Webhooks

Integrate ChronoMind with external services

{/* Error */} {error && (
{error}
)} {/* Create form */} {showCreate && (

New Webhook

setNewUrl(e.target.value)} placeholder="https://example.com/webhook" className="w-full px-3 py-2 rounded-lg text-sm mb-3" style={{ backgroundColor: 'var(--cm-surface-muted)', color: 'var(--cm-text-primary)', border: '1px solid var(--cm-border)' }} /> setNewDesc(e.target.value)} placeholder="Description (optional)" className="w-full px-3 py-2 rounded-lg text-sm mb-3" style={{ backgroundColor: 'var(--cm-surface-muted)', color: 'var(--cm-text-primary)', border: '1px solid var(--cm-border)' }} />

Events

{eventTypes.map(et => ( ))}
)} {/* Subscriptions list */} {loading ? (
) : subs.length === 0 ? (

No webhooks configured.

) : (
{subs.map(sub => (
{sub.url}
{sub.description && (

{sub.description}

)}
{sub.events.map(evt => ( {evt} ))}
{sub.consecutiveFailures > 0 ? `${sub.consecutiveFailures} failures` : 'Healthy'}
))}
)}
); }