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.

bento-box.tsx 16KB

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