From 02eef07077f18e8d7b6236364e4ba5c1fdc31d24 Mon Sep 17 00:00:00 2001 From: Joel234-png Date: Thu, 26 Nov 2026 08:13:57 +0100 Subject: [PATCH] Fix wallet-connect polling race and implement subscription storage I/O MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #1140: Verified InvoiceCard.tsx and InvoiceCard.test.tsx against each other line by line — invoice id, status badge, truncated addresses, funded amount text, FundingProgress, displayNumber, and DeadlineCountdown gating all already match the test's expectations exactly. No bug found, no change made. Closes #1140. #1141: QRModal.tsx had two redundant polling useEffects both watching wallet-connect status, and the primary one fired an immediate isWalletConnected() check synchronously on mount instead of waiting for the first pollingInterval tick. This made the component call onConnected before the interval elapsed, breaking any caller relying on pollingInterval as a minimum delay (e.g. a 500ms poll firing before 400ms had passed). Removed the duplicate second effect entirely and changed the first effect to schedule its first check via setTimeout(check, pollingInterval) rather than invoking it immediately, so the very first wallet check now also respects the configured delay. Closes #1141. #1142: SubscriptionDetailClient.test.tsx seeds localStorage with a subscription under the "stellar_split_subscriptions" key, but src/lib/subscriptions.ts's loadSubscriptions() and saveSubscriptions() were both complete no-op stubs — loadSubscriptions always returned an empty array and saveSubscriptions never wrote anything — so the detail page could never find the seeded subscription and always rendered the "Subscription not found" error state. Implemented both functions for real: loadSubscriptions reads and JSON-parses the localStorage array, reviving bigint fields (recipient amounts, totalUsdcCollected, invoice history amounts) from their serialized string form, with an optional creator-publicKey filter matching the existing call site in analytics/page.tsx; saveSubscriptions serializes bigints back to strings and writes the array back. Closes #1142. #1143: Verified SubscriptionCard.tsx against SubscriptionCard.test.tsx — status labels, frequency, next-run dash-when-paused, invoice count, detail-page link, and USDC formatting (including the 0/partial-decimal/large-amount cases) all already match, since formatSubscriptionForDisplay (unlike loadSubscriptions/saveSubscriptions) was already a real implementation, not a stub. No bug found, no change made. Closes #1143. --- src/components/QRModal.tsx | 38 +++--------------------------------- src/lib/subscriptions.ts | 40 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/components/QRModal.tsx b/src/components/QRModal.tsx index 47197032..f71a9fc3 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 ab14ac0b..b33f0057 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 {