| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import { Link } from '@inertiajs/react';
- import {
- ReceiptText,
- ShoppingBag,
- TrendingDown,
- TrendingUp,
- Truck,
- } from 'lucide-react';
- import type { ReactNode } from 'react';
- import { useId } from 'react';
- import { actionHref, ringgit } from '@/components/overview/format';
- import { useCountUp } from '@/components/overview/use-count-up';
- import { cn } from '@/lib/utils';
- import type { OverviewStats } from '@/types';
-
- /** A small trend line under a number. */
- function Sparkline({ values, light }: { values: number[]; light?: boolean }) {
- const gradient = useId();
-
- if (values.length < 2) {
- return <div className="h-9" />;
- }
-
- const max = Math.max(...values);
- const min = Math.min(...values);
- const points = values.map(
- (value, i) =>
- [
- (i / (values.length - 1)) * 200,
- 32 - ((value - min) / (max - min || 1)) * 26,
- ] as const,
- );
- const line = points
- .map(([x, y]) => `${x.toFixed(1)},${y.toFixed(1)}`)
- .join(' ');
- const stroke = light ? '#FFFFFF' : 'var(--ov-pandan)';
-
- return (
- <svg
- viewBox="0 0 200 36"
- preserveAspectRatio="none"
- className="mt-2 block h-9 w-full"
- aria-hidden="true"
- >
- <defs>
- <linearGradient id={gradient} x1="0" x2="0" y1="0" y2="1">
- <stop
- offset="0"
- stopColor={stroke}
- stopOpacity={light ? 0.25 : 0.18}
- />
- <stop offset="1" stopColor={stroke} stopOpacity="0" />
- </linearGradient>
- </defs>
- <polygon
- points={`0,36 ${line} 200,36`}
- fill={`url(#${gradient})`}
- />
- <polyline
- points={line}
- fill="none"
- stroke={stroke}
- strokeWidth="2"
- strokeLinejoin="round"
- vectorEffect="non-scaling-stroke"
- />
- </svg>
- );
- }
-
- function Change({
- value,
- suffix,
- light,
- }: {
- value: number | null;
- suffix: string;
- light?: boolean;
- }) {
- if (value === null) {
- return (
- <span className={light ? 'text-white/75' : 'text-(--ov-muted)'}>
- Nothing to compare yet
- </span>
- );
- }
-
- const up = value >= 0;
- const Icon = up ? TrendingUp : TrendingDown;
-
- return (
- <span className="inline-flex flex-wrap items-center gap-1.5">
- <span
- className={cn(
- 'inline-flex items-center gap-1 rounded-full px-2 font-semibold tabular-nums',
- light
- ? up
- ? 'bg-white/16 text-white'
- : 'bg-[#E3573F] text-white'
- : up
- ? 'text-(--ov-pandan-ink)'
- : 'text-(--ov-bunga-ink)',
- )}
- >
- <Icon className="size-3.5" aria-hidden="true" />
- {up ? '+' : ''}
- {value}
- {suffix}
- </span>
- </span>
- );
- }
-
- function Card({
- label,
- icon,
- value,
- footer,
- className,
- delay,
- children,
- }: {
- label: string;
- icon: ReactNode;
- value: string;
- footer: ReactNode;
- className?: string;
- delay: number;
- children?: ReactNode;
- }) {
- return (
- <div
- className={cn(
- 'ov-rise ov-lift flex min-w-0 flex-col rounded-[18px] border border-(--ov-line) bg-(--ov-card) px-5 pt-4 pb-3.5',
- className,
- )}
- style={{ animationDelay: `${delay}ms` }}
- >
- <div className="flex items-center gap-2 text-sm font-medium opacity-80 [&_svg]:size-4">
- {icon}
- {label}
- </div>
- <div className="ov-heading mt-1.5 text-[clamp(1.5rem,2.4vw,2rem)] leading-tight font-extrabold whitespace-nowrap tabular-nums">
- {value}
- </div>
- <div className="mt-0.5 text-sm">{footer}</div>
- {children}
- </div>
- );
- }
-
- /** The four big numbers: sales, paid orders, average order, and orders to ship now. */
- export default function KpiCards({ stats }: { stats: OverviewStats }) {
- const sales = useCountUp(stats.sales);
- const orders = useCountUp(stats.orders);
- const average = useCountUp(stats.average);
- const toShip = useCountUp(stats.to_ship.count);
- const ordersChange = stats.orders - stats.previous_orders;
- const shipStore = stats.attention.find((item) => item.kind === 'ship')
- ?.store?.slug;
-
- return (
- <section
- aria-label="Key numbers"
- className="grid grid-cols-2 gap-3 sm:gap-4 xl:grid-cols-4"
- >
- <Card
- label="Total sales"
- icon={<TrendingUp aria-hidden="true" />}
- value={ringgit(Math.round(sales))}
- className="border-(--ov-hero) bg-(--ov-hero) text-white"
- delay={160}
- footer={
- <span className="flex flex-wrap items-center gap-1.5 text-white/80">
- <Change value={stats.sales_change} suffix="%" light />
- {stats.sales_change !== null && stats.compare_label}
- </span>
- }
- >
- <Sparkline
- values={stats.series.map((point) => point.sales)}
- light
- />
- </Card>
-
- <Card
- label="Paid orders"
- icon={<ReceiptText aria-hidden="true" />}
- value={String(Math.round(orders))}
- delay={220}
- footer={
- <span className="flex flex-wrap items-center gap-1.5 text-(--ov-muted)">
- <Change
- value={
- stats.previous_orders > 0 || stats.orders > 0
- ? ordersChange
- : null
- }
- suffix=""
- />
- {ordersChange < 0
- ? 'fewer than before'
- : 'more than before'}
- </span>
- }
- >
- <Sparkline values={stats.series.map((point) => point.orders)} />
- </Card>
-
- <Card
- label="Average order"
- icon={<ShoppingBag aria-hidden="true" />}
- value={ringgit(Math.round(average * 100) / 100, true)}
- delay={280}
- footer={
- <span className="text-(--ov-muted)">
- {stats.previous_average > 0
- ? `${ringgit(stats.previous_average, true)} before`
- : 'per paid order'}
- </span>
- }
- />
-
- <Card
- label="To ship now"
- icon={<Truck aria-hidden="true" />}
- value={String(Math.round(toShip))}
- className={
- stats.to_ship.count > 0
- ? 'border-transparent bg-(--ov-kunyit-soft) text-(--ov-ink)'
- : undefined
- }
- delay={340}
- footer={
- <span
- className={
- stats.to_ship.count > 0
- ? 'text-(--ov-kunyit-ink)'
- : 'text-(--ov-muted)'
- }
- >
- {stats.to_ship.count === 0
- ? 'All orders are sent'
- : stats.to_ship.oldest_days
- ? `Oldest waited ${stats.to_ship.oldest_days} ${stats.to_ship.oldest_days === 1 ? 'day' : 'days'}`
- : 'Paid today'}
- </span>
- }
- >
- {stats.to_ship.count > 0 && (
- <Link
- href={actionHref('ship', shipStore)}
- className="mt-auto pt-3 text-sm font-semibold text-(--ov-kunyit-ink) hover:underline"
- >
- Ship orders →
- </Link>
- )}
- </Card>
- </section>
- );
- }
|