AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

sections.tsx 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. import { Link } from '@inertiajs/react';
  2. import {
  3. AlertTriangle,
  4. CheckCircle2,
  5. Circle,
  6. CreditCard,
  7. PackageX,
  8. Store as StoreIcon,
  9. Truck,
  10. UploadCloud,
  11. } from 'lucide-react';
  12. import type { ReactNode } from 'react';
  13. import { useEffect, useState } from 'react';
  14. import OrderController from '@/actions/App/Http/Controllers/OrderController';
  15. import ProductController from '@/actions/App/Http/Controllers/ProductController';
  16. import StoreController from '@/actions/App/Http/Controllers/StoreController';
  17. import { actionHref, ringgit, when } from '@/components/overview/format';
  18. import { cn } from '@/lib/utils';
  19. import { create } from '@/routes/stores';
  20. import type { AttentionItem, GettingStarted, OverviewStats } from '@/types';
  21. /** A white box with a heading, used by every section below the big numbers. */
  22. export function Panel({
  23. title,
  24. aside,
  25. delay,
  26. className,
  27. children,
  28. }: {
  29. title: string;
  30. aside?: ReactNode;
  31. delay: number;
  32. className?: string;
  33. children: ReactNode;
  34. }) {
  35. return (
  36. <section
  37. aria-label={title}
  38. className={cn(
  39. 'ov-rise min-w-0 rounded-[18px] border border-(--ov-line) bg-(--ov-card) p-5 sm:p-6',
  40. className,
  41. )}
  42. style={{ animationDelay: `${delay}ms` }}
  43. >
  44. <div className="mb-4 flex items-baseline justify-between gap-3">
  45. <h2 className="ov-heading text-[17px] font-bold">{title}</h2>
  46. {aside && (
  47. <div className="text-sm whitespace-nowrap text-(--ov-muted)">
  48. {aside}
  49. </div>
  50. )}
  51. </div>
  52. {children}
  53. </section>
  54. );
  55. }
  56. const tones = {
  57. warning: 'bg-(--ov-kunyit-soft) text-(--ov-kunyit-ink)',
  58. danger: 'bg-(--ov-bunga-soft) text-(--ov-bunga-ink)',
  59. brand: 'bg-(--ov-pandan-soft) text-(--ov-pandan-ink)',
  60. };
  61. const attentionIcon = (item: AttentionItem) =>
  62. ({
  63. ship: Truck,
  64. restock: item.title.includes('out of stock') ? PackageX : AlertTriangle,
  65. publish: UploadCloud,
  66. plan: CreditCard,
  67. })[item.kind];
  68. const attentionButton = {
  69. ship: 'Ship',
  70. restock: 'Restock',
  71. publish: 'Open',
  72. plan: 'Choose plan',
  73. };
  74. /** Everything that needs the merchant now, in one list, each with its own button. */
  75. export function AttentionList({
  76. items,
  77. showStore,
  78. }: {
  79. items: AttentionItem[];
  80. showStore: boolean;
  81. }) {
  82. if (items.length === 0) {
  83. return (
  84. <div className="grid justify-items-center gap-2 rounded-2xl bg-(--ov-paper) px-4 py-10 text-center">
  85. <CheckCircle2
  86. className="size-8 text-(--ov-pandan)"
  87. aria-hidden="true"
  88. />
  89. <p className="font-semibold">All clear</p>
  90. <p className="text-sm text-(--ov-muted)">
  91. No orders waiting and nothing running low.
  92. </p>
  93. </div>
  94. );
  95. }
  96. return (
  97. <ul className="grid gap-2.5">
  98. {items.map((item, index) => {
  99. const Icon = attentionIcon(item);
  100. return (
  101. <li
  102. key={index}
  103. className="grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-x-3 rounded-2xl bg-(--ov-paper) p-3"
  104. >
  105. <span
  106. className={cn(
  107. 'grid size-9 place-items-center rounded-[10px]',
  108. tones[item.tone],
  109. )}
  110. >
  111. <Icon className="size-[18px]" aria-hidden="true" />
  112. </span>
  113. <div className="min-w-0">
  114. <p className="text-sm leading-snug font-semibold">
  115. {item.title}
  116. </p>
  117. <p className="text-xs text-(--ov-muted)">
  118. {showStore &&
  119. item.store &&
  120. item.kind !== 'publish'
  121. ? `${item.store.name} · `
  122. : ''}
  123. {item.note}
  124. </p>
  125. </div>
  126. <Link
  127. href={actionHref(
  128. item.kind,
  129. item.store?.slug,
  130. item.product_id,
  131. )}
  132. className="rounded-full border border-(--ov-line) bg-(--ov-card) px-3 py-1.5 text-xs font-semibold whitespace-nowrap transition-colors hover:border-(--ov-pandan) hover:text-(--ov-pandan-ink)"
  133. >
  134. {attentionButton[item.kind]}
  135. </Link>
  136. </li>
  137. );
  138. })}
  139. </ul>
  140. );
  141. }
  142. /** Bars that grow from zero after the page shows. */
  143. function useGrow(): boolean {
  144. const [grown, setGrown] = useState(false);
  145. useEffect(() => {
  146. const frame = requestAnimationFrame(() =>
  147. requestAnimationFrame(() => setGrown(true)),
  148. );
  149. return () => cancelAnimationFrame(frame);
  150. }, []);
  151. return grown;
  152. }
  153. const storeColours = [
  154. 'var(--ov-pandan)',
  155. 'var(--ov-kunyit)',
  156. 'var(--ov-bunga)',
  157. '#4C8C9E',
  158. '#8A6FB8',
  159. ];
  160. /** Each store's share of sales in the range. Stores not published yet sit at the bottom, faded. */
  161. export function StoreSales({ stats }: { stats: OverviewStats }) {
  162. const grown = useGrow();
  163. const max = Math.max(...stats.by_store.map((store) => store.sales), 0);
  164. const anySelected = stats.by_store.some((store) => store.selected);
  165. // The best seller gets green, the next yellow, then red, whatever order the stores are in.
  166. const rank = [...stats.by_store]
  167. .sort((a, b) => b.sales - a.sales)
  168. .map((store) => store.slug);
  169. return (
  170. <div className="grid gap-3.5">
  171. {stats.by_store.map((store) => (
  172. <Link
  173. key={store.slug}
  174. href={StoreController.show.url(store.slug)}
  175. className={cn(
  176. 'grid gap-1.5 rounded-lg transition-opacity hover:opacity-100',
  177. (!store.is_published && store.sales === 0) ||
  178. (anySelected && !store.selected)
  179. ? 'opacity-55'
  180. : '',
  181. )}
  182. >
  183. <div className="flex justify-between gap-3 text-sm">
  184. <span
  185. className={cn(
  186. 'truncate',
  187. store.selected && 'font-semibold',
  188. )}
  189. >
  190. {store.name}
  191. </span>
  192. <span className="shrink-0 text-(--ov-muted) tabular-nums">
  193. {!store.is_published && store.sales === 0
  194. ? 'Not published yet'
  195. : `${ringgit(store.sales)} · ${store.share}%`}
  196. </span>
  197. </div>
  198. <div className="h-2.5 overflow-hidden rounded-full bg-(--ov-hover)">
  199. <div
  200. className="h-full rounded-full transition-[width] duration-1000 ease-out"
  201. style={{
  202. width:
  203. grown && max > 0
  204. ? `${(store.sales / max) * 100}%`
  205. : '0%',
  206. background:
  207. storeColours[
  208. rank.indexOf(store.slug) %
  209. storeColours.length
  210. ],
  211. }}
  212. />
  213. </div>
  214. </Link>
  215. ))}
  216. </div>
  217. );
  218. }
  219. const thumbTones = [
  220. 'bg-(--ov-pandan-soft) text-(--ov-pandan-ink)',
  221. 'bg-(--ov-kunyit-soft) text-(--ov-kunyit-ink)',
  222. 'bg-(--ov-bunga-soft) text-(--ov-bunga-ink)',
  223. ];
  224. /** The best sellers in the range. */
  225. export function TopProducts({
  226. stats,
  227. showStore,
  228. }: {
  229. stats: OverviewStats;
  230. showStore: boolean;
  231. }) {
  232. if (stats.top_products.length === 0) {
  233. return (
  234. <p className="text-sm text-(--ov-muted)">
  235. No sales in this time yet.
  236. </p>
  237. );
  238. }
  239. return (
  240. <ul className="grid gap-3">
  241. {stats.top_products.map((product, index) => {
  242. const inner = (
  243. <>
  244. <span
  245. className={cn(
  246. 'ov-heading grid size-10 place-items-center rounded-[10px] text-[15px] font-extrabold',
  247. thumbTones[index % thumbTones.length],
  248. )}
  249. aria-hidden="true"
  250. >
  251. {product.name.charAt(0).toUpperCase()}
  252. </span>
  253. <span className="min-w-0">
  254. <span className="block truncate text-sm font-medium">
  255. {product.name}
  256. </span>
  257. <span className="block text-xs text-(--ov-muted) tabular-nums">
  258. {product.units} sold
  259. {showStore && product.store.name
  260. ? ` · ${product.store.name}`
  261. : ''}
  262. </span>
  263. </span>
  264. <span className="text-right text-sm font-semibold tabular-nums">
  265. {ringgit(product.revenue)}
  266. </span>
  267. </>
  268. );
  269. return (
  270. <li
  271. key={`${product.store.slug}-${product.product_id ?? product.name}`}
  272. >
  273. {product.product_id && product.store.slug ? (
  274. <Link
  275. href={ProductController.edit.url({
  276. slug: product.store.slug,
  277. product: product.product_id,
  278. })}
  279. className="grid grid-cols-[40px_minmax(0,1fr)_auto] items-center gap-3 rounded-lg hover:bg-(--ov-paper)"
  280. >
  281. {inner}
  282. </Link>
  283. ) : (
  284. <div className="grid grid-cols-[40px_minmax(0,1fr)_auto] items-center gap-3">
  285. {inner}
  286. </div>
  287. )}
  288. </li>
  289. );
  290. })}
  291. </ul>
  292. );
  293. }
  294. /** The newest paid orders, as a table that scrolls sideways on a phone. */
  295. export function RecentOrders({
  296. stats,
  297. showStore,
  298. }: {
  299. stats: OverviewStats;
  300. showStore: boolean;
  301. }) {
  302. if (stats.recent_orders.length === 0) {
  303. return <p className="text-sm text-(--ov-muted)">No paid orders yet.</p>;
  304. }
  305. return (
  306. <div className="-mx-5 overflow-x-auto px-5 sm:-mx-6 sm:px-6">
  307. <table className="w-full min-w-[600px] border-collapse text-sm">
  308. <thead>
  309. <tr className="text-left text-xs text-(--ov-muted)">
  310. <th className="pr-3 pb-2.5 font-medium">Order</th>
  311. <th className="pr-3 pb-2.5 font-medium">Customer</th>
  312. {showStore && (
  313. <th className="pr-3 pb-2.5 font-medium">Store</th>
  314. )}
  315. <th className="pr-3 pb-2.5 font-medium">Delivers to</th>
  316. <th className="pr-3 pb-2.5 text-right font-medium">
  317. Total
  318. </th>
  319. <th className="pr-3 pb-2.5 font-medium">Status</th>
  320. <th className="pb-2.5 font-medium">Paid</th>
  321. </tr>
  322. </thead>
  323. <tbody>
  324. {stats.recent_orders.map((order) => (
  325. <tr
  326. key={order.id}
  327. className="border-t border-(--ov-line) transition-colors hover:bg-(--ov-paper)"
  328. >
  329. <td className="py-3 pr-3 text-(--ov-muted) tabular-nums">
  330. <Link
  331. href={OrderController.show.url({
  332. slug: order.store.slug,
  333. order: order.id,
  334. })}
  335. className="hover:text-(--ov-ink) hover:underline"
  336. >
  337. #{order.number}
  338. </Link>
  339. </td>
  340. <td className="py-3 pr-3 whitespace-nowrap">
  341. {order.customer_name}
  342. </td>
  343. {showStore && (
  344. <td className="py-3 pr-3 whitespace-nowrap">
  345. {order.store.name}
  346. </td>
  347. )}
  348. <td className="py-3 pr-3 whitespace-nowrap">
  349. {order.state ?? '—'}
  350. </td>
  351. <td className="py-3 pr-3 text-right whitespace-nowrap tabular-nums">
  352. {ringgit(order.total, true)}
  353. </td>
  354. <td className="py-3 pr-3">
  355. <span
  356. className={cn(
  357. 'inline-flex rounded-full px-2.5 py-0.5 text-xs font-semibold whitespace-nowrap',
  358. order.shipped
  359. ? 'bg-(--ov-pandan-soft) text-(--ov-pandan-ink)'
  360. : 'bg-[#F2B33D] text-[#3D2A05]',
  361. )}
  362. >
  363. {order.shipped ? 'Shipped' : 'To ship'}
  364. </span>
  365. </td>
  366. <td className="py-3 whitespace-nowrap text-(--ov-muted)">
  367. {when(order.paid_at)}
  368. </td>
  369. </tr>
  370. ))}
  371. </tbody>
  372. </table>
  373. </div>
  374. );
  375. }
  376. /** Until the first sale: the four steps to get there, each with a button. */
  377. export function GettingStartedCard({ data }: { data: GettingStarted }) {
  378. const slug = data.store?.slug;
  379. const steps = [
  380. {
  381. key: 'create',
  382. title: 'Create your store',
  383. note: 'Tell us about your brand.',
  384. done: data.steps.create,
  385. action: { label: 'Create store', href: create.url() },
  386. },
  387. {
  388. key: 'design',
  389. title: 'Let AI design it',
  390. note: data.store
  391. ? `A full design for ${data.store.name}.`
  392. : 'A full design in about a minute.',
  393. done: data.steps.design,
  394. action: slug
  395. ? {
  396. label: 'Design store',
  397. href: StoreController.show.url(slug),
  398. }
  399. : null,
  400. },
  401. {
  402. key: 'products',
  403. title: 'Add your products',
  404. note: 'Name, price, photo and stock.',
  405. done: data.steps.products,
  406. action: slug
  407. ? {
  408. label: 'Add products',
  409. href: ProductController.create.url(slug),
  410. }
  411. : null,
  412. },
  413. {
  414. key: 'publish',
  415. title: 'Publish your store',
  416. note: 'Then share the link on WhatsApp.',
  417. done: data.steps.publish,
  418. action: slug
  419. ? { label: 'Publish', href: StoreController.show.url(slug) }
  420. : null,
  421. },
  422. ];
  423. const done = steps.filter((step) => step.done).length;
  424. const grown = useGrow();
  425. const next = steps.find((step) => !step.done);
  426. return (
  427. <section
  428. aria-label="Get your first sale"
  429. className="ov-rise grid gap-5 rounded-[20px] bg-(--ov-pulse) p-6 text-[#E7F0EA]"
  430. style={{ animationDelay: '80ms' }}
  431. >
  432. <div className="grid gap-2.5">
  433. <h2 className="ov-heading text-2xl font-extrabold">
  434. Get your first sale
  435. </h2>
  436. <p className="text-sm text-[#E7F0EA]/75 tabular-nums">
  437. {done} of {steps.length} steps done
  438. </p>
  439. <div className="h-2 max-w-md overflow-hidden rounded-full bg-white/15">
  440. <div
  441. className="h-full rounded-full bg-[#F2B33D] transition-[width] duration-1000 ease-out"
  442. style={{
  443. width: grown
  444. ? `${(done / steps.length) * 100}%`
  445. : '0%',
  446. }}
  447. />
  448. </div>
  449. </div>
  450. <ol className="grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
  451. {steps.map((step) => (
  452. <li
  453. key={step.key}
  454. className={cn(
  455. 'flex flex-col gap-2 rounded-2xl bg-white/8 p-4',
  456. step.done && 'opacity-70',
  457. )}
  458. >
  459. {step.done ? (
  460. <CheckCircle2
  461. className="size-6 text-[#F2B33D]"
  462. aria-hidden="true"
  463. />
  464. ) : (
  465. <Circle
  466. className="size-6 text-[#F2B33D]"
  467. aria-hidden="true"
  468. />
  469. )}
  470. <b className="text-sm">
  471. {step.title}
  472. {step.done && (
  473. <span className="sr-only"> (done)</span>
  474. )}
  475. </b>
  476. <span className="flex-1 text-xs text-[#E7F0EA]/75">
  477. {step.note}
  478. </span>
  479. {!step.done && step.action && step === next && (
  480. <Link
  481. href={step.action.href}
  482. className="self-start rounded-full bg-[#F2B33D] px-3.5 py-1.5 text-xs font-semibold text-[#3D2A05] hover:bg-[#f5c25f]"
  483. >
  484. {step.action.label}
  485. </Link>
  486. )}
  487. </li>
  488. ))}
  489. </ol>
  490. </section>
  491. );
  492. }
  493. /** Where the sales chart goes before there are any sales. */
  494. export function EmptySales({ storeUrl }: { storeUrl: string | null }) {
  495. return (
  496. <div className="grid justify-items-center gap-2 rounded-2xl border-2 border-dashed border-(--ov-line) px-5 py-10 text-center text-(--ov-muted)">
  497. <StoreIcon
  498. className="size-8 text-(--ov-pandan)"
  499. aria-hidden="true"
  500. />
  501. <p className="text-base font-semibold text-(--ov-ink)">
  502. Your sales chart starts with your first order
  503. </p>
  504. <p className="text-sm">
  505. {storeUrl
  506. ? 'Share your store link with friends and customers.'
  507. : 'Publish your store, then share its link with friends and customers.'}
  508. </p>
  509. {storeUrl && (
  510. <a
  511. href={storeUrl}
  512. target="_blank"
  513. rel="noreferrer"
  514. className="mt-1 rounded-lg border border-(--ov-line) bg-(--ov-paper) px-3 py-1.5 text-sm text-(--ov-ink) hover:border-(--ov-pandan)"
  515. >
  516. {storeUrl.replace(/^https?:\/\//, '')}
  517. </a>
  518. )}
  519. </div>
  520. );
  521. }