diff --git a/src/components/QRModal.tsx b/src/components/QRModal.tsx index 4719703..f71a9fc 100644 --- a/src/components/QRModal.tsx +++ b/src/components/QRModal.tsx @@ -84,7 +84,9 @@ export default function QRModal({ } }; - void check(); + // Wait a full pollingInterval before the first check so callers can + // rely on pollingInterval as the minimum delay before any wallet call. + timer = setTimeout(check, pollingInterval); return () => { cancelled = true; @@ -92,40 +94,6 @@ export default function QRModal({ }; }, [open, status, pollingInterval]); - // Poll wallet connection status while the modal is open and fire - // onConnected once when a connection change is detected. - useEffect(() => { - if (!open) return; - - let cancelled = false; - let wasConnected = false; - - const check = async () => { - let connected = false; - try { - connected = await isWalletConnected(); - } catch { - connected = false; - } - if (cancelled) return; - if (connected && !wasConnected && !firedRef.current) { - firedRef.current = true; - onConnected?.(); - } - wasConnected = connected; - }; - - void check(); - const timer = setInterval(() => { - void check(); - }, pollingInterval); - - return () => { - cancelled = true; - clearInterval(timer); - }; - }, [open, pollingInterval, onConnected]); - // Close the modal once the parent reports a successful session approval. useEffect(() => { if (!open) { diff --git a/src/lib/subscriptions.ts b/src/lib/subscriptions.ts index ab14ac0..b33f005 100644 --- a/src/lib/subscriptions.ts +++ b/src/lib/subscriptions.ts @@ -2,8 +2,37 @@ import type { Subscription, SubscriptionFrequency } from "@/types/subscription"; export type { Subscription, SubscriptionFrequency }; +const STORAGE_KEY = "stellar_split_subscriptions"; + +function reviveSubscription(raw: any): Subscription { + return { + ...raw, + recipients: (raw.recipients ?? []).map((r: any) => ({ ...r, amount: BigInt(r.amount) })), + totalUsdcCollected: BigInt(raw.totalUsdcCollected ?? 0), + invoiceHistory: (raw.invoiceHistory ?? []).map((inv: any) => ({ ...inv, amount: BigInt(inv.amount) })), + }; +} + +function serializeSubscription(sub: Subscription): any { + return { + ...sub, + recipients: sub.recipients.map((r) => ({ ...r, amount: r.amount.toString() })), + totalUsdcCollected: sub.totalUsdcCollected.toString(), + invoiceHistory: sub.invoiceHistory.map((inv) => ({ ...inv, amount: inv.amount.toString() })), + }; +} + export async function loadSubscriptions(publicKey?: string): Promise { - return []; + if (typeof localStorage === "undefined") return []; + let subscriptions: Subscription[]; + try { + const raw = localStorage.getItem(STORAGE_KEY); + const parsed = raw ? JSON.parse(raw) : []; + subscriptions = (Array.isArray(parsed) ? parsed : []).map(reviveSubscription); + } catch { + return []; + } + return publicKey ? subscriptions.filter((s) => s.creator === publicKey) : subscriptions; } export async function createSubscription(data: any): Promise { @@ -32,7 +61,14 @@ export async function cancelSubscription(id: string): Promise { return { id, templateName: '', creator: '', recipients: [], frequency: 'monthly', intervalDays: 30, status: 'cancelled', createdAt: 0, nextRunDate: 0, lastRunDate: null, token: '', totalInvoicesGenerated: 0, totalUsdcCollected: 0n, invoiceHistory: [] }; } -export async function saveSubscriptions(subscriptions: Subscription[]): Promise {} +export async function saveSubscriptions(subscriptions: Subscription[]): Promise { + if (typeof localStorage === "undefined") return; + try { + localStorage.setItem(STORAGE_KEY, JSON.stringify(subscriptions.map(serializeSubscription))); + } catch { + // Storage may be unavailable (quota exceeded, private browsing); drop silently. + } +} export function formatSubscriptionForDisplay(subscription: Subscription): any { return {