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.

kpi-cards.tsx 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { Link } from '@inertiajs/react';
  2. import {
  3. ReceiptText,
  4. ShoppingBag,
  5. TrendingDown,
  6. TrendingUp,
  7. Truck,
  8. } from 'lucide-react';
  9. import type { ReactNode } from 'react';
  10. import { useId } from 'react';
  11. import { actionHref, ringgit } from '@/components/overview/format';
  12. import { useCountUp } from '@/components/overview/use-count-up';
  13. import { cn } from '@/lib/utils';
  14. import type { OverviewStats } from '@/types';
  15. /** A small trend line under a number. */
  16. function Sparkline({ values, light }: { values: number[]; light?: boolean }) {
  17. const gradient = useId();
  18. if (values.length < 2) {
  19. return <div className="h-9" />;
  20. }
  21. const max = Math.max(...values);
  22. const min = Math.min(...values);
  23. const points = values.map(
  24. (value, i) =>
  25. [
  26. (i / (values.length - 1)) * 200,
  27. 32 - ((value - min) / (max - min || 1)) * 26,
  28. ] as const,
  29. );
  30. const line = points
  31. .map(([x, y]) => `${x.toFixed(1)},${y.toFixed(1)}`)
  32. .join(' ');
  33. const stroke = light ? '#FFFFFF' : 'var(--ov-pandan)';
  34. return (
  35. <svg
  36. viewBox="0 0 200 36"
  37. preserveAspectRatio="none"
  38. className="mt-2 block h-9 w-full"
  39. aria-hidden="true"
  40. >
  41. <defs>
  42. <linearGradient id={gradient} x1="0" x2="0" y1="0" y2="1">
  43. <stop
  44. offset="0"
  45. stopColor={stroke}
  46. stopOpacity={light ? 0.25 : 0.18}
  47. />
  48. <stop offset="1" stopColor={stroke} stopOpacity="0" />
  49. </linearGradient>
  50. </defs>
  51. <polygon
  52. points={`0,36 ${line} 200,36`}
  53. fill={`url(#${gradient})`}
  54. />
  55. <polyline
  56. points={line}
  57. fill="none"
  58. stroke={stroke}
  59. strokeWidth="2"
  60. strokeLinejoin="round"
  61. vectorEffect="non-scaling-stroke"
  62. />
  63. </svg>
  64. );
  65. }
  66. function Change({
  67. value,
  68. suffix,
  69. light,
  70. }: {
  71. value: number | null;
  72. suffix: string;
  73. light?: boolean;
  74. }) {
  75. if (value === null) {
  76. return (
  77. <span className={light ? 'text-white/75' : 'text-(--ov-muted)'}>
  78. Nothing to compare yet
  79. </span>
  80. );
  81. }
  82. const up = value >= 0;
  83. const Icon = up ? TrendingUp : TrendingDown;
  84. return (
  85. <span className="inline-flex flex-wrap items-center gap-1.5">
  86. <span
  87. className={cn(
  88. 'inline-flex items-center gap-1 rounded-full px-2 font-semibold tabular-nums',
  89. light
  90. ? up
  91. ? 'bg-white/16 text-white'
  92. : 'bg-[#E3573F] text-white'
  93. : up
  94. ? 'text-(--ov-pandan-ink)'
  95. : 'text-(--ov-bunga-ink)',
  96. )}
  97. >
  98. <Icon className="size-3.5" aria-hidden="true" />
  99. {up ? '+' : ''}
  100. {value}
  101. {suffix}
  102. </span>
  103. </span>
  104. );
  105. }
  106. function Card({
  107. label,
  108. icon,
  109. value,
  110. footer,
  111. className,
  112. delay,
  113. children,
  114. }: {
  115. label: string;
  116. icon: ReactNode;
  117. value: string;
  118. footer: ReactNode;
  119. className?: string;
  120. delay: number;
  121. children?: ReactNode;
  122. }) {
  123. return (
  124. <div
  125. className={cn(
  126. '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',
  127. className,
  128. )}
  129. style={{ animationDelay: `${delay}ms` }}
  130. >
  131. <div className="flex items-center gap-2 text-sm font-medium opacity-80 [&_svg]:size-4">
  132. {icon}
  133. {label}
  134. </div>
  135. <div className="ov-heading mt-1.5 text-[clamp(1.5rem,2.4vw,2rem)] leading-tight font-extrabold whitespace-nowrap tabular-nums">
  136. {value}
  137. </div>
  138. <div className="mt-0.5 text-sm">{footer}</div>
  139. {children}
  140. </div>
  141. );
  142. }
  143. /** The four big numbers: sales, paid orders, average order, and orders to ship now. */
  144. export default function KpiCards({ stats }: { stats: OverviewStats }) {
  145. const sales = useCountUp(stats.sales);
  146. const orders = useCountUp(stats.orders);
  147. const average = useCountUp(stats.average);
  148. const toShip = useCountUp(stats.to_ship.count);
  149. const ordersChange = stats.orders - stats.previous_orders;
  150. const shipStore = stats.attention.find((item) => item.kind === 'ship')
  151. ?.store?.slug;
  152. return (
  153. <section
  154. aria-label="Key numbers"
  155. className="grid grid-cols-2 gap-3 sm:gap-4 xl:grid-cols-4"
  156. >
  157. <Card
  158. label="Total sales"
  159. icon={<TrendingUp aria-hidden="true" />}
  160. value={ringgit(Math.round(sales))}
  161. className="border-(--ov-hero) bg-(--ov-hero) text-white"
  162. delay={160}
  163. footer={
  164. <span className="flex flex-wrap items-center gap-1.5 text-white/80">
  165. <Change value={stats.sales_change} suffix="%" light />
  166. {stats.sales_change !== null && stats.compare_label}
  167. </span>
  168. }
  169. >
  170. <Sparkline
  171. values={stats.series.map((point) => point.sales)}
  172. light
  173. />
  174. </Card>
  175. <Card
  176. label="Paid orders"
  177. icon={<ReceiptText aria-hidden="true" />}
  178. value={String(Math.round(orders))}
  179. delay={220}
  180. footer={
  181. <span className="flex flex-wrap items-center gap-1.5 text-(--ov-muted)">
  182. <Change
  183. value={
  184. stats.previous_orders > 0 || stats.orders > 0
  185. ? ordersChange
  186. : null
  187. }
  188. suffix=""
  189. />
  190. {ordersChange < 0
  191. ? 'fewer than before'
  192. : 'more than before'}
  193. </span>
  194. }
  195. >
  196. <Sparkline values={stats.series.map((point) => point.orders)} />
  197. </Card>
  198. <Card
  199. label="Average order"
  200. icon={<ShoppingBag aria-hidden="true" />}
  201. value={ringgit(Math.round(average * 100) / 100, true)}
  202. delay={280}
  203. footer={
  204. <span className="text-(--ov-muted)">
  205. {stats.previous_average > 0
  206. ? `${ringgit(stats.previous_average, true)} before`
  207. : 'per paid order'}
  208. </span>
  209. }
  210. />
  211. <Card
  212. label="To ship now"
  213. icon={<Truck aria-hidden="true" />}
  214. value={String(Math.round(toShip))}
  215. className={
  216. stats.to_ship.count > 0
  217. ? 'border-transparent bg-(--ov-kunyit-soft) text-(--ov-ink)'
  218. : undefined
  219. }
  220. delay={340}
  221. footer={
  222. <span
  223. className={
  224. stats.to_ship.count > 0
  225. ? 'text-(--ov-kunyit-ink)'
  226. : 'text-(--ov-muted)'
  227. }
  228. >
  229. {stats.to_ship.count === 0
  230. ? 'All orders are sent'
  231. : stats.to_ship.oldest_days
  232. ? `Oldest waited ${stats.to_ship.oldest_days} ${stats.to_ship.oldest_days === 1 ? 'day' : 'days'}`
  233. : 'Paid today'}
  234. </span>
  235. }
  236. >
  237. {stats.to_ship.count > 0 && (
  238. <Link
  239. href={actionHref('ship', shipStore)}
  240. className="mt-auto pt-3 text-sm font-semibold text-(--ov-kunyit-ink) hover:underline"
  241. >
  242. Ship orders →
  243. </Link>
  244. )}
  245. </Card>
  246. </section>
  247. );
  248. }