import { useState } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { api } from "@/lib/api"; import { PlaidLinkSession } from "./PlaidLinkSession"; export function BankReconnectButton({ itemId, institution, onReconnected }: { itemId: string; institution: string; onReconnected: () => Promise }) { const [token, setToken] = useState(null); const [busy, setBusy] = useState(false); const start = async () => { setBusy(true); try { setToken((await api.plaid.createLinkToken(itemId)).link_token); } catch { toast.error("Couldn't start bank sign-in. Please try again."); } finally { setBusy(false); } }; const complete = async () => { setToken(null); setBusy(true); // Update mode repairs the existing Item; never exchange a new public token. try { await onReconnected(); toast.success(`${institution} reconnected and synced`); } catch (error) { toast.error(error instanceof Error ? error.message : "Bank sign-in completed, but sync needs another attempt."); } finally { setBusy(false); } }; return token ? { void complete(); }} onClose={() => setToken(null)} /> : ; }