import { Link } from '@inertiajs/react'; import { AlertTriangle, CheckCircle2, Circle, CreditCard, PackageX, Store as StoreIcon, Truck, UploadCloud, } from 'lucide-react'; import type { ReactNode } from 'react'; import { useEffect, useState } from 'react'; import OrderController from '@/actions/App/Http/Controllers/OrderController'; import ProductController from '@/actions/App/Http/Controllers/ProductController'; import StoreController from '@/actions/App/Http/Controllers/StoreController'; import { actionHref, ringgit, when } from '@/components/overview/format'; import { cn } from '@/lib/utils'; import { create } from '@/routes/stores'; import type { AttentionItem, GettingStarted, OverviewStats } from '@/types'; /** A white box with a heading, used by every section below the big numbers. */ export function Panel({ title, aside, delay, className, children, }: { title: string; aside?: ReactNode; delay: number; className?: string; children: ReactNode; }) { return (

{title}

{aside && (
{aside}
)}
{children}
); } const tones = { warning: 'bg-(--ov-kunyit-soft) text-(--ov-kunyit-ink)', danger: 'bg-(--ov-bunga-soft) text-(--ov-bunga-ink)', brand: 'bg-(--ov-pandan-soft) text-(--ov-pandan-ink)', }; const attentionIcon = (item: AttentionItem) => ({ ship: Truck, restock: item.title.includes('out of stock') ? PackageX : AlertTriangle, publish: UploadCloud, plan: CreditCard, })[item.kind]; const attentionButton = { ship: 'Ship', restock: 'Restock', publish: 'Open', plan: 'Choose plan', }; /** Everything that needs the merchant now, in one list, each with its own button. */ export function AttentionList({ items, showStore, }: { items: AttentionItem[]; showStore: boolean; }) { if (items.length === 0) { return (
); } return ( ); } /** Bars that grow from zero after the page shows. */ function useGrow(): boolean { const [grown, setGrown] = useState(false); useEffect(() => { const frame = requestAnimationFrame(() => requestAnimationFrame(() => setGrown(true)), ); return () => cancelAnimationFrame(frame); }, []); return grown; } const storeColours = [ 'var(--ov-pandan)', 'var(--ov-kunyit)', 'var(--ov-bunga)', '#4C8C9E', '#8A6FB8', ]; /** Each store's share of sales in the range. Stores not published yet sit at the bottom, faded. */ export function StoreSales({ stats }: { stats: OverviewStats }) { const grown = useGrow(); const max = Math.max(...stats.by_store.map((store) => store.sales), 0); const anySelected = stats.by_store.some((store) => store.selected); // The best seller gets green, the next yellow, then red, whatever order the stores are in. const rank = [...stats.by_store] .sort((a, b) => b.sales - a.sales) .map((store) => store.slug); return (
{stats.by_store.map((store) => (
{store.name} {!store.is_published && store.sales === 0 ? 'Not published yet' : `${ringgit(store.sales)} · ${store.share}%`}
0 ? `${(store.sales / max) * 100}%` : '0%', background: storeColours[ rank.indexOf(store.slug) % storeColours.length ], }} />
))}
); } const thumbTones = [ 'bg-(--ov-pandan-soft) text-(--ov-pandan-ink)', 'bg-(--ov-kunyit-soft) text-(--ov-kunyit-ink)', 'bg-(--ov-bunga-soft) text-(--ov-bunga-ink)', ]; /** The best sellers in the range. */ export function TopProducts({ stats, showStore, }: { stats: OverviewStats; showStore: boolean; }) { if (stats.top_products.length === 0) { return (

No sales in this time yet.

); } return ( ); } /** The newest paid orders, as a table that scrolls sideways on a phone. */ export function RecentOrders({ stats, showStore, }: { stats: OverviewStats; showStore: boolean; }) { if (stats.recent_orders.length === 0) { return

No paid orders yet.

; } return (
{showStore && ( )} {stats.recent_orders.map((order) => ( {showStore && ( )} ))}
Order CustomerStoreDelivers to Total Status Paid
#{order.number} {order.customer_name} {order.store.name} {order.state ?? '—'} {ringgit(order.total, true)} {order.shipped ? 'Shipped' : 'To ship'} {when(order.paid_at)}
); } /** Until the first sale: the four steps to get there, each with a button. */ export function GettingStartedCard({ data }: { data: GettingStarted }) { const slug = data.store?.slug; const steps = [ { key: 'create', title: 'Create your store', note: 'Tell us about your brand.', done: data.steps.create, action: { label: 'Create store', href: create.url() }, }, { key: 'design', title: 'Let AI design it', note: data.store ? `A full design for ${data.store.name}.` : 'A full design in about a minute.', done: data.steps.design, action: slug ? { label: 'Design store', href: StoreController.show.url(slug), } : null, }, { key: 'products', title: 'Add your products', note: 'Name, price, photo and stock.', done: data.steps.products, action: slug ? { label: 'Add products', href: ProductController.create.url(slug), } : null, }, { key: 'publish', title: 'Publish your store', note: 'Then share the link on WhatsApp.', done: data.steps.publish, action: slug ? { label: 'Publish', href: StoreController.show.url(slug) } : null, }, ]; const done = steps.filter((step) => step.done).length; const grown = useGrow(); const next = steps.find((step) => !step.done); return (

Get your first sale

{done} of {steps.length} steps done

    {steps.map((step) => (
  1. {step.done ? (
  2. ))}
); } /** Where the sales chart goes before there are any sales. */ export function EmptySales({ storeUrl }: { storeUrl: string | null }) { return (
); }