import React, { Suspense } from 'react'
import { Link } from 'react-router-dom'
import { authClient } from '../lib/auth-client.ts'
import { usePurchases, usePriceAlerts, usePriceHistory } from '../hooks/useApi.ts'
import { StoreIcon } from '../components/StoreIcon.tsx'
const LazySparklineCard = React.lazy(() =>
import('../components/SparklineChart.tsx').then((mod) => ({ default: mod.SparklineCard }))
)
export function Dashboard() {
const { data: session, isPending } = authClient.useSession()
if (isPending) {
return
}
if (!session) {
return (
CartSnitch
Track prices. Save money.
Sign In
Create Account
)
}
return
}
function AuthenticatedDashboard({ userName }: { userName: string }) {
const { data: purchases = [], isLoading: purchasesLoading } = usePurchases()
const { data: alerts = [], isLoading: alertsLoading } = usePriceAlerts()
const { data: eggHistory = [] } = usePriceHistory('prod10')
const { data: milkHistory = [] } = usePriceHistory('prod1')
const triggeredAlerts = alerts.filter((a) => a.triggered)
const watchingAlerts = alerts.filter((a) => !a.triggered)
const recentPurchases = purchases.slice(0, 3)
const sparklineData = eggHistory.filter((p) => p.storeId === 'meijer').slice(-8)
const milkSparkline = milkHistory.filter((p) => p.storeId === 'kroger').slice(-8)
const eggCurrent = sparklineData.length > 0 ? `$${sparklineData[sparklineData.length - 1].price.toFixed(2)}` : '—'
const milkCurrent = milkSparkline.length > 0 ? `$${milkSparkline[milkSparkline.length - 1].price.toFixed(2)}` : '—'
if (purchasesLoading || alertsLoading) {
return
}
return (
Hi, {userName.split(' ')[0]}
{/* Triggered alerts banner */}
{triggeredAlerts.length > 0 && (
✓
{triggeredAlerts.length} price {triggeredAlerts.length === 1 ? 'alert' : 'alerts'} triggered!
{triggeredAlerts.map((a) => a.productName).join(', ')}
)}
{/* Quick stats */}
Watching
{watchingAlerts.length}
price alerts
This Month
${recentPurchases.reduce((sum, p) => sum + p.total, 0).toFixed(0)}
grocery spend
{/* Price trend sparklines */}
{/* Recent purchases */}
Recent Purchases
View all
{recentPurchases.map((purchase) => (
{purchase.storeName}
{new Date(purchase.date).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
})}{' '}
· {purchase.items.length} items
${purchase.total.toFixed(2)}
))}
{/* Quick actions */}
Quick Actions
Compare Prices
Link a Store
)
}
function DashboardSkeleton() {
return (
)
}
function SparklinePlaceholder() {
return (
)
}