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

show.tsx 18KB

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