| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- 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 (
- <section
- aria-label={title}
- className={cn(
- 'ov-rise min-w-0 rounded-[18px] border border-(--ov-line) bg-(--ov-card) p-5 sm:p-6',
- className,
- )}
- style={{ animationDelay: `${delay}ms` }}
- >
- <div className="mb-4 flex items-baseline justify-between gap-3">
- <h2 className="ov-heading text-[17px] font-bold">{title}</h2>
- {aside && (
- <div className="text-sm whitespace-nowrap text-(--ov-muted)">
- {aside}
- </div>
- )}
- </div>
- {children}
- </section>
- );
- }
-
- 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 (
- <div className="grid justify-items-center gap-2 rounded-2xl bg-(--ov-paper) px-4 py-10 text-center">
- <CheckCircle2
- className="size-8 text-(--ov-pandan)"
- aria-hidden="true"
- />
- <p className="font-semibold">All clear</p>
- <p className="text-sm text-(--ov-muted)">
- No orders waiting and nothing running low.
- </p>
- </div>
- );
- }
-
- return (
- <ul className="grid gap-2.5">
- {items.map((item, index) => {
- const Icon = attentionIcon(item);
-
- return (
- <li
- key={index}
- className="grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-x-3 rounded-2xl bg-(--ov-paper) p-3"
- >
- <span
- className={cn(
- 'grid size-9 place-items-center rounded-[10px]',
- tones[item.tone],
- )}
- >
- <Icon className="size-[18px]" aria-hidden="true" />
- </span>
- <div className="min-w-0">
- <p className="text-sm leading-snug font-semibold">
- {item.title}
- </p>
- <p className="text-xs text-(--ov-muted)">
- {showStore &&
- item.store &&
- item.kind !== 'publish'
- ? `${item.store.name} · `
- : ''}
- {item.note}
- </p>
- </div>
- <Link
- href={actionHref(
- item.kind,
- item.store?.slug,
- item.product_id,
- )}
- 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)"
- >
- {attentionButton[item.kind]}
- </Link>
- </li>
- );
- })}
- </ul>
- );
- }
-
- /** 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 (
- <div className="grid gap-3.5">
- {stats.by_store.map((store) => (
- <Link
- key={store.slug}
- href={StoreController.show.url(store.slug)}
- className={cn(
- 'grid gap-1.5 rounded-lg transition-opacity hover:opacity-100',
- (!store.is_published && store.sales === 0) ||
- (anySelected && !store.selected)
- ? 'opacity-55'
- : '',
- )}
- >
- <div className="flex justify-between gap-3 text-sm">
- <span
- className={cn(
- 'truncate',
- store.selected && 'font-semibold',
- )}
- >
- {store.name}
- </span>
- <span className="shrink-0 text-(--ov-muted) tabular-nums">
- {!store.is_published && store.sales === 0
- ? 'Not published yet'
- : `${ringgit(store.sales)} · ${store.share}%`}
- </span>
- </div>
- <div className="h-2.5 overflow-hidden rounded-full bg-(--ov-hover)">
- <div
- className="h-full rounded-full transition-[width] duration-1000 ease-out"
- style={{
- width:
- grown && max > 0
- ? `${(store.sales / max) * 100}%`
- : '0%',
- background:
- storeColours[
- rank.indexOf(store.slug) %
- storeColours.length
- ],
- }}
- />
- </div>
- </Link>
- ))}
- </div>
- );
- }
-
- 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 (
- <p className="text-sm text-(--ov-muted)">
- No sales in this time yet.
- </p>
- );
- }
-
- return (
- <ul className="grid gap-3">
- {stats.top_products.map((product, index) => {
- const inner = (
- <>
- <span
- className={cn(
- 'ov-heading grid size-10 place-items-center rounded-[10px] text-[15px] font-extrabold',
- thumbTones[index % thumbTones.length],
- )}
- aria-hidden="true"
- >
- {product.name.charAt(0).toUpperCase()}
- </span>
- <span className="min-w-0">
- <span className="block truncate text-sm font-medium">
- {product.name}
- </span>
- <span className="block text-xs text-(--ov-muted) tabular-nums">
- {product.units} sold
- {showStore && product.store.name
- ? ` · ${product.store.name}`
- : ''}
- </span>
- </span>
- <span className="text-right text-sm font-semibold tabular-nums">
- {ringgit(product.revenue)}
- </span>
- </>
- );
-
- return (
- <li
- key={`${product.store.slug}-${product.product_id ?? product.name}`}
- >
- {product.product_id && product.store.slug ? (
- <Link
- href={ProductController.edit.url({
- slug: product.store.slug,
- product: product.product_id,
- })}
- className="grid grid-cols-[40px_minmax(0,1fr)_auto] items-center gap-3 rounded-lg hover:bg-(--ov-paper)"
- >
- {inner}
- </Link>
- ) : (
- <div className="grid grid-cols-[40px_minmax(0,1fr)_auto] items-center gap-3">
- {inner}
- </div>
- )}
- </li>
- );
- })}
- </ul>
- );
- }
-
- /** 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 <p className="text-sm text-(--ov-muted)">No paid orders yet.</p>;
- }
-
- return (
- <div className="-mx-5 overflow-x-auto px-5 sm:-mx-6 sm:px-6">
- <table className="w-full min-w-[600px] border-collapse text-sm">
- <thead>
- <tr className="text-left text-xs text-(--ov-muted)">
- <th className="pr-3 pb-2.5 font-medium">Order</th>
- <th className="pr-3 pb-2.5 font-medium">Customer</th>
- {showStore && (
- <th className="pr-3 pb-2.5 font-medium">Store</th>
- )}
- <th className="pr-3 pb-2.5 font-medium">Delivers to</th>
- <th className="pr-3 pb-2.5 text-right font-medium">
- Total
- </th>
- <th className="pr-3 pb-2.5 font-medium">Status</th>
- <th className="pb-2.5 font-medium">Paid</th>
- </tr>
- </thead>
- <tbody>
- {stats.recent_orders.map((order) => (
- <tr
- key={order.id}
- className="border-t border-(--ov-line) transition-colors hover:bg-(--ov-paper)"
- >
- <td className="py-3 pr-3 text-(--ov-muted) tabular-nums">
- <Link
- href={OrderController.show.url({
- slug: order.store.slug,
- order: order.id,
- })}
- className="hover:text-(--ov-ink) hover:underline"
- >
- #{order.number}
- </Link>
- </td>
- <td className="py-3 pr-3 whitespace-nowrap">
- {order.customer_name}
- </td>
- {showStore && (
- <td className="py-3 pr-3 whitespace-nowrap">
- {order.store.name}
- </td>
- )}
- <td className="py-3 pr-3 whitespace-nowrap">
- {order.state ?? '—'}
- </td>
- <td className="py-3 pr-3 text-right whitespace-nowrap tabular-nums">
- {ringgit(order.total, true)}
- </td>
- <td className="py-3 pr-3">
- <span
- className={cn(
- 'inline-flex rounded-full px-2.5 py-0.5 text-xs font-semibold whitespace-nowrap',
- order.shipped
- ? 'bg-(--ov-pandan-soft) text-(--ov-pandan-ink)'
- : 'bg-[#F2B33D] text-[#3D2A05]',
- )}
- >
- {order.shipped ? 'Shipped' : 'To ship'}
- </span>
- </td>
- <td className="py-3 whitespace-nowrap text-(--ov-muted)">
- {when(order.paid_at)}
- </td>
- </tr>
- ))}
- </tbody>
- </table>
- </div>
- );
- }
-
- /** 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 (
- <section
- aria-label="Get your first sale"
- className="ov-rise grid gap-5 rounded-[20px] bg-(--ov-pulse) p-6 text-[#E7F0EA]"
- style={{ animationDelay: '80ms' }}
- >
- <div className="grid gap-2.5">
- <h2 className="ov-heading text-2xl font-extrabold">
- Get your first sale
- </h2>
- <p className="text-sm text-[#E7F0EA]/75 tabular-nums">
- {done} of {steps.length} steps done
- </p>
- <div className="h-2 max-w-md overflow-hidden rounded-full bg-white/15">
- <div
- className="h-full rounded-full bg-[#F2B33D] transition-[width] duration-1000 ease-out"
- style={{
- width: grown
- ? `${(done / steps.length) * 100}%`
- : '0%',
- }}
- />
- </div>
- </div>
- <ol className="grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
- {steps.map((step) => (
- <li
- key={step.key}
- className={cn(
- 'flex flex-col gap-2 rounded-2xl bg-white/8 p-4',
- step.done && 'opacity-70',
- )}
- >
- {step.done ? (
- <CheckCircle2
- className="size-6 text-[#F2B33D]"
- aria-hidden="true"
- />
- ) : (
- <Circle
- className="size-6 text-[#F2B33D]"
- aria-hidden="true"
- />
- )}
- <b className="text-sm">
- {step.title}
- {step.done && (
- <span className="sr-only"> (done)</span>
- )}
- </b>
- <span className="flex-1 text-xs text-[#E7F0EA]/75">
- {step.note}
- </span>
- {!step.done && step.action && step === next && (
- <Link
- href={step.action.href}
- className="self-start rounded-full bg-[#F2B33D] px-3.5 py-1.5 text-xs font-semibold text-[#3D2A05] hover:bg-[#f5c25f]"
- >
- {step.action.label}
- </Link>
- )}
- </li>
- ))}
- </ol>
- </section>
- );
- }
-
- /** Where the sales chart goes before there are any sales. */
- export function EmptySales({ storeUrl }: { storeUrl: string | null }) {
- return (
- <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)">
- <StoreIcon
- className="size-8 text-(--ov-pandan)"
- aria-hidden="true"
- />
- <p className="text-base font-semibold text-(--ov-ink)">
- Your sales chart starts with your first order
- </p>
- <p className="text-sm">
- {storeUrl
- ? 'Share your store link with friends and customers.'
- : 'Publish your store, then share its link with friends and customers.'}
- </p>
- {storeUrl && (
- <a
- href={storeUrl}
- target="_blank"
- rel="noreferrer"
- 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)"
- >
- {storeUrl.replace(/^https?:\/\//, '')}
- </a>
- )}
- </div>
- );
- }
|