AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

show.tsx 16KB

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