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 ( ); } /** * 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 (

Store pulse

{pulse.source === 'ai' ? `Written by AI${time ? ` today at ${time}` : ''}` : 'Written from your numbers. The AI was not available.'}

{pulse.rewrites_left > 0 ? ( ) : ( )}

{pulse.rewrites_left > 0 ? `${pulse.rewrites_left} of ${pulse.rewrites_limit} left today` : 'None left today. New note tomorrow.'}

{typed} {!done && (

{pulse.actions.length > 0 && (
{pulse.actions.map((action) => { const Icon = icons[action.kind]; return (
)}
); } /** Shown for a moment while today's note loads (or is written for the first time). */ export function PulseLoading() { return (

Store pulse

Writing today’s note…

); }