| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import { useEffect, useState } from 'react';
- import { Spinner } from '@/components/ui/spinner';
- import type { StoreGeneration } from '@/types';
-
- const generateSteps = [
- 'Reading your brand description',
- 'Choosing colours and fonts',
- 'Picking the right sections',
- 'Writing your store text',
- 'Checking the design',
- ];
-
- const secondsPerStep = 12;
-
- /**
- * Shown while the AI is working in the background.
- * The steps are a friendly estimate based on time passed, not exact progress.
- */
- export default function DesignProgress({
- generation,
- }: {
- generation: StoreGeneration;
- }) {
- const startedAt = generation.created_at
- ? new Date(generation.created_at).getTime()
- : null;
- const [now, setNow] = useState(() => Date.now());
-
- useEffect(() => {
- const timer = setInterval(() => setNow(Date.now()), 1000);
-
- return () => clearInterval(timer);
- }, []);
-
- const elapsed = startedAt
- ? Math.max(0, Math.floor((now - startedAt) / 1000))
- : 0;
- const step = Math.min(
- generateSteps.length - 1,
- Math.floor(elapsed / secondsPerStep),
- );
- const isEdit = generation.type === 'edit';
- // A job normally starts within a few seconds. If it is still only queued, nothing is picking it up.
- const waitingTooLong = generation.status === 'queued' && elapsed > 20;
-
- return (
- <div
- className="flex items-start gap-4 rounded-xl border p-6"
- role="status"
- aria-live="polite"
- data-test="design-progress"
- >
- <Spinner className="mt-0.5 size-5 shrink-0" />
- <div className="min-w-0">
- <h3 className="font-medium">
- {isEdit ? 'Making your change' : 'Designing your store'}
- </h3>
- <p className="mt-1 text-sm text-muted-foreground">
- {isEdit
- ? 'This usually takes a few seconds.'
- : `${generateSteps[step]}… This usually takes about a minute.`}
- </p>
- {isEdit && generation.instruction && (
- <p className="mt-2 truncate text-sm">
- “{generation.instruction}”
- </p>
- )}
- {waitingTooLong ? (
- <p
- className="mt-2 text-xs text-amber-700 dark:text-amber-400"
- data-test="worker-hint"
- >
- This has not started yet. The background worker may not
- be running. On a development machine, start it with
- “composer run dev”.
- </p>
- ) : (
- <p className="mt-2 text-xs text-muted-foreground">
- You can leave this page. The work continues in the
- background.
- </p>
- )}
- </div>
- </div>
- );
- }
|