AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

show.tsx 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. import { Form, Head, router } from '@inertiajs/react';
  2. import {
  3. CircleAlert,
  4. ExternalLink,
  5. Monitor,
  6. RefreshCw,
  7. Smartphone,
  8. Sparkles,
  9. SquareDashedMousePointer,
  10. } from 'lucide-react';
  11. import { useEffect, useRef, useState } from 'react';
  12. import StoreDesignController from '@/actions/App/Http/Controllers/StoreDesignController';
  13. import StoreGenerationController from '@/actions/App/Http/Controllers/StoreGenerationController';
  14. import DesignChat from '@/components/stores/design-chat';
  15. import FeaturedPicker from '@/components/stores/featured-picker';
  16. import HeroPicker from '@/components/stores/hero-picker';
  17. import BentoBox from '@/components/stores/bento-box';
  18. import type { LogoSettings } from '@/components/stores/logo-box';
  19. import { LogoBox } from '@/components/stores/logo-box';
  20. import LayoutPicker from '@/components/stores/layout-picker';
  21. import VideoBox from '@/components/stores/video-box';
  22. import StylePicker from '@/components/stores/style-picker';
  23. import DesignProgress from '@/components/stores/design-progress';
  24. import {
  25. PreviewHighlightContext,
  26. usePreviewHighlighter,
  27. } from '@/components/stores/preview-highlight';
  28. import { startPicker } from '@/components/stores/preview-picker';
  29. import type { PreviewSelection } from '@/components/stores/preview-picker';
  30. import PublishPanel from '@/components/stores/publish-panel';
  31. import StoreNav from '@/components/stores/store-nav';
  32. import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
  33. import { Badge } from '@/components/ui/badge';
  34. import { Button } from '@/components/ui/button';
  35. import {
  36. Card,
  37. CardContent,
  38. CardDescription,
  39. CardHeader,
  40. CardTitle,
  41. } from '@/components/ui/card';
  42. import {
  43. Dialog,
  44. DialogClose,
  45. DialogContent,
  46. DialogDescription,
  47. DialogFooter,
  48. DialogHeader,
  49. DialogTitle,
  50. DialogTrigger,
  51. } from '@/components/ui/dialog';
  52. import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group';
  53. import { cn } from '@/lib/utils';
  54. import { dashboard } from '@/routes';
  55. import type {
  56. FeaturedSection,
  57. HeroChoice,
  58. HeroSectionInfo,
  59. BentoSectionInfo,
  60. HomeSectionInfo,
  61. LayoutSection,
  62. LinkSuggestion,
  63. ProductOption,
  64. VideoSectionInfo,
  65. StoreStyleInfo,
  66. StylePreset,
  67. Store,
  68. StoreGeneration,
  69. Usage,
  70. } from '@/types';
  71. type Device = 'desktop' | 'mobile';
  72. type Props = {
  73. store: Store;
  74. generation: StoreGeneration | null;
  75. edits: StoreGeneration[];
  76. usage: Usage;
  77. featuredSections: FeaturedSection[];
  78. productOptions: ProductOption[];
  79. heroSections: HeroSectionInfo[];
  80. heroChoices: HeroChoice[];
  81. style: StoreStyleInfo | null;
  82. stylePresets: StylePreset[];
  83. layoutSections: LayoutSection[];
  84. layoutOptions: Record<string, string[]>;
  85. videoSections: VideoSectionInfo[];
  86. bentoSections: BentoSectionInfo[];
  87. homeSections: HomeSectionInfo[];
  88. linkSuggestions: LinkSuggestion[];
  89. logo: LogoSettings;
  90. };
  91. export default function ShowStore({
  92. store,
  93. generation,
  94. edits,
  95. usage,
  96. featuredSections,
  97. productOptions,
  98. heroSections,
  99. heroChoices,
  100. style,
  101. stylePresets,
  102. layoutSections,
  103. layoutOptions,
  104. videoSections,
  105. bentoSections,
  106. homeSections,
  107. linkSuggestions,
  108. logo,
  109. }: Props) {
  110. const [device, setDevice] = useState<Device>('desktop');
  111. const [confirmOpen, setConfirmOpen] = useState(false);
  112. const previewUrl = StoreDesignController.preview[
  113. '/stores/{slug}/preview'
  114. ].url(store.slug);
  115. const frameRef = useRef<HTMLIFrameElement>(null);
  116. const highlighter = usePreviewHighlighter({
  117. frameRef,
  118. slug: store.slug,
  119. homeUrl: previewUrl,
  120. });
  121. // "Select to edit": pick a part of the preview, then Ask the AI opens to change it.
  122. const [selecting, setSelecting] = useState(false);
  123. const [selection, setSelection] = useState<PreviewSelection | null>(null);
  124. // Counts preview loads, so the picker starts again after the preview reloads.
  125. const [frameLoads, setFrameLoads] = useState(0);
  126. useEffect(() => {
  127. const frame = frameRef.current;
  128. if (!selecting || !frame) {
  129. return;
  130. }
  131. const stop = startPicker(
  132. frame,
  133. (picked) => {
  134. setSelection(picked);
  135. setSelecting(false);
  136. },
  137. () => setSelecting(false),
  138. );
  139. const onKey = (event: KeyboardEvent) => {
  140. if (event.key === 'Escape') {
  141. setSelecting(false);
  142. }
  143. };
  144. window.addEventListener('keydown', onKey);
  145. return () => {
  146. stop();
  147. window.removeEventListener('keydown', onKey);
  148. };
  149. }, [selecting, frameLoads, store.design_version]);
  150. const busy =
  151. generation?.status === 'queued' || generation?.status === 'running';
  152. const failed = generation?.status === 'failed' ? generation : null;
  153. const regenerationsLeft = Math.max(
  154. 0,
  155. usage.regenerations.limit - usage.regenerations.used,
  156. );
  157. // While the AI is working, ask the server for fresh data every 2 seconds.
  158. useEffect(() => {
  159. if (!busy) {
  160. return;
  161. }
  162. const timer = setInterval(() => {
  163. router.reload({
  164. only: [
  165. 'store',
  166. 'generation',
  167. 'edits',
  168. 'usage',
  169. 'featuredSections',
  170. 'heroSections',
  171. 'style',
  172. 'layoutSections',
  173. 'videoSections',
  174. 'bentoSections',
  175. 'homeSections',
  176. ],
  177. });
  178. }, 2000);
  179. return () => clearInterval(timer);
  180. }, [busy]);
  181. return (
  182. <>
  183. <Head title={store.name} />
  184. <div
  185. className={cn(
  186. 'mx-auto w-full max-w-6xl space-y-6 p-4',
  187. // Room at the bottom so the "Ask the AI" button never covers the last box.
  188. store.has_design && 'pb-24',
  189. )}
  190. >
  191. <div className="flex flex-wrap items-start justify-between gap-3">
  192. <div>
  193. <h1 className="text-xl font-semibold">{store.name}</h1>
  194. <p className="text-sm text-muted-foreground">
  195. {store.category_label} · {store.language_label}
  196. </p>
  197. </div>
  198. <Badge
  199. variant={
  200. store.status === 'published'
  201. ? 'default'
  202. : 'secondary'
  203. }
  204. >
  205. {store.status_label}
  206. </Badge>
  207. </div>
  208. <StoreNav slug={store.slug} current="design" />
  209. {store.has_design && <PublishPanel store={store} busy={busy} />}
  210. <Card>
  211. <CardHeader>
  212. <CardTitle>Brand description</CardTitle>
  213. <CardDescription>
  214. This is what the AI will use to design your store.
  215. </CardDescription>
  216. </CardHeader>
  217. <CardContent>
  218. <p className="text-sm whitespace-pre-line">
  219. {store.description}
  220. </p>
  221. </CardContent>
  222. </Card>
  223. {store.has_design ? (
  224. <PreviewHighlightContext.Provider value={highlighter}>
  225. <section className="space-y-3">
  226. <div className="flex flex-wrap items-center justify-between gap-3">
  227. <div>
  228. <h2 className="font-medium">
  229. Store design
  230. </h2>
  231. {store.is_published && (
  232. <p className="text-xs text-muted-foreground">
  233. This preview shows your working
  234. design, including changes that are
  235. not published yet.
  236. </p>
  237. )}
  238. </div>
  239. <div className="flex flex-wrap items-center gap-2">
  240. <Button
  241. variant={
  242. selecting ? 'default' : 'outline'
  243. }
  244. size="sm"
  245. aria-pressed={selecting}
  246. onClick={() =>
  247. setSelecting((value) => !value)
  248. }
  249. data-test="select-to-edit-button"
  250. >
  251. <SquareDashedMousePointer />
  252. {selecting
  253. ? 'Stop selecting'
  254. : 'Select to edit'}
  255. </Button>
  256. <ToggleGroup
  257. type="single"
  258. variant="outline"
  259. size="sm"
  260. value={device}
  261. onValueChange={(value) => {
  262. if (value) {
  263. setDevice(value as Device);
  264. }
  265. }}
  266. aria-label="Preview size"
  267. >
  268. <ToggleGroupItem
  269. value="desktop"
  270. aria-label="Desktop preview"
  271. >
  272. <Monitor />
  273. </ToggleGroupItem>
  274. <ToggleGroupItem
  275. value="mobile"
  276. aria-label="Mobile preview"
  277. >
  278. <Smartphone />
  279. </ToggleGroupItem>
  280. </ToggleGroup>
  281. <Button variant="outline" size="sm" asChild>
  282. <a
  283. href={previewUrl}
  284. target="_blank"
  285. rel="noreferrer"
  286. >
  287. <ExternalLink />
  288. Open full page
  289. </a>
  290. </Button>
  291. <Dialog
  292. open={confirmOpen}
  293. onOpenChange={setConfirmOpen}
  294. >
  295. <DialogTrigger asChild>
  296. <Button
  297. variant="outline"
  298. size="sm"
  299. disabled={busy}
  300. data-test="regenerate-button"
  301. >
  302. <RefreshCw />
  303. Regenerate
  304. </Button>
  305. </DialogTrigger>
  306. <DialogContent>
  307. <DialogHeader>
  308. <DialogTitle>
  309. Design this store again?
  310. </DialogTitle>
  311. <DialogDescription>
  312. The AI will create a
  313. completely new design. Your
  314. current design and every
  315. change you made to it will
  316. be replaced. You have{' '}
  317. {regenerationsLeft} of{' '}
  318. {usage.regenerations.limit}{' '}
  319. regenerations left
  320. {usage.period === 'month'
  321. ? ' this month'
  322. : ''}
  323. .
  324. </DialogDescription>
  325. </DialogHeader>
  326. <DialogFooter>
  327. <DialogClose asChild>
  328. <Button variant="outline">
  329. Cancel
  330. </Button>
  331. </DialogClose>
  332. <Form
  333. {...StoreGenerationController.generate.form(
  334. store.slug,
  335. )}
  336. onSuccess={() =>
  337. setConfirmOpen(false)
  338. }
  339. >
  340. {({ processing }) => (
  341. <Button
  342. disabled={
  343. processing ||
  344. regenerationsLeft ===
  345. 0
  346. }
  347. data-test="confirm-regenerate-button"
  348. >
  349. Regenerate
  350. </Button>
  351. )}
  352. </Form>
  353. </DialogFooter>
  354. </DialogContent>
  355. </Dialog>
  356. </div>
  357. </div>
  358. {busy && generation && (
  359. <DesignProgress generation={generation} />
  360. )}
  361. {failed && <FailedNotice generation={failed} />}
  362. {selecting && (
  363. <p
  364. className="flex items-center gap-2 rounded-lg border border-emerald-600/30 bg-emerald-50 px-3 py-2 text-sm text-emerald-900 dark:bg-emerald-950/40 dark:text-emerald-200"
  365. role="status"
  366. >
  367. <SquareDashedMousePointer
  368. className="size-4 shrink-0"
  369. aria-hidden="true"
  370. />
  371. Click a part of your store, or drag a box
  372. around an area. Press Esc to stop.
  373. </p>
  374. )}
  375. <div
  376. className={cn(
  377. 'flex justify-center rounded-xl border bg-muted/40 p-3',
  378. selecting &&
  379. 'border-emerald-500 ring-2 ring-emerald-500/30',
  380. )}
  381. >
  382. <iframe
  383. key={store.design_version ?? 0}
  384. ref={frameRef}
  385. onLoad={() => {
  386. highlighter.onLoad();
  387. setFrameLoads((count) => count + 1);
  388. }}
  389. title={`Preview of ${store.name}`}
  390. src={previewUrl}
  391. className={cn(
  392. 'h-[70vh] min-h-[480px] rounded-lg border bg-white transition-[width]',
  393. device === 'mobile'
  394. ? 'w-[390px] max-w-full'
  395. : 'w-full',
  396. )}
  397. />
  398. </div>
  399. <LogoBox store={store} logo={logo} busy={busy} />
  400. <StylePicker
  401. store={store}
  402. current={style}
  403. presets={stylePresets}
  404. busy={busy}
  405. />
  406. <HeroPicker
  407. store={store}
  408. sections={heroSections}
  409. choices={heroChoices}
  410. busy={busy}
  411. />
  412. <LayoutPicker
  413. store={store}
  414. sections={layoutSections}
  415. options={layoutOptions}
  416. busy={busy}
  417. />
  418. <BentoBox
  419. store={store}
  420. bentos={bentoSections}
  421. homeSections={homeSections}
  422. photos={heroChoices}
  423. links={linkSuggestions}
  424. busy={busy}
  425. />
  426. <VideoBox
  427. store={store}
  428. videos={videoSections}
  429. homeSections={homeSections}
  430. busy={busy}
  431. />
  432. <FeaturedPicker
  433. store={store}
  434. sections={featuredSections}
  435. products={productOptions}
  436. busy={busy}
  437. />
  438. <DesignChat
  439. store={store}
  440. edits={edits}
  441. usage={usage.edits}
  442. busy={busy}
  443. selection={selection}
  444. onClearSelection={() => setSelection(null)}
  445. onStartSelecting={() => setSelecting(true)}
  446. />
  447. </section>
  448. </PreviewHighlightContext.Provider>
  449. ) : busy && generation ? (
  450. <DesignProgress generation={generation} />
  451. ) : (
  452. <div className="space-y-3">
  453. {failed && <FailedNotice generation={failed} />}
  454. <div className="flex flex-wrap items-center justify-between gap-4 rounded-xl border border-dashed p-6">
  455. <div className="flex items-start gap-3">
  456. <Sparkles className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
  457. <div>
  458. <h2 className="font-medium">
  459. Store design
  460. </h2>
  461. <p className="mt-1 max-w-md text-sm text-muted-foreground">
  462. Let the AI design your store from the
  463. brand description above. It takes about
  464. a minute, and your first design is free.
  465. </p>
  466. </div>
  467. </div>
  468. <Form
  469. {...StoreGenerationController.generate.form(
  470. store.slug,
  471. )}
  472. >
  473. {({ processing }) => (
  474. <Button
  475. disabled={processing}
  476. data-test="generate-store-button"
  477. >
  478. <Sparkles />
  479. {failed
  480. ? 'Try again'
  481. : 'Generate my store'}
  482. </Button>
  483. )}
  484. </Form>
  485. </div>
  486. </div>
  487. )}
  488. </div>
  489. </>
  490. );
  491. }
  492. function FailedNotice({ generation }: { generation: StoreGeneration }) {
  493. return (
  494. <Alert variant="destructive" data-test="generation-failed">
  495. <CircleAlert />
  496. <AlertTitle>
  497. {generation.type === 'edit'
  498. ? 'That change did not work'
  499. : 'The design did not work'}
  500. </AlertTitle>
  501. <AlertDescription>
  502. {generation.error ?? 'Please try again.'} Your store was not
  503. changed, and this attempt does not count towards your plan.
  504. </AlertDescription>
  505. </Alert>
  506. );
  507. }
  508. ShowStore.layout = {
  509. breadcrumbs: [
  510. {
  511. title: 'Overview',
  512. href: dashboard(),
  513. },
  514. {
  515. title: 'Store',
  516. href: '#',
  517. },
  518. ],
  519. };