AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

bento-box.tsx 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import { router, useForm } from '@inertiajs/react';
  2. import { ChevronDown, LayoutGrid, Plus, Trash2, X } from 'lucide-react';
  3. import type { FormEvent } from 'react';
  4. import { useState } from 'react';
  5. import BentoSectionController from '@/actions/App/Http/Controllers/BentoSectionController';
  6. import InputError from '@/components/input-error';
  7. import { Button } from '@/components/ui/button';
  8. import { Input } from '@/components/ui/input';
  9. import { Label } from '@/components/ui/label';
  10. import type {
  11. BentoSectionInfo,
  12. BentoTileInfo,
  13. HeroChoice,
  14. HomeSectionInfo,
  15. LinkSuggestion,
  16. Store,
  17. } from '@/types';
  18. import { SectionName } from '@/components/stores/preview-highlight';
  19. const selectClass =
  20. 'h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:opacity-50 md:text-sm';
  21. const tones: Record<string, string> = {
  22. primary: 'Main colour',
  23. accent: 'Accent colour',
  24. dark: 'Dark',
  25. surface: 'Soft',
  26. };
  27. const MIN_TILES = 3;
  28. const MAX_TILES = 6;
  29. /**
  30. * The Design tab's Bento box: add tiles of different sizes (filled with a sensible start), then change
  31. * each tile's words, link, photo and colour.
  32. */
  33. export default function BentoBox({
  34. store,
  35. bentos,
  36. homeSections,
  37. photos,
  38. links,
  39. busy,
  40. }: {
  41. store: Store;
  42. bentos: BentoSectionInfo[];
  43. homeSections: HomeSectionInfo[];
  44. photos: HeroChoice[];
  45. links: LinkSuggestion[];
  46. busy: boolean;
  47. }) {
  48. const trust = homeSections.find((section) =>
  49. section.label.startsWith('trust bar'),
  50. );
  51. const [after, setAfter] = useState(trust?.id ?? homeSections[0]?.id ?? '');
  52. const [adding, setAdding] = useState(false);
  53. function add() {
  54. router.post(
  55. BentoSectionController.store.url(store.slug),
  56. { after },
  57. {
  58. preserveScroll: true,
  59. onStart: () => setAdding(true),
  60. onFinish: () => setAdding(false),
  61. },
  62. );
  63. }
  64. return (
  65. <details className="group rounded-xl border p-4" data-test="bento-box">
  66. <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
  67. <LayoutGrid className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
  68. <div>
  69. <h3 className="font-medium">
  70. Bento tiles{' '}
  71. {bentos.length > 0 && (
  72. <span className="text-muted-foreground">
  73. ({bentos.length})
  74. </span>
  75. )}
  76. </h3>
  77. <p className="mt-0.5 text-sm text-muted-foreground">
  78. Tiles of different sizes that lead shoppers to your
  79. range, collections, offers or story. Open to add or
  80. change them.
  81. </p>
  82. </div>
  83. <ChevronDown
  84. className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
  85. aria-hidden="true"
  86. />
  87. </summary>
  88. {/* Suggestions for every tile link field. */}
  89. <datalist id="bento-links">
  90. {links.map((link) => (
  91. <option key={link.href} value={link.href}>
  92. {link.label}
  93. </option>
  94. ))}
  95. </datalist>
  96. <div className="mt-4 space-y-6">
  97. {bentos.map((bento) => (
  98. <BentoForm
  99. key={bento.section_id}
  100. store={store}
  101. bento={bento}
  102. photos={photos}
  103. busy={busy}
  104. />
  105. ))}
  106. <div className="flex flex-wrap items-end gap-3 border-t pt-4">
  107. <div className="grid min-w-48 flex-1 gap-1.5">
  108. <Label htmlFor="bento-after">
  109. {bentos.length > 0
  110. ? 'Add another set of tiles after'
  111. : 'Add bento tiles after'}
  112. </Label>
  113. <select
  114. id="bento-after"
  115. value={after}
  116. onChange={(event) => setAfter(event.target.value)}
  117. className={selectClass}
  118. >
  119. {homeSections.map((section) => (
  120. <option key={section.id} value={section.id}>
  121. {section.label}
  122. </option>
  123. ))}
  124. </select>
  125. </div>
  126. <Button
  127. type="button"
  128. size="sm"
  129. variant="outline"
  130. onClick={add}
  131. disabled={busy || adding}
  132. >
  133. <Plus />
  134. Add tiles
  135. </Button>
  136. </div>
  137. </div>
  138. </details>
  139. );
  140. }
  141. function BentoForm({
  142. store,
  143. bento,
  144. photos,
  145. busy,
  146. }: {
  147. store: Store;
  148. bento: BentoSectionInfo;
  149. photos: HeroChoice[];
  150. busy: boolean;
  151. }) {
  152. const form = useForm<{
  153. heading: string;
  154. variant: 'mosaic' | 'even';
  155. tiles: BentoTileInfo[];
  156. }>({
  157. heading: bento.heading ?? '',
  158. variant: bento.variant,
  159. tiles: bento.tiles.map((tile) => ({
  160. ...tile,
  161. text: tile.text ?? '',
  162. tone: tile.tone ?? 'surface',
  163. })),
  164. });
  165. const [removing, setRemoving] = useState(false);
  166. const errors = form.errors as Record<string, string | undefined>;
  167. const id = bento.section_id;
  168. function setTile(index: number, field: keyof BentoTileInfo, value: string) {
  169. form.setData(
  170. 'tiles',
  171. form.data.tiles.map((tile, i) =>
  172. i === index
  173. ? {
  174. ...tile,
  175. [field]:
  176. value === '' && field === 'image' ? null : value,
  177. }
  178. : tile,
  179. ),
  180. );
  181. }
  182. function submit(event: FormEvent) {
  183. event.preventDefault();
  184. form.put(
  185. BentoSectionController.update.url({
  186. slug: store.slug,
  187. sectionId: id,
  188. }),
  189. { preserveScroll: true },
  190. );
  191. }
  192. function remove() {
  193. router.delete(
  194. BentoSectionController.destroy.url({
  195. slug: store.slug,
  196. sectionId: id,
  197. }),
  198. { preserveScroll: true },
  199. );
  200. }
  201. const tileError = (index: number) =>
  202. Object.entries(errors).find(([key]) =>
  203. key.startsWith(`tiles.${index}.`),
  204. )?.[1];
  205. const disabled = busy || form.processing;
  206. return (
  207. <form onSubmit={submit} className="space-y-4" noValidate>
  208. <SectionName
  209. target={{ sectionId: id, pageId: 'home', label: 'Bento tiles' }}
  210. className="text-sm font-medium"
  211. >
  212. {bento.heading || 'Bento tiles'}
  213. </SectionName>
  214. <div className="grid gap-3 sm:grid-cols-2">
  215. <div className="grid gap-1.5">
  216. <Label htmlFor={`bento-heading-${id}`}>
  217. Heading above the tiles (optional)
  218. </Label>
  219. <Input
  220. id={`bento-heading-${id}`}
  221. value={form.data.heading}
  222. maxLength={80}
  223. onChange={(event) =>
  224. form.setData('heading', event.target.value)
  225. }
  226. />
  227. </div>
  228. <div className="grid gap-1.5">
  229. <Label htmlFor={`bento-variant-${id}`}>Layout</Label>
  230. <select
  231. id={`bento-variant-${id}`}
  232. value={form.data.variant}
  233. onChange={(event) =>
  234. form.setData(
  235. 'variant',
  236. event.target.value as 'mosaic' | 'even',
  237. )
  238. }
  239. className={selectClass}
  240. >
  241. <option value="mosaic">
  242. One big tile and smaller ones
  243. </option>
  244. <option value="even">Equal tiles</option>
  245. </select>
  246. </div>
  247. </div>
  248. <ol className="space-y-3">
  249. {form.data.tiles.map((tile, index) => (
  250. <li
  251. key={index}
  252. className="space-y-2 rounded-lg border p-3"
  253. data-test="bento-tile"
  254. >
  255. <div className="flex items-center justify-between gap-2">
  256. <p className="text-sm font-medium">
  257. Tile {index + 1}
  258. {index === 0 && form.data.variant === 'mosaic'
  259. ? ' (the big one)'
  260. : ''}
  261. </p>
  262. {form.data.tiles.length > MIN_TILES && (
  263. <Button
  264. type="button"
  265. variant="ghost"
  266. size="icon"
  267. aria-label={`Remove tile ${index + 1}`}
  268. onClick={() =>
  269. form.setData(
  270. 'tiles',
  271. form.data.tiles.filter(
  272. (_, i) => i !== index,
  273. ),
  274. )
  275. }
  276. >
  277. <X />
  278. </Button>
  279. )}
  280. </div>
  281. <div className="grid gap-2 sm:grid-cols-2">
  282. <Input
  283. aria-label={`Tile ${index + 1} title`}
  284. value={tile.title}
  285. maxLength={60}
  286. placeholder="Title"
  287. onChange={(event) =>
  288. setTile(index, 'title', event.target.value)
  289. }
  290. />
  291. <Input
  292. aria-label={`Tile ${index + 1} short text`}
  293. value={tile.text ?? ''}
  294. maxLength={140}
  295. placeholder="Short text (optional)"
  296. onChange={(event) =>
  297. setTile(index, 'text', event.target.value)
  298. }
  299. />
  300. <Input
  301. aria-label={`Tile ${index + 1} link`}
  302. list="bento-links"
  303. value={tile.href}
  304. placeholder="/shop"
  305. onChange={(event) =>
  306. setTile(index, 'href', event.target.value)
  307. }
  308. />
  309. <div className="grid grid-cols-2 gap-2">
  310. <select
  311. aria-label={`Tile ${index + 1} photo`}
  312. value={tile.image ?? ''}
  313. onChange={(event) =>
  314. setTile(
  315. index,
  316. 'image',
  317. event.target.value,
  318. )
  319. }
  320. className={selectClass}
  321. >
  322. <option value="">No photo</option>
  323. {photos.map((photo) => (
  324. <option
  325. key={photo.ref}
  326. value={photo.ref}
  327. >
  328. {photo.label}
  329. </option>
  330. ))}
  331. </select>
  332. <select
  333. aria-label={`Tile ${index + 1} colour`}
  334. value={tile.tone ?? 'surface'}
  335. disabled={Boolean(tile.image)}
  336. onChange={(event) =>
  337. setTile(
  338. index,
  339. 'tone',
  340. event.target.value,
  341. )
  342. }
  343. className={selectClass}
  344. >
  345. {Object.entries(tones).map(
  346. ([value, label]) => (
  347. <option key={value} value={value}>
  348. {label}
  349. </option>
  350. ),
  351. )}
  352. </select>
  353. </div>
  354. </div>
  355. <InputError message={tileError(index)} />
  356. </li>
  357. ))}
  358. </ol>
  359. {form.data.tiles.length < MAX_TILES && (
  360. <Button
  361. type="button"
  362. variant="outline"
  363. size="sm"
  364. onClick={() =>
  365. form.setData('tiles', [
  366. ...form.data.tiles,
  367. {
  368. title: '',
  369. text: '',
  370. href: '/shop',
  371. image: null,
  372. tone: 'surface',
  373. },
  374. ])
  375. }
  376. >
  377. <Plus />
  378. Add a tile
  379. </Button>
  380. )}
  381. <InputError message={errors.section ?? errors.tiles} />
  382. <div className="flex flex-wrap items-center gap-2">
  383. <Button size="sm" disabled={disabled}>
  384. Save tiles
  385. </Button>
  386. {removing ? (
  387. <>
  388. <Button
  389. type="button"
  390. size="sm"
  391. variant="destructive"
  392. onClick={remove}
  393. disabled={disabled}
  394. >
  395. Yes, remove these tiles
  396. </Button>
  397. <Button
  398. type="button"
  399. size="sm"
  400. variant="ghost"
  401. onClick={() => setRemoving(false)}
  402. >
  403. Keep them
  404. </Button>
  405. </>
  406. ) : (
  407. <Button
  408. type="button"
  409. size="sm"
  410. variant="ghost"
  411. onClick={() => setRemoving(true)}
  412. >
  413. <Trash2 />
  414. Remove
  415. </Button>
  416. )}
  417. </div>
  418. </form>
  419. );
  420. }