AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

show.tsx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import { Form, Head, router } from '@inertiajs/react';
  2. import {
  3. CircleAlert,
  4. ExternalLink,
  5. Monitor,
  6. RefreshCw,
  7. Smartphone,
  8. Sparkles,
  9. } from 'lucide-react';
  10. import { useEffect, useState } from 'react';
  11. import StoreDesignController from '@/actions/App/Http/Controllers/StoreDesignController';
  12. import StoreGenerationController from '@/actions/App/Http/Controllers/StoreGenerationController';
  13. import DesignChat from '@/components/stores/design-chat';
  14. import DesignProgress from '@/components/stores/design-progress';
  15. import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
  16. import { Badge } from '@/components/ui/badge';
  17. import { Button } from '@/components/ui/button';
  18. import {
  19. Card,
  20. CardContent,
  21. CardDescription,
  22. CardHeader,
  23. CardTitle,
  24. } from '@/components/ui/card';
  25. import {
  26. Dialog,
  27. DialogClose,
  28. DialogContent,
  29. DialogDescription,
  30. DialogFooter,
  31. DialogHeader,
  32. DialogTitle,
  33. DialogTrigger,
  34. } from '@/components/ui/dialog';
  35. import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group';
  36. import { cn } from '@/lib/utils';
  37. import { dashboard } from '@/routes';
  38. import type { Store, StoreGeneration, Usage } from '@/types';
  39. type Device = 'desktop' | 'mobile';
  40. type Props = {
  41. store: Store;
  42. generation: StoreGeneration | null;
  43. edits: StoreGeneration[];
  44. usage: Usage;
  45. };
  46. export default function ShowStore({ store, generation, edits, usage }: Props) {
  47. const [device, setDevice] = useState<Device>('desktop');
  48. const [confirmOpen, setConfirmOpen] = useState(false);
  49. const previewUrl = StoreDesignController.preview.url(store.slug);
  50. const busy =
  51. generation?.status === 'queued' || generation?.status === 'running';
  52. const failed = generation?.status === 'failed' ? generation : null;
  53. const regenerationsLeft = Math.max(
  54. 0,
  55. usage.regenerations.limit - usage.regenerations.used,
  56. );
  57. // While the AI is working, ask the server for fresh data every 2 seconds.
  58. useEffect(() => {
  59. if (!busy) {
  60. return;
  61. }
  62. const timer = setInterval(() => {
  63. router.reload({ only: ['store', 'generation', 'edits', 'usage'] });
  64. }, 2000);
  65. return () => clearInterval(timer);
  66. }, [busy]);
  67. return (
  68. <>
  69. <Head title={store.name} />
  70. <div className="mx-auto w-full max-w-6xl space-y-6 p-4">
  71. <div className="flex flex-wrap items-start justify-between gap-3">
  72. <div>
  73. <h1 className="text-xl font-semibold">{store.name}</h1>
  74. <p className="text-sm text-muted-foreground">
  75. {store.category_label} · {store.language_label}
  76. </p>
  77. </div>
  78. <Badge
  79. variant={
  80. store.status === 'published'
  81. ? 'default'
  82. : 'secondary'
  83. }
  84. >
  85. {store.status_label}
  86. </Badge>
  87. </div>
  88. <Card>
  89. <CardHeader>
  90. <CardTitle>Brand description</CardTitle>
  91. <CardDescription>
  92. This is what the AI will use to design your store.
  93. </CardDescription>
  94. </CardHeader>
  95. <CardContent>
  96. <p className="text-sm whitespace-pre-line">
  97. {store.description}
  98. </p>
  99. </CardContent>
  100. </Card>
  101. {store.has_design ? (
  102. <section className="space-y-3">
  103. <div className="flex flex-wrap items-center justify-between gap-3">
  104. <h2 className="font-medium">Store design</h2>
  105. <div className="flex items-center gap-2">
  106. <ToggleGroup
  107. type="single"
  108. variant="outline"
  109. size="sm"
  110. value={device}
  111. onValueChange={(value) => {
  112. if (value) {
  113. setDevice(value as Device);
  114. }
  115. }}
  116. aria-label="Preview size"
  117. >
  118. <ToggleGroupItem
  119. value="desktop"
  120. aria-label="Desktop preview"
  121. >
  122. <Monitor />
  123. </ToggleGroupItem>
  124. <ToggleGroupItem
  125. value="mobile"
  126. aria-label="Mobile preview"
  127. >
  128. <Smartphone />
  129. </ToggleGroupItem>
  130. </ToggleGroup>
  131. <Button variant="outline" size="sm" asChild>
  132. <a
  133. href={previewUrl}
  134. target="_blank"
  135. rel="noreferrer"
  136. >
  137. <ExternalLink />
  138. Open full page
  139. </a>
  140. </Button>
  141. <Dialog
  142. open={confirmOpen}
  143. onOpenChange={setConfirmOpen}
  144. >
  145. <DialogTrigger asChild>
  146. <Button
  147. variant="outline"
  148. size="sm"
  149. disabled={busy}
  150. data-test="regenerate-button"
  151. >
  152. <RefreshCw />
  153. Regenerate
  154. </Button>
  155. </DialogTrigger>
  156. <DialogContent>
  157. <DialogHeader>
  158. <DialogTitle>
  159. Design this store again?
  160. </DialogTitle>
  161. <DialogDescription>
  162. The AI will create a completely
  163. new design. Your current design
  164. and every change you made to it
  165. will be replaced. You have{' '}
  166. {regenerationsLeft} of{' '}
  167. {usage.regenerations.limit}{' '}
  168. regenerations left
  169. {usage.period === 'month'
  170. ? ' this month'
  171. : ''}
  172. .
  173. </DialogDescription>
  174. </DialogHeader>
  175. <DialogFooter>
  176. <DialogClose asChild>
  177. <Button variant="outline">
  178. Cancel
  179. </Button>
  180. </DialogClose>
  181. <Form
  182. {...StoreGenerationController.generate.form(
  183. store.slug,
  184. )}
  185. onSuccess={() =>
  186. setConfirmOpen(false)
  187. }
  188. >
  189. {({ processing }) => (
  190. <Button
  191. disabled={
  192. processing ||
  193. regenerationsLeft ===
  194. 0
  195. }
  196. data-test="confirm-regenerate-button"
  197. >
  198. Regenerate
  199. </Button>
  200. )}
  201. </Form>
  202. </DialogFooter>
  203. </DialogContent>
  204. </Dialog>
  205. </div>
  206. </div>
  207. {busy && generation && (
  208. <DesignProgress generation={generation} />
  209. )}
  210. {failed && <FailedNotice generation={failed} />}
  211. <div className="flex justify-center rounded-xl border bg-muted/40 p-3">
  212. <iframe
  213. key={store.design_version ?? 0}
  214. title={`Preview of ${store.name}`}
  215. src={previewUrl}
  216. className={cn(
  217. 'h-[70vh] min-h-[480px] rounded-lg border bg-white transition-[width]',
  218. device === 'mobile'
  219. ? 'w-[390px] max-w-full'
  220. : 'w-full',
  221. )}
  222. />
  223. </div>
  224. <DesignChat
  225. store={store}
  226. edits={edits}
  227. usage={usage.edits}
  228. busy={busy}
  229. />
  230. </section>
  231. ) : busy && generation ? (
  232. <DesignProgress generation={generation} />
  233. ) : (
  234. <div className="space-y-3">
  235. {failed && <FailedNotice generation={failed} />}
  236. <div className="flex flex-wrap items-center justify-between gap-4 rounded-xl border border-dashed p-6">
  237. <div className="flex items-start gap-3">
  238. <Sparkles className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
  239. <div>
  240. <h2 className="font-medium">
  241. Store design
  242. </h2>
  243. <p className="mt-1 max-w-md text-sm text-muted-foreground">
  244. Let the AI design your store from the
  245. brand description above. It takes about
  246. a minute, and your first design is free.
  247. </p>
  248. </div>
  249. </div>
  250. <Form
  251. {...StoreGenerationController.generate.form(
  252. store.slug,
  253. )}
  254. >
  255. {({ processing }) => (
  256. <Button
  257. disabled={processing}
  258. data-test="generate-store-button"
  259. >
  260. <Sparkles />
  261. {failed
  262. ? 'Try again'
  263. : 'Generate my store'}
  264. </Button>
  265. )}
  266. </Form>
  267. </div>
  268. </div>
  269. )}
  270. </div>
  271. </>
  272. );
  273. }
  274. function FailedNotice({ generation }: { generation: StoreGeneration }) {
  275. return (
  276. <Alert variant="destructive" data-test="generation-failed">
  277. <CircleAlert />
  278. <AlertTitle>
  279. {generation.type === 'edit'
  280. ? 'That change did not work'
  281. : 'The design did not work'}
  282. </AlertTitle>
  283. <AlertDescription>
  284. {generation.error ?? 'Please try again.'} Your store was not
  285. changed, and this attempt does not count towards your plan.
  286. </AlertDescription>
  287. </Alert>
  288. );
  289. }
  290. ShowStore.layout = {
  291. breadcrumbs: [
  292. {
  293. title: 'Dashboard',
  294. href: dashboard(),
  295. },
  296. {
  297. title: 'Store',
  298. href: '#',
  299. },
  300. ],
  301. };