Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions src/components/QRModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,48 +84,16 @@ 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;
if (timer) clearTimeout(timer);
};
}, [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) {
Expand Down
40 changes: 38 additions & 2 deletions src/lib/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Subscription[]> {
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<Subscription> {
Expand Down Expand Up @@ -32,7 +61,14 @@ export async function cancelSubscription(id: string): Promise<Subscription> {
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<void> {}
export async function saveSubscriptions(subscriptions: Subscription[]): Promise<void> {
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 {
Expand Down