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 (
{title}
{aside && (
{aside}
)}
{children}
);
}
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 (
);
}
/** 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 (
);
}
/** 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
No paid orders yet.
;
}
return (
Order
Customer
{showStore && (
Store
)}
Delivers to
Total
Status
Paid
{stats.recent_orders.map((order) => (
#{order.number}
{order.customer_name}
{showStore && (
{order.store.name}
)}
{order.state ?? '—'}
{ringgit(order.total, true)}
{order.shipped ? 'Shipped' : 'To ship'}
{when(order.paid_at)}
))}
);
}
/** 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 (
))}
);
}
/** Where the sales chart goes before there are any sales. */
export function EmptySales({ storeUrl }: { storeUrl: string | null }) {
return (
Your sales chart starts with your first order
{storeUrl
? 'Share your store link with friends and customers.'
: 'Publish your store, then share its link with friends and customers.'}