"use client"; import { useCallback, useEffect, useState } from "react"; import { Loader2, PlugZap } from "lucide-react"; import { AccountConnectionCard } from "@/components/accounts/account-connection-card"; import { DeleteAccountCard } from "@/components/accounts/delete-account-card"; import { ExtensionSyncCard } from "@/components/settings/extension-sync-card"; import { ThreadsConnectionSettings, type ThreadsConnectionSettingsData, } from "@/components/settings/threads-connection-settings"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { EmptyState } from "@/components/layout/empty-state"; import { PageHeader } from "@/components/layout/page-header"; import { notify } from "@/lib/notifications/store"; import { parseFetchJson } from "@/lib/utils"; interface ConnectionPageData { accountId: string | null; accountName: string | null; connection: ThreadsConnectionSettingsData | null; } export default function ConnectionsPage() { const [data, setData] = useState(null); const [draft, setDraft] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const load = useCallback(async () => { setLoading(true); try { const res = await fetch("/api/accounts/connection"); const json = await parseFetchJson(res); if (!res.ok) { throw new Error(json.error ?? "無法載入連線設定"); } setData(json); setDraft(json.connection ?? null); } catch (error) { setData(null); setDraft(null); notify({ type: "error", title: "無法載入連線設定", message: error instanceof Error ? error.message : "請稍後再試", }); } finally { setLoading(false); } }, []); useEffect(() => { load(); }, [load]); function patchConnection(patch: Partial) { setDraft((prev) => (prev ? { ...prev, ...patch } : prev)); } async function handleSave() { if (!draft || !data?.accountId) return; setSaving(true); try { const res = await fetch("/api/accounts/connection", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(draft), }); const json = await parseFetchJson<{ connection?: ThreadsConnectionSettingsData; error?: string; }>(res); if (!res.ok) { throw new Error(json.error ?? "儲存失敗"); } setData((prev) => prev ? { ...prev, connection: json.connection ?? null, } : prev ); setDraft(json.connection ?? null); notify({ type: "success", title: "連線設定已儲存" }); } catch (error) { notify({ type: "error", title: "儲存失敗", message: error instanceof Error ? error.message : "請稍後再試", }); } finally { setSaving(false); } } if (loading) { return (
); } if (!data) { return (
重新載入} />
); } if (!data.accountId || !draft) { return (
); } return (
{saving ? : } 儲存連線設定 } />
目前帳號 {data.accountName}
); }