import { useState, useEffect } from 'react'; import { toast } from 'sonner'; import { Loader2 } from 'lucide-react'; import { api } from '@/api'; import { errMessage } from '@/lib/utils'; import { Button } from '@/components/ui/button'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from '@/components/ui/alert-dialog'; import { SectionCard, type SectionCardProps } from './dataShared'; export default function SeedDemoDataSection({ onSeeded, cardProps = {} }: { onSeeded?: () => void; cardProps?: Partial; }) { const [loading, setLoading] = useState(false); const [seeded, setSeeded] = useState(false); const [counts, setCounts] = useState<{ bills: number; categories: number; payments: number; transactions: number }>( { bills: 0, categories: 0, payments: 0, transactions: 0 } ); const [clearing, setClearing] = useState(false); const [showClearConfirm, setShowClearConfirm] = useState(false); const [statusLoading, setStatusLoading] = useState(true); useEffect(() => { api.seededStatus() .then(raw => { const data = raw as { seeded?: boolean; seededBills?: number; seededCategories?: number; seededPayments?: number; seededTransactions?: number }; setSeeded(!!data.seeded); if (data.seeded) setCounts({ bills: data.seededBills || 0, categories: data.seededCategories || 0, payments: data.seededPayments || 0, transactions: data.seededTransactions || 0, }); }) .catch(err => console.error('Failed to check seeded status:', err)) .finally(() => setStatusLoading(false)); }, []); const handleSeed = async () => { setLoading(true); try { const data = await api.seedDemoData() as { billsCreated?: number; categoriesCreated?: number; paymentsCreated?: number; transactionsCreated?: number } | null; if (!data || typeof data !== 'object') throw new Error('Invalid response from server'); setCounts({ bills: data.billsCreated || 0, categories: data.categoriesCreated || 0, payments: data.paymentsCreated || 0, transactions: data.transactionsCreated || 0, }); setSeeded(true); toast.success(`Seeded ${data.billsCreated || 0} bills with ${data.paymentsCreated || 0} payments and ${data.transactionsCreated || 0} transactions.`); setTimeout(() => onSeeded?.(), 100); } catch (err) { console.error('Seed error:', err); toast.error(errMessage(err, 'Failed to seed demo data.')); } finally { setLoading(false); } }; const handleClearDemoData = async () => { setClearing(true); try { const data = await api.clearDemoData() as { removed?: number }; setSeeded(false); setCounts({ bills: 0, categories: 0, payments: 0, transactions: 0 }); setShowClearConfirm(false); toast.success(`Removed all demo data (${data.removed || 0} records). Your own data is untouched.`); onSeeded?.(); } catch (err) { toast.error(errMessage(err, 'Failed to clear demo data.')); } finally { setClearing(false); } }; return (
{statusLoading ? (

Loading…

) : seeded ? ( <>

Demo data seeded

Bills

{counts.bills}

Payments

{counts.payments}

Transactions

{counts.transactions}

Categories

{counts.categories}

) : (

Populate every page with a realistic dataset — bills, months of payment history, bank transactions, budgets, a debt snowball, and more — so you can explore the whole app. It's tied to your account and fully removable; your own data is never touched.

)}
Clear Demo Data This removes all seeded demo data — {counts.bills} bills, {counts.payments} payments, {' '}{counts.transactions} transactions and related records. Your own bills and categories are kept. This cannot be undone. Cancel {clearing ? <>Clearing… : 'Clear Data'}
); }