AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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