| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- import { Form, Head, router } from '@inertiajs/react';
- import {
- CircleAlert,
- ExternalLink,
- Monitor,
- RefreshCw,
- Smartphone,
- Sparkles,
- } from 'lucide-react';
- import { useEffect, useState } from 'react';
- import StoreDesignController from '@/actions/App/Http/Controllers/StoreDesignController';
- import StoreGenerationController from '@/actions/App/Http/Controllers/StoreGenerationController';
- import DesignChat from '@/components/stores/design-chat';
- import DesignProgress from '@/components/stores/design-progress';
- import PublishPanel from '@/components/stores/publish-panel';
- import StoreNav from '@/components/stores/store-nav';
- import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
- import { Badge } from '@/components/ui/badge';
- import { Button } from '@/components/ui/button';
- import {
- Card,
- CardContent,
- CardDescription,
- CardHeader,
- CardTitle,
- } from '@/components/ui/card';
- import {
- Dialog,
- DialogClose,
- DialogContent,
- DialogDescription,
- DialogFooter,
- DialogHeader,
- DialogTitle,
- DialogTrigger,
- } from '@/components/ui/dialog';
- import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group';
- import { cn } from '@/lib/utils';
- import { dashboard } from '@/routes';
- import type { Store, StoreGeneration, Usage } from '@/types';
-
- type Device = 'desktop' | 'mobile';
-
- type Props = {
- store: Store;
- generation: StoreGeneration | null;
- edits: StoreGeneration[];
- usage: Usage;
- };
-
- export default function ShowStore({ store, generation, edits, usage }: Props) {
- const [device, setDevice] = useState<Device>('desktop');
- const [confirmOpen, setConfirmOpen] = useState(false);
- const previewUrl = StoreDesignController.preview[
- '/stores/{slug}/preview'
- ].url(store.slug);
-
- const busy =
- generation?.status === 'queued' || generation?.status === 'running';
- const failed = generation?.status === 'failed' ? generation : null;
- const regenerationsLeft = Math.max(
- 0,
- usage.regenerations.limit - usage.regenerations.used,
- );
-
- // While the AI is working, ask the server for fresh data every 2 seconds.
- useEffect(() => {
- if (!busy) {
- return;
- }
-
- const timer = setInterval(() => {
- router.reload({ only: ['store', 'generation', 'edits', 'usage'] });
- }, 2000);
-
- return () => clearInterval(timer);
- }, [busy]);
-
- return (
- <>
- <Head title={store.name} />
-
- <div className="mx-auto w-full max-w-6xl space-y-6 p-4">
- <div className="flex flex-wrap items-start justify-between gap-3">
- <div>
- <h1 className="text-xl font-semibold">{store.name}</h1>
- <p className="text-sm text-muted-foreground">
- {store.category_label} · {store.language_label}
- </p>
- </div>
- <Badge
- variant={
- store.status === 'published'
- ? 'default'
- : 'secondary'
- }
- >
- {store.status_label}
- </Badge>
- </div>
-
- <StoreNav slug={store.slug} current="design" />
-
- {store.has_design && <PublishPanel store={store} busy={busy} />}
-
- <Card>
- <CardHeader>
- <CardTitle>Brand description</CardTitle>
- <CardDescription>
- This is what the AI will use to design your store.
- </CardDescription>
- </CardHeader>
- <CardContent>
- <p className="text-sm whitespace-pre-line">
- {store.description}
- </p>
- </CardContent>
- </Card>
-
- {store.has_design ? (
- <section className="space-y-3">
- <div className="flex flex-wrap items-center justify-between gap-3">
- <div>
- <h2 className="font-medium">Store design</h2>
- {store.is_published && (
- <p className="text-xs text-muted-foreground">
- This preview shows your working design,
- including changes that are not published
- yet.
- </p>
- )}
- </div>
-
- <div className="flex items-center gap-2">
- <ToggleGroup
- type="single"
- variant="outline"
- size="sm"
- value={device}
- onValueChange={(value) => {
- if (value) {
- setDevice(value as Device);
- }
- }}
- aria-label="Preview size"
- >
- <ToggleGroupItem
- value="desktop"
- aria-label="Desktop preview"
- >
- <Monitor />
- </ToggleGroupItem>
- <ToggleGroupItem
- value="mobile"
- aria-label="Mobile preview"
- >
- <Smartphone />
- </ToggleGroupItem>
- </ToggleGroup>
-
- <Button variant="outline" size="sm" asChild>
- <a
- href={previewUrl}
- target="_blank"
- rel="noreferrer"
- >
- <ExternalLink />
- Open full page
- </a>
- </Button>
-
- <Dialog
- open={confirmOpen}
- onOpenChange={setConfirmOpen}
- >
- <DialogTrigger asChild>
- <Button
- variant="outline"
- size="sm"
- disabled={busy}
- data-test="regenerate-button"
- >
- <RefreshCw />
- Regenerate
- </Button>
- </DialogTrigger>
- <DialogContent>
- <DialogHeader>
- <DialogTitle>
- Design this store again?
- </DialogTitle>
- <DialogDescription>
- The AI will create a completely
- new design. Your current design
- and every change you made to it
- will be replaced. You have{' '}
- {regenerationsLeft} of{' '}
- {usage.regenerations.limit}{' '}
- regenerations left
- {usage.period === 'month'
- ? ' this month'
- : ''}
- .
- </DialogDescription>
- </DialogHeader>
- <DialogFooter>
- <DialogClose asChild>
- <Button variant="outline">
- Cancel
- </Button>
- </DialogClose>
- <Form
- {...StoreGenerationController.generate.form(
- store.slug,
- )}
- onSuccess={() =>
- setConfirmOpen(false)
- }
- >
- {({ processing }) => (
- <Button
- disabled={
- processing ||
- regenerationsLeft ===
- 0
- }
- data-test="confirm-regenerate-button"
- >
- Regenerate
- </Button>
- )}
- </Form>
- </DialogFooter>
- </DialogContent>
- </Dialog>
- </div>
- </div>
-
- {busy && generation && (
- <DesignProgress generation={generation} />
- )}
-
- {failed && <FailedNotice generation={failed} />}
-
- <div className="flex justify-center rounded-xl border bg-muted/40 p-3">
- <iframe
- key={store.design_version ?? 0}
- title={`Preview of ${store.name}`}
- src={previewUrl}
- className={cn(
- 'h-[70vh] min-h-[480px] rounded-lg border bg-white transition-[width]',
- device === 'mobile'
- ? 'w-[390px] max-w-full'
- : 'w-full',
- )}
- />
- </div>
-
- <DesignChat
- store={store}
- edits={edits}
- usage={usage.edits}
- busy={busy}
- />
- </section>
- ) : busy && generation ? (
- <DesignProgress generation={generation} />
- ) : (
- <div className="space-y-3">
- {failed && <FailedNotice generation={failed} />}
-
- <div className="flex flex-wrap items-center justify-between gap-4 rounded-xl border border-dashed p-6">
- <div className="flex items-start gap-3">
- <Sparkles className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
- <div>
- <h2 className="font-medium">
- Store design
- </h2>
- <p className="mt-1 max-w-md text-sm text-muted-foreground">
- Let the AI design your store from the
- brand description above. It takes about
- a minute, and your first design is free.
- </p>
- </div>
- </div>
-
- <Form
- {...StoreGenerationController.generate.form(
- store.slug,
- )}
- >
- {({ processing }) => (
- <Button
- disabled={processing}
- data-test="generate-store-button"
- >
- <Sparkles />
- {failed
- ? 'Try again'
- : 'Generate my store'}
- </Button>
- )}
- </Form>
- </div>
- </div>
- )}
- </div>
- </>
- );
- }
-
- function FailedNotice({ generation }: { generation: StoreGeneration }) {
- return (
- <Alert variant="destructive" data-test="generation-failed">
- <CircleAlert />
- <AlertTitle>
- {generation.type === 'edit'
- ? 'That change did not work'
- : 'The design did not work'}
- </AlertTitle>
- <AlertDescription>
- {generation.error ?? 'Please try again.'} Your store was not
- changed, and this attempt does not count towards your plan.
- </AlertDescription>
- </Alert>
- );
- }
-
- ShowStore.layout = {
- breadcrumbs: [
- {
- title: 'Dashboard',
- href: dashboard(),
- },
- {
- title: 'Store',
- href: '#',
- },
- ],
- };
|