Files
Mebbling/app/dashboard/webhook-control.tsx
T

22 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2026-07-19 00:29:30 +08:00
"use client";
import { useState } from "react";
2026-07-19 05:36:52 +08:00
export function WebhookControl({ sourceId, configured, automatic = false }: { sourceId: number; configured: boolean; automatic?: boolean }) {
2026-07-19 05:55:43 +08:00
const [url, setUrl] = useState(""); const [error, setError] = useState(""); const [notice, setNotice] = useState(""); const [busy, setBusy] = useState(false);
2026-07-19 00:29:30 +08:00
async function generate() {
2026-07-19 05:55:43 +08:00
setBusy(true); setError(""); setNotice("");
2026-07-19 00:29:30 +08:00
try {
const response = await fetch(`/api/sources/${sourceId}/webhook`, { method: "POST", headers: { Accept: "application/json" } });
2026-07-19 05:55:43 +08:00
const body = await response.json(); if (!response.ok) throw new Error(body.error || "無法產生 webhook URL"); if (body.rotated) setNotice("Webhook 已安全輪替。"); else setUrl(body.url);
2026-07-19 00:29:30 +08:00
} catch (reason) { setError(reason instanceof Error ? reason.message : "無法產生 webhook URL"); }
finally { setBusy(false); }
}
async function copy() { if (url) await navigator.clipboard.writeText(url); }
2026-07-19 05:55:43 +08:00
if (automatic) return <div className="webhook-control"><p className="meta">Webhook:已由 Memos 自動設定並驗證簽章。</p><button type="button" onClick={generate} disabled={busy}>{busy ? "輪替中…" : "安全輪替 Webhook"}</button>{notice && <p>{notice}</p>}{error && <p className="error">{error}</p>}</div>;
2026-07-19 00:29:30 +08:00
return <div className="webhook-control"><p className="meta">Webhook{configured ? "已設定" : "尚未設定"}</p>
{url ? <><label className="sr-only" htmlFor={`webhook-${sourceId}`}>Webhook URL</label><input id={`webhook-${sourceId}`} readOnly value={url} onFocus={(event) => event.currentTarget.select()} /><div className="row"><button type="button" onClick={copy}>複製 URL</button><button type="button" className="danger" onClick={generate} disabled={busy}>重新產生</button></div><p className="meta">請立即複製到 Memos;重新整理後完整密鑰不會再顯示。</p></> : <button type="button" onClick={generate} disabled={busy}>{busy ? "產生中…" : configured ? "重新產生 webhook URL" : "產生 webhook URL"}</button>}
{error && <p className="error">{error}</p>}
</div>;
}