"use client"; import { useState } from "react"; type ApiResponse = { data: T; meta: { timestamp: string; version: "v1" }; error: null | { message: string; code?: string }; }; type LinkTokenData = { linkToken?: string }; type ExportData = { status?: string; url?: string; csv?: string }; async function postJson(path: string) { const res = await fetch(path, { method: "POST" }); if (!res.ok) { throw new Error("Request failed"); } const payload = (await res.json()) as ApiResponse; return payload.data; } async function getJson(path: string) { const res = await fetch(path); if (!res.ok) { throw new Error("Request failed"); } const payload = (await res.json()) as ApiResponse; return payload.data; } export function HeroActions() { const [status, setStatus] = useState(""); const onStart = async () => { setStatus("Requesting a secure link token..."); try { const data = await postJson("/api/accounts/link"); if (data.linkToken) { setStatus(`Link token ready: ${data.linkToken}`); } else { setStatus("Link token requested."); } } catch { setStatus("Unable to request link token."); } }; const onViewExport = async () => { setStatus("Preparing export sample..."); try { const userId = localStorage.getItem("ledgerone_user_id"); const query = userId ? `?user_id=${encodeURIComponent(userId)}` : ""; const data = await getJson(`/api/exports/csv${query}`); if (data.csv) { const blob = new Blob([data.csv], { type: "text/csv" }); const url = URL.createObjectURL(blob); window.open(url, "_blank", "noopener,noreferrer"); setStatus("Export sample opened."); } else { setStatus("Export sample ready."); } } catch { setStatus("Unable to fetch export sample."); } }; return (
{status ?

{status}

: null}
); }