From 48f4c2c0155e893d48891517eec00e2fe282040d Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 9 Jun 2026 13:14:41 +0100 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/automations.ts | 10 ++ .../automations/AutomationsPanel.svelte | 90 ++++++++++++++++- cptr/routers/automations.py | 97 +++++++++++++++++-- cptr/utils/automations.py | 8 +- 4 files changed, 195 insertions(+), 10 deletions(-) diff --git a/cptr/frontend/src/lib/apis/automations.ts b/cptr/frontend/src/lib/apis/automations.ts index 2b6f8c9..4b71a1a 100644 --- a/cptr/frontend/src/lib/apis/automations.ts +++ b/cptr/frontend/src/lib/apis/automations.ts @@ -16,6 +16,8 @@ export type AutomationData = { updated_at: number; last_run: AutomationRunData | null; next_runs: number[] | null; + has_webhook: boolean; + webhook_url: string | null; }; export type AutomationRunData = { @@ -81,3 +83,11 @@ export async function getAutomationRuns( ): Promise { return fetchJSON(`/api/automations/${id}/runs?skip=${skip}&limit=${limit}`); } + +export async function generateWebhook(id: string): Promise { + return fetchJSON(`/api/automations/${id}/webhook`, { method: 'POST' }); +} + +export async function deleteWebhook(id: string): Promise { + return fetchJSON(`/api/automations/${id}/webhook`, { method: 'DELETE' }); +} diff --git a/cptr/frontend/src/lib/components/automations/AutomationsPanel.svelte b/cptr/frontend/src/lib/components/automations/AutomationsPanel.svelte index b636a40..26cbe19 100644 --- a/cptr/frontend/src/lib/components/automations/AutomationsPanel.svelte +++ b/cptr/frontend/src/lib/components/automations/AutomationsPanel.svelte @@ -16,6 +16,8 @@ runAutomation, getAutomationById, getAutomationRuns, + generateWebhook, + deleteWebhook, type AutomationData, type AutomationRunData } from '$lib/apis/automations'; @@ -139,6 +141,47 @@ const freq = match[1].toLowerCase(); return freq.charAt(0).toUpperCase() + freq.slice(1); } + + // ── Webhook ── + let webhookLoading = $state(false); + let webhookCopied = $state(false); + + async function handleGenerateWebhook() { + if (!detail) return; + webhookLoading = true; + try { + const updated = await generateWebhook(detail.id); + detail = updated; + items = items.map((i) => (i.id === updated.id ? updated : i)); + toast.success('Webhook enabled'); + } catch (e: any) { + toast.error(e.message || 'Failed to generate webhook'); + } finally { + webhookLoading = false; + } + } + + async function handleRevokeWebhook() { + if (!detail) return; + webhookLoading = true; + try { + const updated = await deleteWebhook(detail.id); + detail = updated; + items = items.map((i) => (i.id === updated.id ? updated : i)); + toast.success('Webhook disabled'); + } catch (e: any) { + toast.error(e.message || 'Failed to revoke webhook'); + } finally { + webhookLoading = false; + } + } + + function copyWebhookUrl() { + if (!detail?.webhook_url) return; + navigator.clipboard.writeText(detail.webhook_url); + webhookCopied = true; + setTimeout(() => (webhookCopied = false), 2000); + }
@@ -150,7 +193,7 @@
{:else if detail} -
+
{#if !$sidebarOpen}
+
+ Webhook + {#if detail.webhook_url} +
+ ...?token={detail.webhook_url.split('token=')[1]?.slice(0, 12)}... + +
+ {:else if detail.has_webhook} + +
+ Enabled + · + + · + +
+ {:else} + + {/if} +
@@ -269,7 +355,7 @@ {:else} -
+
{#if !$sidebarOpen}