Lumen & Gesso / website · lumengesso.com / app / admin / pinterest / DisconnectButton.tsx
| 1 | 'use client'; |
| 2 | |
| 3 | import { useState } from 'react'; |
| 4 | import { useRouter } from 'next/navigation'; |
| 5 | |
| 6 | // Tiny client component for the Disconnect action. Confirms, posts to |
| 7 | // /api/admin/pinterest/disconnect, then refreshes the parent server |
| 8 | // component so the status rows re-render. Disabled while the call is |
| 9 | // in flight. |
| 10 | export function DisconnectButton({ legacyEnvSet }: { legacyEnvSet: boolean }) { |
| 11 | const router = useRouter(); |
| 12 | const [busy, setBusy] = useState(false); |
| 13 | const [err, setErr] = useState<string | null>(null); |
| 14 | |
| 15 | const onClick = async () => { |
| 16 | const warn = legacyEnvSet |
| 17 | ? '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.' |
| 18 | : 'Disconnect Pinterest? This clears the saved refresh token from the database. You can reconnect anytime.'; |
| 19 | if (!confirm(warn)) return; |
| 20 | setBusy(true); setErr(null); |
| 21 | try { |
| 22 | const res = await fetch('/api/admin/pinterest/disconnect', { method: 'POST' }); |
| 23 | const j = (await res.json().catch(() => ({}))) as { ok?: boolean; error?: string }; |
| 24 | if (!res.ok || !j.ok) throw new Error(j.error || `HTTP ${res.status}`); |
| 25 | router.refresh(); |
| 26 | } catch (e) { |
| 27 | setErr(e instanceof Error ? e.message : 'disconnect failed'); |
| 28 | } finally { |
| 29 | setBusy(false); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | return ( |
| 34 | <div style={{ marginTop: 12 }}> |
| 35 | <button |
| 36 | type="button" |
| 37 | onClick={onClick} |
| 38 | disabled={busy} |
| 39 | style={{ |
| 40 | padding: '8px 14px', |
| 41 | borderRadius: 8, |
| 42 | border: '1px solid var(--rose)', |
| 43 | background: 'transparent', |
| 44 | color: 'var(--rose-deep)', |
| 45 | cursor: busy ? 'wait' : 'pointer', |
| 46 | fontSize: 13, |
| 47 | fontWeight: 500, |
| 48 | }} |
| 49 | > |
| 50 | {busy ? 'Disconnecting…' : 'Disconnect Pinterest'} |
| 51 | </button> |
| 52 | {err && ( |
| 53 | <div role="alert" style={{ marginTop: 8, color: 'var(--rose-deep)', fontSize: 12 }}> |
| 54 | {err} |
| 55 | </div> |
| 56 | )} |
| 57 | </div> |
| 58 | ); |
| 59 | } |