Surface regenerate errors in clean-pdf Zotero section

regenerate() silently did nothing on {ok:false} or a rejected server
action, risking an unhandled promise rejection and leaving the student
staring at a stuck "меняем…" button with no feedback. Wrap the action
call in try/catch and show an inline error message on failure.
This commit is contained in:
2026-07-06 13:45:12 +05:00
parent 1ccf994112
commit 118fa3961f
@@ -8,6 +8,7 @@ export function ZoteroSection({ apiKey, baseUrl }: { apiKey: string; baseUrl: st
const [key, setKey] = useState(apiKey);
const [revealed, setRevealed] = useState(false);
const [pending, startTransition] = useTransition();
const [error, setError] = useState<string | null>(null);
const shownKey = revealed ? key : key.slice(0, 8) + "…" + key.slice(-4);
const curlExample = `curl -H "Authorization: Bearer ${key}" \\\n "${baseUrl}/api/pdf?url=https://example.com/article&format=A4&theme=light" \\\n -o article.pdf`;
@@ -15,9 +16,19 @@ export function ZoteroSection({ apiKey, baseUrl }: { apiKey: string; baseUrl: st
function regenerate() {
if (!confirm("Старый ключ перестанет работать (в том числе в Zotero). Продолжить?")) return;
setError(null);
startTransition(async () => {
try {
const res = await regeneratePdfApiKey();
if (res.ok && res.key) { setKey(res.key); setRevealed(true); }
if (res.ok && res.key) {
setKey(res.key);
setRevealed(true);
} else {
setError("Не удалось перевыпустить ключ, попробуйте ещё раз");
}
} catch {
setError("Не удалось перевыпустить ключ, попробуйте ещё раз");
}
});
}
@@ -40,6 +51,7 @@ export function ZoteroSection({ apiKey, baseUrl }: { apiKey: string; baseUrl: st
<p className="text-xs" style={{ color: "var(--muted-foreground)" }}>
Ключ персональный не публикуйте его. Если ключ утёк, перевыпустите: старый сразу отключится.
</p>
{error && <p className="text-sm" style={{ color: "var(--destructive)" }}>{error}</p>}
</div>
<div className="flex flex-col gap-2 text-sm" style={{ color: "var(--foreground)" }}>