|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState, useTransition } from "react"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { createIntegration, revokeIntegration } from "@/app/actions/integrations"; |
| 6 | + |
| 7 | +const KIND_LABELS = { crawlproof: "Crawlproof", outrank: "Outrank" }; |
| 8 | + |
| 9 | +export function IntegrationsManager({ initial }) { |
| 10 | + const router = useRouter(); |
| 11 | + const [pending, start] = useTransition(); |
| 12 | + const [items, setItems] = useState(initial); |
| 13 | + const [kind, setKind] = useState("crawlproof"); |
| 14 | + const [name, setName] = useState("Crawlproof"); |
| 15 | + const [origin, setOrigin] = useState(""); |
| 16 | + const [revealed, setRevealed] = useState({}); |
| 17 | + const [copied, setCopied] = useState(null); |
| 18 | + const [error, setError] = useState(null); |
| 19 | + const [justCreatedToken, setJustCreatedToken] = useState(null); |
| 20 | + |
| 21 | + useEffect(() => { setOrigin(window.location.origin); }, []); |
| 22 | + |
| 23 | + const onCreate = (e) => { |
| 24 | + e.preventDefault(); |
| 25 | + setError(null); |
| 26 | + setJustCreatedToken(null); |
| 27 | + start(async () => { |
| 28 | + const res = await createIntegration({ name, kind }); |
| 29 | + if (!res.ok) { setError(res.error); return; } |
| 30 | + setJustCreatedToken(res.accessToken); |
| 31 | + setName(kind === "crawlproof" ? "Crawlproof" : "Outrank"); |
| 32 | + router.refresh(); |
| 33 | + }); |
| 34 | + }; |
| 35 | + |
| 36 | + const onRevoke = (it) => { |
| 37 | + if (!confirm(`Revoke "${it.name}"? The source will stop being able to publish.`)) return; |
| 38 | + start(async () => { |
| 39 | + const res = await revokeIntegration({ id: it.id }); |
| 40 | + if (!res.ok) { setError(res.error); return; } |
| 41 | + setItems((prev) => prev.filter((i) => i.id !== it.id)); |
| 42 | + router.refresh(); |
| 43 | + }); |
| 44 | + }; |
| 45 | + |
| 46 | + const copy = (key, text) => { |
| 47 | + navigator.clipboard?.writeText(text); |
| 48 | + setCopied(key); |
| 49 | + setTimeout(() => setCopied(null), 1500); |
| 50 | + }; |
| 51 | + |
| 52 | + const webhookUrl = `${origin}/api/autoblog`; |
| 53 | + const mutedColor = "var(--color-text-secondary)"; |
| 54 | + const borderColor = "var(--color-border)"; |
| 55 | + |
| 56 | + return ( |
| 57 | + <div style={{ display: "flex", flexDirection: "column", gap: "1.5rem" }}> |
| 58 | + <div> |
| 59 | + <p style={{ fontSize: "0.7rem", textTransform: "uppercase", letterSpacing: "0.05em", color: mutedColor, marginBottom: "0.5rem" }}> |
| 60 | + Webhook endpoint |
| 61 | + </p> |
| 62 | + <div style={{ display: "flex", gap: "0.5rem" }}> |
| 63 | + <code style={{ flex: 1, wordBreak: "break-all", borderRadius: "0.375rem", border: `1px solid ${borderColor}`, background: "var(--color-bg-secondary)", padding: "0.5rem 0.75rem", fontSize: "0.75rem" }}> |
| 64 | + {webhookUrl || "https://qrypt.chat/api/autoblog"} |
| 65 | + </code> |
| 66 | + <button type="button" onClick={() => copy("url", webhookUrl)} |
| 67 | + style={{ padding: "0.5rem 0.75rem", fontSize: "0.875rem", border: `1px solid ${borderColor}`, borderRadius: "0.375rem", cursor: "pointer" }}> |
| 68 | + {copied === "url" ? "Copied" : "Copy"} |
| 69 | + </button> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + |
| 73 | + <form onSubmit={onCreate}> |
| 74 | + <p style={{ fontSize: "0.7rem", textTransform: "uppercase", letterSpacing: "0.05em", color: mutedColor, marginBottom: "0.5rem" }}> |
| 75 | + Generate token |
| 76 | + </p> |
| 77 | + <div style={{ display: "flex", gap: "0.5rem", flexWrap: "wrap" }}> |
| 78 | + <select value={kind} onChange={(e) => { setKind(e.target.value); setName(e.target.value === "crawlproof" ? "Crawlproof" : "Outrank"); }} |
| 79 | + style={{ padding: "0.5rem 0.75rem", fontSize: "0.875rem", border: `1px solid ${borderColor}`, borderRadius: "0.375rem", background: "var(--color-bg)" }}> |
| 80 | + <option value="crawlproof">Crawlproof</option> |
| 81 | + <option value="outrank">Outrank</option> |
| 82 | + </select> |
| 83 | + <input style={{ flex: 1, minWidth: "160px", padding: "0.5rem 0.75rem", fontSize: "0.875rem", border: `1px solid ${borderColor}`, borderRadius: "0.375rem", background: "var(--color-bg)" }} |
| 84 | + type="text" value={name} onChange={(e) => setName(e.target.value)} |
| 85 | + placeholder="Integration name" maxLength={100} required /> |
| 86 | + <button type="submit" disabled={pending} |
| 87 | + style={{ padding: "0.5rem 1rem", fontSize: "0.875rem", fontWeight: 500, background: "var(--color-accent)", color: "#fff", border: "none", borderRadius: "0.375rem", cursor: "pointer", opacity: pending ? 0.5 : 1 }}> |
| 88 | + {pending ? "…" : "Generate"} |
| 89 | + </button> |
| 90 | + </div> |
| 91 | + {error && <p style={{ marginTop: "0.5rem", fontSize: "0.875rem", color: "#ef4444" }}>{error}</p>} |
| 92 | + {justCreatedToken && ( |
| 93 | + <div style={{ marginTop: "0.75rem", borderRadius: "0.375rem", border: "1px solid rgba(34,197,94,0.4)", background: "rgba(34,197,94,0.05)", padding: "0.75rem" }}> |
| 94 | + <p style={{ marginBottom: "0.5rem", fontSize: "0.75rem", fontWeight: 600, color: "#16a34a" }}> |
| 95 | + Token created — copy now and paste into CrawlProof autoblog webhook settings. |
| 96 | + </p> |
| 97 | + <div style={{ display: "flex", gap: "0.5rem", alignItems: "center" }}> |
| 98 | + <code style={{ flex: 1, wordBreak: "break-all", fontSize: "0.75rem" }}>{justCreatedToken}</code> |
| 99 | + <button type="button" onClick={() => copy("new", justCreatedToken)} |
| 100 | + style={{ padding: "0.25rem 0.5rem", border: `1px solid ${borderColor}`, borderRadius: "0.25rem", fontSize: "0.75rem", cursor: "pointer" }}> |
| 101 | + {copied === "new" ? "Copied" : "Copy"} |
| 102 | + </button> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + )} |
| 106 | + </form> |
| 107 | + |
| 108 | + <div> |
| 109 | + <p style={{ fontSize: "0.7rem", textTransform: "uppercase", letterSpacing: "0.05em", color: mutedColor, marginBottom: "0.5rem" }}> |
| 110 | + Access tokens ({items.length}) |
| 111 | + </p> |
| 112 | + {items.length === 0 ? ( |
| 113 | + <p style={{ fontSize: "0.875rem", color: mutedColor }}>None yet — generate one above.</p> |
| 114 | + ) : ( |
| 115 | + <ul style={{ listStyle: "none", padding: 0, display: "flex", flexDirection: "column", gap: "0.5rem" }}> |
| 116 | + {items.map((it) => { |
| 117 | + const show = !!revealed[it.id]; |
| 118 | + const masked = `${it.access_token.slice(0, 8)}…${it.access_token.slice(-4)}`; |
| 119 | + return ( |
| 120 | + <li key={it.id} style={{ borderRadius: "0.375rem", border: `1px solid ${borderColor}`, padding: "0.75rem", fontSize: "0.875rem" }}> |
| 121 | + <div style={{ display: "flex", justifyContent: "space-between", gap: "0.75rem", alignItems: "flex-start" }}> |
| 122 | + <div style={{ flex: 1, minWidth: 0 }}> |
| 123 | + <div style={{ display: "flex", flexWrap: "wrap", alignItems: "baseline", gap: "0.5rem" }}> |
| 124 | + <span style={{ fontWeight: 500 }}>{it.name}</span> |
| 125 | + <span style={{ fontSize: "0.7rem", padding: "0.1rem 0.4rem", borderRadius: "0.25rem", background: "var(--color-bg-secondary)" }}> |
| 126 | + {KIND_LABELS[it.kind] ?? it.kind} |
| 127 | + </span> |
| 128 | + <span style={{ fontSize: "0.75rem", color: mutedColor }}> |
| 129 | + {it.request_count} requests |
| 130 | + {it.last_used_at && ` · last ${new Date(it.last_used_at).toLocaleString()}`} |
| 131 | + </span> |
| 132 | + </div> |
| 133 | + <div style={{ marginTop: "0.5rem", display: "flex", alignItems: "center", gap: "0.5rem" }}> |
| 134 | + <code style={{ flex: 1, wordBreak: "break-all", borderRadius: "0.25rem", border: `1px solid ${borderColor}`, background: "var(--color-bg-secondary)", padding: "0.25rem 0.5rem", fontSize: "0.75rem" }}> |
| 135 | + {show ? it.access_token : masked} |
| 136 | + </code> |
| 137 | + <button type="button" onClick={() => setRevealed((p) => ({ ...p, [it.id]: !p[it.id] }))} |
| 138 | + style={{ fontSize: "0.75rem", color: mutedColor, background: "none", border: "none", cursor: "pointer" }}> |
| 139 | + {show ? "Hide" : "Reveal"} |
| 140 | + </button> |
| 141 | + <button type="button" onClick={() => copy(it.id, it.access_token)} |
| 142 | + style={{ fontSize: "0.75rem", color: mutedColor, background: "none", border: "none", cursor: "pointer" }}> |
| 143 | + {copied === it.id ? "Copied" : "Copy"} |
| 144 | + </button> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + <button type="button" onClick={() => onRevoke(it)} disabled={pending} |
| 148 | + style={{ fontSize: "0.75rem", color: "#ef4444", background: "none", border: "none", cursor: "pointer" }}> |
| 149 | + Revoke |
| 150 | + </button> |
| 151 | + </div> |
| 152 | + </li> |
| 153 | + ); |
| 154 | + })} |
| 155 | + </ul> |
| 156 | + )} |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + ); |
| 160 | +} |
0 commit comments