| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- import { Link, router } from '@inertiajs/react';
- import { Package, RefreshCw, Sparkles, Truck, UploadCloud } from 'lucide-react';
- import { useEffect, useState } from 'react';
- import DashboardController from '@/actions/App/Http/Controllers/DashboardController';
- import { actionHref, prefersReducedMotion } from '@/components/overview/format';
- import { cn } from '@/lib/utils';
- import type { StorePulse } from '@/types';
-
- const icons = { ship: Truck, restock: Package, publish: UploadCloud };
-
- /** The note types itself out once, the first time it appears. */
- function useTyped(text: string): string {
- const [shown, setShown] = useState(() =>
- prefersReducedMotion() ? text : '',
- );
-
- useEffect(() => {
- if (prefersReducedMotion()) {
- setShown(text);
-
- return;
- }
-
- let count = 0;
- setShown('');
- const timer = window.setInterval(() => {
- count = Math.min(text.length, count + 2);
- setShown(text.slice(0, count));
-
- if (count >= text.length) {
- window.clearInterval(timer);
- }
- }, 16);
-
- return () => window.clearInterval(timer);
- }, [text]);
-
- return shown;
- }
-
- function Orb() {
- return (
- <div
- className="relative grid size-11 shrink-0 place-items-center rounded-full bg-[#F2B33D]/15 text-[#F2B33D]"
- aria-hidden="true"
- >
- <Sparkles className="size-5" />
- <span className="ov-spin absolute -inset-1 rounded-full border-[2.5px] border-[#F2B33D] border-r-transparent border-b-transparent" />
- </div>
- );
- }
-
- /**
- * The "Store pulse" card: a short AI note on how the stores are doing, with buttons for what to do first.
- * The note is saved for the day, so refreshing the page does not ask the AI again.
- */
- export default function PulseCard({
- pulse,
- store,
- }: {
- pulse: StorePulse;
- store: string | null;
- }) {
- const typed = useTyped(pulse.summary);
- const [writing, setWriting] = useState(false);
- const done = typed.length >= pulse.summary.length;
- const time = pulse.written_at
- ? new Date(pulse.written_at).toLocaleTimeString('en-MY', {
- hour: 'numeric',
- minute: '2-digit',
- })
- : null;
-
- const writeAgain = () => {
- router.post(
- DashboardController.rewritePulse.url(),
- { store },
- {
- preserveScroll: true,
- onStart: () => setWriting(true),
- onFinish: () => setWriting(false),
- },
- );
- };
-
- return (
- <section
- aria-label="Store pulse"
- className="ov-rise grid grid-cols-[auto_minmax(0,1fr)] gap-x-4 gap-y-2 rounded-[20px] bg-(--ov-pulse) p-5 text-[#E7F0EA] sm:p-6"
- style={{ animationDelay: '80ms' }}
- >
- <div className="row-span-3 max-sm:col-span-2 max-sm:row-span-1">
- <Orb />
- </div>
-
- <div className="flex flex-wrap items-start gap-x-4 gap-y-1 max-sm:col-span-2">
- <div className="min-w-0">
- <h2 className="ov-heading text-lg font-bold">
- Store pulse
- </h2>
- <p className="text-xs text-[#E7F0EA]/65">
- {pulse.source === 'ai'
- ? `Written by AI${time ? ` today at ${time}` : ''}`
- : 'Written from your numbers. The AI was not available.'}
- </p>
- </div>
-
- <div className="ml-auto text-right">
- {pulse.rewrites_left > 0 ? (
- <button
- type="button"
- onClick={writeAgain}
- disabled={writing}
- className="inline-flex items-center gap-1.5 rounded-full px-2 py-1 text-sm font-medium text-[#E7F0EA]/85 hover:bg-white/10 hover:text-white disabled:opacity-60"
- >
- <RefreshCw
- className={cn(
- 'size-4',
- writing && 'animate-spin',
- )}
- aria-hidden="true"
- />
- {writing ? 'Writing…' : 'Write again'}
- </button>
- ) : (
- <span className="inline-flex items-center gap-1.5 px-2 py-1 text-sm text-[#E7F0EA]/55">
- <RefreshCw className="size-4" aria-hidden="true" />
- Write again
- </span>
- )}
- <p
- className="px-2 text-xs text-[#E7F0EA]/60 tabular-nums"
- data-test="pulse-rewrites-left"
- >
- {pulse.rewrites_left > 0
- ? `${pulse.rewrites_left} of ${pulse.rewrites_limit} left today`
- : 'None left today. New note tomorrow.'}
- </p>
- </div>
- </div>
-
- <p
- className="min-h-[3.2em] max-w-[70ch] text-base leading-relaxed max-sm:col-span-2"
- aria-live="polite"
- >
- {typed}
- {!done && (
- <span
- className="ov-caret ml-0.5 inline-block h-[1.05em] w-0.5 translate-y-0.5 bg-[#F2B33D]"
- aria-hidden="true"
- />
- )}
- </p>
-
- {pulse.actions.length > 0 && (
- <div className="flex flex-wrap gap-2 max-sm:col-span-2">
- {pulse.actions.map((action) => {
- const Icon = icons[action.kind];
-
- return (
- <Link
- key={action.label}
- href={actionHref(
- action.kind,
- action.store_slug,
- action.product_id,
- )}
- className="inline-flex items-center gap-1.5 rounded-full bg-white/12 px-3.5 py-2 text-sm font-semibold transition hover:-translate-y-px hover:bg-[#F2B33D] hover:text-[#3D2A05]"
- >
- <Icon className="size-4" aria-hidden="true" />
- {action.label}
- </Link>
- );
- })}
- </div>
- )}
- </section>
- );
- }
-
- /** Shown for a moment while today's note loads (or is written for the first time). */
- export function PulseLoading() {
- return (
- <section
- aria-label="Store pulse"
- className="grid grid-cols-[auto_minmax(0,1fr)] gap-x-4 gap-y-3 rounded-[20px] bg-(--ov-pulse) p-5 text-[#E7F0EA] sm:p-6"
- >
- <Orb />
- <div className="grid gap-2.5">
- <p className="ov-heading text-lg font-bold">Store pulse</p>
- <p className="text-sm text-[#E7F0EA]/70">
- Writing today’s note…
- </p>
- <div className="h-3 w-4/5 animate-pulse rounded-full bg-white/10" />
- <div className="h-3 w-3/5 animate-pulse rounded-full bg-white/10" />
- </div>
- </section>
- );
- }
|