| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- import { Deferred, Head, router, usePage } from '@inertiajs/react';
- import { ReceiptText, ShoppingBag, TrendingUp, Truck } from 'lucide-react';
- import { fontsUrl } from '@/components/home/brand';
- import KpiCards from '@/components/overview/kpi-cards';
- import PulseCard, { PulseLoading } from '@/components/overview/pulse-card';
- import SalesChart from '@/components/overview/sales-chart';
- import {
- AttentionList,
- EmptySales,
- GettingStartedCard,
- Panel,
- RecentOrders,
- StoreSales,
- TopProducts,
- } from '@/components/overview/sections';
- import { cn } from '@/lib/utils';
- import { dashboard } from '@/routes';
- import type {
- GettingStarted,
- OverviewRange,
- OverviewStats,
- StorePulse,
- } from '@/types';
-
- type Props = {
- filters: { range: OverviewRange; store: string | null };
- stores: { name: string; slug: string }[];
- hasSales: boolean;
- gettingStarted: GettingStarted | null;
- liveUrl: string | null;
- stats: OverviewStats | null;
- /** Loaded just after the page (see DashboardController). Null until the first sale. */
- pulse?: StorePulse | null;
- };
-
- const ranges: { value: OverviewRange; label: string }[] = [
- { value: 'today', label: 'Today' },
- { value: '7d', label: '7 days' },
- { value: '30d', label: '30 days' },
- { value: 'month', label: 'This month' },
- ];
-
- function greeting(): string {
- const hour = Number(
- new Intl.DateTimeFormat('en-MY', {
- hour: 'numeric',
- hourCycle: 'h23',
- timeZone: 'Asia/Kuala_Lumpur',
- }).format(new Date()),
- );
-
- return hour < 12
- ? 'Good morning'
- : hour < 18
- ? 'Good afternoon'
- : 'Good evening';
- }
-
- /** Time range buttons and the store picker. Changing either reloads the numbers. */
- function Filters({ filters, stores }: Pick<Props, 'filters' | 'stores'>) {
- const go = (next: Partial<Props['filters']>) => {
- const query = { ...filters, ...next };
- router.get(
- dashboard.url({
- query: {
- range: query.range === '30d' ? undefined : query.range,
- store: query.store ?? undefined,
- },
- }),
- {},
- { preserveState: true, preserveScroll: true, replace: true },
- );
- };
-
- return (
- <div className="flex flex-wrap items-center gap-2.5">
- <div
- role="group"
- aria-label="Time range"
- className="inline-flex rounded-full border border-(--ov-line) bg-(--ov-card) p-[3px]"
- >
- {ranges.map((range) => (
- <button
- key={range.value}
- type="button"
- aria-pressed={filters.range === range.value}
- onClick={() => go({ range: range.value })}
- className={cn(
- 'rounded-full px-3 py-1.5 text-[13px] whitespace-nowrap transition-colors sm:px-3.5',
- filters.range === range.value
- ? 'bg-(--ov-ink) font-semibold text-(--ov-paper)'
- : 'text-(--ov-muted) hover:text-(--ov-ink)',
- )}
- >
- {range.label}
- </button>
- ))}
- </div>
-
- {stores.length > 1 && (
- <>
- <label htmlFor="overview-store" className="sr-only">
- Store
- </label>
- <select
- id="overview-store"
- value={filters.store ?? ''}
- onChange={(event) =>
- go({ store: event.target.value || null })
- }
- className="rounded-full border border-(--ov-line) bg-(--ov-card) py-[7px] pr-8 pl-3.5 text-[13px] text-(--ov-ink)"
- >
- <option value="">All stores</option>
- {stores.map((store) => (
- <option key={store.slug} value={store.slug}>
- {store.name}
- </option>
- ))}
- </select>
- </>
- )}
- </div>
- );
- }
-
- /** Before the first sale: the same four boxes, at zero, with a word on what will show there. */
- function ZeroCards() {
- const cards = [
- {
- label: 'Total sales',
- value: 'RM 0',
- note: 'Your first sale shows here',
- icon: TrendingUp,
- hero: true,
- },
- {
- label: 'Paid orders',
- value: '0',
- note: 'No orders yet',
- icon: ReceiptText,
- },
- {
- label: 'Average order',
- value: 'RM 0',
- note: 'per paid order',
- icon: ShoppingBag,
- },
- {
- label: 'To ship now',
- value: '0',
- note: 'Nothing to send',
- icon: Truck,
- },
- ];
-
- return (
- <section
- aria-label="Key numbers"
- className="grid grid-cols-2 gap-3 sm:gap-4 xl:grid-cols-4"
- >
- {cards.map((card, index) => (
- <div
- key={card.label}
- className={cn(
- 'ov-rise rounded-[18px] border px-5 py-4',
- card.hero
- ? 'border-(--ov-hero) bg-(--ov-hero) text-white'
- : 'border-(--ov-line) bg-(--ov-card)',
- )}
- style={{ animationDelay: `${160 + index * 60}ms` }}
- >
- <div className="flex items-center gap-2 text-sm font-medium opacity-80">
- <card.icon className="size-4" aria-hidden="true" />
- {card.label}
- </div>
- <div className="ov-heading mt-1.5 text-[clamp(1.5rem,2.4vw,2rem)] font-extrabold">
- {card.value}
- </div>
- <div
- className={cn(
- 'text-sm',
- card.hero ? 'text-white/80' : 'text-(--ov-muted)',
- )}
- >
- {card.note}
- </div>
- </div>
- ))}
- </section>
- );
- }
-
- /**
- * The Overview page. Top to bottom: the AI note, four big numbers, the sales chart beside
- * "Needs attention", sales by store beside top products, and the newest orders.
- * Before the first sale it shows the steps to get there instead.
- */
- export default function Dashboard({
- filters,
- stores,
- hasSales,
- gettingStarted,
- liveUrl,
- stats,
- }: Props) {
- const { auth } = usePage().props;
- const firstName = (auth.user?.name ?? '').split(' ')[0];
- const showStore = filters.store === null && stores.length > 1;
-
- return (
- <>
- <Head title="Overview">
- <link rel="stylesheet" href={fontsUrl} />
- </Head>
-
- <div className="overview flex-1 px-4 pt-6 pb-14 sm:px-6 lg:px-8">
- <div className="mx-auto flex w-full max-w-[1200px] flex-col gap-6">
- <div className="ov-rise flex flex-wrap items-end justify-between gap-4">
- <div>
- <h1 className="ov-heading text-[clamp(1.45rem,2.4vw,1.75rem)] font-extrabold">
- {hasSales
- ? `${greeting()}${firstName ? `, ${firstName}` : ''}`
- : `Welcome${firstName ? `, ${firstName}` : ''}`}
- </h1>
- <p className="mt-0.5 text-(--ov-muted)">
- {hasSales
- ? stats?.label === 'Today, by hour'
- ? 'Here’s how today is going.'
- : 'Here’s how your stores are doing.'
- : 'A few steps and your store is ready for its first order.'}
- </p>
- </div>
- {hasSales && (
- <Filters filters={filters} stores={stores} />
- )}
- </div>
-
- {hasSales && stats ? (
- <>
- <Deferred data="pulse" fallback={<PulseLoading />}>
- <PulseSlot store={filters.store} />
- </Deferred>
-
- <KpiCards stats={stats} />
-
- <div className="grid gap-4 lg:grid-cols-[minmax(0,2fr)_minmax(0,1fr)]">
- <Panel
- title="Sales"
- delay={400}
- aside={
- <span className="flex flex-wrap justify-end gap-x-4 gap-y-1">
- <span className="inline-flex items-center gap-1.5">
- <i className="inline-block h-[3px] w-4 rounded bg-(--ov-pandan)" />
- This period
- </span>
- <span className="inline-flex items-center gap-1.5">
- <i className="inline-block w-4 border-t-2 border-dashed border-(--ov-prev)" />
- Before
- </span>
- </span>
- }
- >
- <p className="-mt-3 mb-3 text-sm text-(--ov-muted)">
- {stats.label}
- </p>
- <SalesChart stats={stats} />
- </Panel>
-
- <Panel
- title="Needs attention"
- delay={460}
- aside={
- stats.attention.length > 0
- ? `${stats.attention.length} ${stats.attention.length === 1 ? 'thing' : 'things'}`
- : null
- }
- >
- <AttentionList
- items={stats.attention}
- showStore={showStore}
- />
- </Panel>
- </div>
-
- <div className="grid gap-4 md:grid-cols-2">
- {stores.length > 1 && (
- <Panel
- title="Sales by store"
- delay={520}
- aside={stats.label}
- >
- <StoreSales stats={stats} />
- </Panel>
- )}
- <Panel
- title="Top products"
- delay={580}
- aside={stats.label}
- className={
- stores.length > 1
- ? undefined
- : 'md:col-span-2'
- }
- >
- <TopProducts
- stats={stats}
- showStore={showStore}
- />
- </Panel>
- </div>
-
- <Panel title="Recent orders" delay={640}>
- <RecentOrders
- stats={stats}
- showStore={showStore}
- />
- </Panel>
- </>
- ) : (
- <>
- {gettingStarted && (
- <GettingStartedCard data={gettingStarted} />
- )}
- <ZeroCards />
- <Panel title="Sales" delay={400}>
- <EmptySales storeUrl={liveUrl} />
- </Panel>
- </>
- )}
- </div>
- </div>
- </>
- );
- }
-
- /** The pulse card, once its data has arrived. Nothing shows when there is no note. */
- function PulseSlot({ store }: { store: string | null }) {
- const { pulse } = usePage<Props>().props;
-
- return pulse ? <PulseCard pulse={pulse} store={store} /> : null;
- }
-
- Dashboard.layout = {
- breadcrumbs: [
- {
- title: 'Overview',
- href: dashboard(),
- },
- ],
- };
|