'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; // Tiny client component for the Disconnect action. Confirms, posts to // /api/admin/pinterest/disconnect, then refreshes the parent server // component so the status rows re-render. Disabled while the call is // in flight. export function DisconnectButton({ legacyEnvSet }: { legacyEnvSet: boolean }) { const router = useRouter(); const [busy, setBusy] = useState(false); const [err, setErr] = useState(null); const onClick = async () => { const warn = legacyEnvSet ? 'Disconnect Pinterest? This clears the saved refresh token from the database.\n\nHeads up: PINTEREST_REFRESH_TOKEN is also set as an env var — to fully disconnect, also remove it from .env.local and your Vercel envs.' : 'Disconnect Pinterest? This clears the saved refresh token from the database. You can reconnect anytime.'; if (!confirm(warn)) return; setBusy(true); setErr(null); try { const res = await fetch('/api/admin/pinterest/disconnect', { method: 'POST' }); const j = (await res.json().catch(() => ({}))) as { ok?: boolean; error?: string }; if (!res.ok || !j.ok) throw new Error(j.error || `HTTP ${res.status}`); router.refresh(); } catch (e) { setErr(e instanceof Error ? e.message : 'disconnect failed'); } finally { setBusy(false); } }; return (
{err && (
{err}
)}
); }