AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

sections.tsx 21KB

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