AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dashboard.tsx 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { Deferred, Head, router, usePage } from '@inertiajs/react';
  2. import { ReceiptText, ShoppingBag, TrendingUp, Truck } from 'lucide-react';
  3. import { fontsUrl } from '@/components/home/brand';
  4. import KpiCards from '@/components/overview/kpi-cards';
  5. import PulseCard, { PulseLoading } from '@/components/overview/pulse-card';
  6. import SalesChart from '@/components/overview/sales-chart';
  7. import {
  8. AttentionList,
  9. EmptySales,
  10. GettingStartedCard,
  11. Panel,
  12. RecentOrders,
  13. StoreSales,
  14. TopProducts,
  15. } from '@/components/overview/sections';
  16. import { cn } from '@/lib/utils';
  17. import { dashboard } from '@/routes';
  18. import type {
  19. GettingStarted,
  20. OverviewRange,
  21. OverviewStats,
  22. StorePulse,
  23. } from '@/types';
  24. type Props = {
  25. filters: { range: OverviewRange; store: string | null };
  26. stores: { name: string; slug: string }[];
  27. hasSales: boolean;
  28. gettingStarted: GettingStarted | null;
  29. liveUrl: string | null;
  30. stats: OverviewStats | null;
  31. /** Loaded just after the page (see DashboardController). Null until the first sale. */
  32. pulse?: StorePulse | null;
  33. };
  34. const ranges: { value: OverviewRange; label: string }[] = [
  35. { value: 'today', label: 'Today' },
  36. { value: '7d', label: '7 days' },
  37. { value: '30d', label: '30 days' },
  38. { value: 'month', label: 'This month' },
  39. ];
  40. function greeting(): string {
  41. const hour = Number(
  42. new Intl.DateTimeFormat('en-MY', {
  43. hour: 'numeric',
  44. hourCycle: 'h23',
  45. timeZone: 'Asia/Kuala_Lumpur',
  46. }).format(new Date()),
  47. );
  48. return hour < 12
  49. ? 'Good morning'
  50. : hour < 18
  51. ? 'Good afternoon'
  52. : 'Good evening';
  53. }
  54. /** Time range buttons and the store picker. Changing either reloads the numbers. */
  55. function Filters({ filters, stores }: Pick<Props, 'filters' | 'stores'>) {
  56. const go = (next: Partial<Props['filters']>) => {
  57. const query = { ...filters, ...next };
  58. router.get(
  59. dashboard.url({
  60. query: {
  61. range: query.range === '30d' ? undefined : query.range,
  62. store: query.store ?? undefined,
  63. },
  64. }),
  65. {},
  66. { preserveState: true, preserveScroll: true, replace: true },
  67. );
  68. };
  69. return (
  70. <div className="flex flex-wrap items-center gap-2.5">
  71. <div
  72. role="group"
  73. aria-label="Time range"
  74. className="inline-flex rounded-full border border-(--ov-line) bg-(--ov-card) p-[3px]"
  75. >
  76. {ranges.map((range) => (
  77. <button
  78. key={range.value}
  79. type="button"
  80. aria-pressed={filters.range === range.value}
  81. onClick={() => go({ range: range.value })}
  82. className={cn(
  83. 'rounded-full px-3 py-1.5 text-[13px] whitespace-nowrap transition-colors sm:px-3.5',
  84. filters.range === range.value
  85. ? 'bg-(--ov-ink) font-semibold text-(--ov-paper)'
  86. : 'text-(--ov-muted) hover:text-(--ov-ink)',
  87. )}
  88. >
  89. {range.label}
  90. </button>
  91. ))}
  92. </div>
  93. {stores.length > 1 && (
  94. <>
  95. <label htmlFor="overview-store" className="sr-only">
  96. Store
  97. </label>
  98. <select
  99. id="overview-store"
  100. value={filters.store ?? ''}
  101. onChange={(event) =>
  102. go({ store: event.target.value || null })
  103. }
  104. className="rounded-full border border-(--ov-line) bg-(--ov-card) py-[7px] pr-8 pl-3.5 text-[13px] text-(--ov-ink)"
  105. >
  106. <option value="">All stores</option>
  107. {stores.map((store) => (
  108. <option key={store.slug} value={store.slug}>
  109. {store.name}
  110. </option>
  111. ))}
  112. </select>
  113. </>
  114. )}
  115. </div>
  116. );
  117. }
  118. /** Before the first sale: the same four boxes, at zero, with a word on what will show there. */
  119. function ZeroCards() {
  120. const cards = [
  121. {
  122. label: 'Total sales',
  123. value: 'RM 0',
  124. note: 'Your first sale shows here',
  125. icon: TrendingUp,
  126. hero: true,
  127. },
  128. {
  129. label: 'Paid orders',
  130. value: '0',
  131. note: 'No orders yet',
  132. icon: ReceiptText,
  133. },
  134. {
  135. label: 'Average order',
  136. value: 'RM 0',
  137. note: 'per paid order',
  138. icon: ShoppingBag,
  139. },
  140. {
  141. label: 'To ship now',
  142. value: '0',
  143. note: 'Nothing to send',
  144. icon: Truck,
  145. },
  146. ];
  147. return (
  148. <section
  149. aria-label="Key numbers"
  150. className="grid grid-cols-2 gap-3 sm:gap-4 xl:grid-cols-4"
  151. >
  152. {cards.map((card, index) => (
  153. <div
  154. key={card.label}
  155. className={cn(
  156. 'ov-rise rounded-[18px] border px-5 py-4',
  157. card.hero
  158. ? 'border-(--ov-hero) bg-(--ov-hero) text-white'
  159. : 'border-(--ov-line) bg-(--ov-card)',
  160. )}
  161. style={{ animationDelay: `${160 + index * 60}ms` }}
  162. >
  163. <div className="flex items-center gap-2 text-sm font-medium opacity-80">
  164. <card.icon className="size-4" aria-hidden="true" />
  165. {card.label}
  166. </div>
  167. <div className="ov-heading mt-1.5 text-[clamp(1.5rem,2.4vw,2rem)] font-extrabold">
  168. {card.value}
  169. </div>
  170. <div
  171. className={cn(
  172. 'text-sm',
  173. card.hero ? 'text-white/80' : 'text-(--ov-muted)',
  174. )}
  175. >
  176. {card.note}
  177. </div>
  178. </div>
  179. ))}
  180. </section>
  181. );
  182. }
  183. /**
  184. * The Overview page. Top to bottom: the AI note, four big numbers, the sales chart beside
  185. * "Needs attention", sales by store beside top products, and the newest orders.
  186. * Before the first sale it shows the steps to get there instead.
  187. */
  188. export default function Dashboard({
  189. filters,
  190. stores,
  191. hasSales,
  192. gettingStarted,
  193. liveUrl,
  194. stats,
  195. }: Props) {
  196. const { auth } = usePage().props;
  197. const firstName = (auth.user?.name ?? '').split(' ')[0];
  198. const showStore = filters.store === null && stores.length > 1;
  199. return (
  200. <>
  201. <Head title="Overview">
  202. <link rel="stylesheet" href={fontsUrl} />
  203. </Head>
  204. <div className="overview flex-1 px-4 pt-6 pb-14 sm:px-6 lg:px-8">
  205. <div className="mx-auto flex w-full max-w-[1200px] flex-col gap-6">
  206. <div className="ov-rise flex flex-wrap items-end justify-between gap-4">
  207. <div>
  208. <h1 className="ov-heading text-[clamp(1.45rem,2.4vw,1.75rem)] font-extrabold">
  209. {hasSales
  210. ? `${greeting()}${firstName ? `, ${firstName}` : ''}`
  211. : `Welcome${firstName ? `, ${firstName}` : ''}`}
  212. </h1>
  213. <p className="mt-0.5 text-(--ov-muted)">
  214. {hasSales
  215. ? stats?.label === 'Today, by hour'
  216. ? 'Here’s how today is going.'
  217. : 'Here’s how your stores are doing.'
  218. : 'A few steps and your store is ready for its first order.'}
  219. </p>
  220. </div>
  221. {hasSales && (
  222. <Filters filters={filters} stores={stores} />
  223. )}
  224. </div>
  225. {hasSales && stats ? (
  226. <>
  227. <Deferred data="pulse" fallback={<PulseLoading />}>
  228. <PulseSlot store={filters.store} />
  229. </Deferred>
  230. <KpiCards stats={stats} />
  231. <div className="grid gap-4 lg:grid-cols-[minmax(0,2fr)_minmax(0,1fr)]">
  232. <Panel
  233. title="Sales"
  234. delay={400}
  235. aside={
  236. <span className="flex flex-wrap justify-end gap-x-4 gap-y-1">
  237. <span className="inline-flex items-center gap-1.5">
  238. <i className="inline-block h-[3px] w-4 rounded bg-(--ov-pandan)" />
  239. This period
  240. </span>
  241. <span className="inline-flex items-center gap-1.5">
  242. <i className="inline-block w-4 border-t-2 border-dashed border-(--ov-prev)" />
  243. Before
  244. </span>
  245. </span>
  246. }
  247. >
  248. <p className="-mt-3 mb-3 text-sm text-(--ov-muted)">
  249. {stats.label}
  250. </p>
  251. <SalesChart stats={stats} />
  252. </Panel>
  253. <Panel
  254. title="Needs attention"
  255. delay={460}
  256. aside={
  257. stats.attention.length > 0
  258. ? `${stats.attention.length} ${stats.attention.length === 1 ? 'thing' : 'things'}`
  259. : null
  260. }
  261. >
  262. <AttentionList
  263. items={stats.attention}
  264. showStore={showStore}
  265. />
  266. </Panel>
  267. </div>
  268. <div className="grid gap-4 md:grid-cols-2">
  269. {stores.length > 1 && (
  270. <Panel
  271. title="Sales by store"
  272. delay={520}
  273. aside={stats.label}
  274. >
  275. <StoreSales stats={stats} />
  276. </Panel>
  277. )}
  278. <Panel
  279. title="Top products"
  280. delay={580}
  281. aside={stats.label}
  282. className={
  283. stores.length > 1
  284. ? undefined
  285. : 'md:col-span-2'
  286. }
  287. >
  288. <TopProducts
  289. stats={stats}
  290. showStore={showStore}
  291. />
  292. </Panel>
  293. </div>
  294. <Panel title="Recent orders" delay={640}>
  295. <RecentOrders
  296. stats={stats}
  297. showStore={showStore}
  298. />
  299. </Panel>
  300. </>
  301. ) : (
  302. <>
  303. {gettingStarted && (
  304. <GettingStartedCard data={gettingStarted} />
  305. )}
  306. <ZeroCards />
  307. <Panel title="Sales" delay={400}>
  308. <EmptySales storeUrl={liveUrl} />
  309. </Panel>
  310. </>
  311. )}
  312. </div>
  313. </div>
  314. </>
  315. );
  316. }
  317. /** The pulse card, once its data has arrived. Nothing shows when there is no note. */
  318. function PulseSlot({ store }: { store: string | null }) {
  319. const { pulse } = usePage<Props>().props;
  320. return pulse ? <PulseCard pulse={pulse} store={store} /> : null;
  321. }
  322. Dashboard.layout = {
  323. breadcrumbs: [
  324. {
  325. title: 'Overview',
  326. href: dashboard(),
  327. },
  328. ],
  329. };