AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

show.tsx 17KB

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