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.

hero-picker.tsx 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { router, useForm } from '@inertiajs/react';
  2. import { ChevronDown, Check, ImageIcon, Upload } from 'lucide-react';
  3. import type { FormEvent } from 'react';
  4. import { useState } from 'react';
  5. import HeroImageController from '@/actions/App/Http/Controllers/HeroImageController';
  6. import InputError from '@/components/input-error';
  7. import { Button } from '@/components/ui/button';
  8. import { cn } from '@/lib/utils';
  9. import type { HeroChoice, HeroSectionInfo, Store } from '@/types';
  10. const layouts: { value: HeroSectionInfo['variant']; label: string }[] = [
  11. { value: 'image-full', label: 'Full-width photo' },
  12. { value: 'split', label: 'Photo beside words' },
  13. { value: 'centered', label: 'Words only' },
  14. ];
  15. /**
  16. * The Design tab's "Hero" box: the layout of each hero section and the photo it shows.
  17. * Automatic uses the store's newest product photo, so a store looks finished as soon as it has photos.
  18. */
  19. export default function HeroPicker({
  20. store,
  21. sections,
  22. choices,
  23. busy,
  24. }: {
  25. store: Store;
  26. sections: HeroSectionInfo[];
  27. choices: HeroChoice[];
  28. busy: boolean;
  29. }) {
  30. if (sections.length === 0) {
  31. return null;
  32. }
  33. return (
  34. <details
  35. className="group rounded-xl border p-4"
  36. data-test="hero-picker"
  37. >
  38. <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
  39. <ImageIcon className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
  40. <div>
  41. <h3 className="font-medium">Hero</h3>
  42. <p className="mt-0.5 text-sm text-muted-foreground">
  43. The big banner at the top. Choose its layout and photo,
  44. or upload your own banner photo.
  45. </p>
  46. </div>
  47. <ChevronDown
  48. className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
  49. aria-hidden="true"
  50. />
  51. </summary>
  52. <div className="mt-5 space-y-5">
  53. {sections.map((section) => (
  54. <HeroRow
  55. key={section.section_id}
  56. store={store}
  57. section={section}
  58. choices={choices}
  59. busy={busy}
  60. showTitle={sections.length > 1}
  61. />
  62. ))}
  63. </div>
  64. </details>
  65. );
  66. }
  67. function HeroRow({
  68. store,
  69. section,
  70. choices,
  71. busy,
  72. showTitle,
  73. }: {
  74. store: Store;
  75. section: HeroSectionInfo;
  76. choices: HeroChoice[];
  77. busy: boolean;
  78. showTitle: boolean;
  79. }) {
  80. const [saving, setSaving] = useState(false);
  81. const upload = useForm<{ photo: File | null }>({ photo: null });
  82. const ids = { upload: `hero-photo-${section.section_id}` };
  83. const shown = section.variant !== 'centered';
  84. const chosen =
  85. section.image !== null &&
  86. choices.some((choice) => choice.ref === section.image)
  87. ? section.image
  88. : null;
  89. function save(image: string | null, variant: HeroSectionInfo['variant']) {
  90. router.put(
  91. HeroImageController.update.url({
  92. slug: store.slug,
  93. sectionId: section.section_id,
  94. }),
  95. { image, variant },
  96. {
  97. preserveScroll: true,
  98. onStart: () => setSaving(true),
  99. onFinish: () => setSaving(false),
  100. },
  101. );
  102. }
  103. function send(event: FormEvent) {
  104. event.preventDefault();
  105. upload.post(
  106. HeroImageController.upload.url({
  107. slug: store.slug,
  108. sectionId: section.section_id,
  109. }),
  110. {
  111. preserveScroll: true,
  112. forceFormData: true,
  113. onSuccess: () => upload.reset(),
  114. },
  115. );
  116. }
  117. const disabled = busy || saving || upload.processing;
  118. return (
  119. <div className="space-y-4 border-t pt-4">
  120. {showTitle && (
  121. <p className="text-sm">
  122. “{section.headline}”
  123. {section.page_id !== 'home' && (
  124. <span className="text-muted-foreground">
  125. {' '}
  126. on {section.page_title}
  127. </span>
  128. )}
  129. </p>
  130. )}
  131. <fieldset className="space-y-2">
  132. <legend className="text-sm font-medium">Layout</legend>
  133. <div className="flex flex-wrap gap-2">
  134. {layouts.map((layout) => (
  135. <button
  136. key={layout.value}
  137. type="button"
  138. disabled={disabled}
  139. aria-pressed={section.variant === layout.value}
  140. onClick={() => save(chosen, layout.value)}
  141. className={cn(
  142. 'rounded-md border px-3 py-1.5 text-sm transition-colors disabled:opacity-50',
  143. section.variant === layout.value
  144. ? 'border-foreground bg-foreground text-background'
  145. : 'hover:bg-muted',
  146. )}
  147. >
  148. {layout.label}
  149. </button>
  150. ))}
  151. </div>
  152. </fieldset>
  153. {shown && (
  154. <fieldset className="space-y-2">
  155. <legend className="text-sm font-medium">Photo</legend>
  156. <ul className="grid grid-cols-3 gap-2 sm:grid-cols-5">
  157. <li>
  158. <PhotoButton
  159. label="Automatic"
  160. hint="Your newest product photo"
  161. selected={chosen === null}
  162. disabled={disabled}
  163. onClick={() => save(null, section.variant)}
  164. />
  165. </li>
  166. {choices.map((choice) => (
  167. <li key={choice.ref}>
  168. <PhotoButton
  169. label={choice.label}
  170. url={choice.url}
  171. selected={chosen === choice.ref}
  172. disabled={disabled}
  173. onClick={() =>
  174. save(choice.ref, section.variant)
  175. }
  176. />
  177. </li>
  178. ))}
  179. </ul>
  180. {choices.length === 0 && (
  181. <p className="text-sm text-muted-foreground">
  182. Add photos to your products, or upload a banner
  183. photo below. Until then the hero shows a soft
  184. pattern in your colours.
  185. </p>
  186. )}
  187. </fieldset>
  188. )}
  189. <form
  190. onSubmit={send}
  191. className="flex flex-wrap items-center gap-2"
  192. noValidate
  193. >
  194. <label htmlFor={ids.upload} className="sr-only">
  195. Upload a banner photo
  196. </label>
  197. <input
  198. id={ids.upload}
  199. type="file"
  200. accept="image/jpeg,image/png,image/webp"
  201. onChange={(event) =>
  202. upload.setData('photo', event.target.files?.[0] ?? null)
  203. }
  204. className="max-w-full text-sm file:mr-3 file:rounded-md file:border file:bg-transparent file:px-3 file:py-1.5 file:text-sm file:font-medium"
  205. />
  206. <Button
  207. size="sm"
  208. variant="outline"
  209. disabled={disabled || upload.data.photo === null}
  210. >
  211. <Upload />
  212. Upload banner photo
  213. </Button>
  214. <p className="w-full text-xs text-muted-foreground">
  215. JPG, PNG or WebP, up to 6 MB. A wide photo at least 1600
  216. pixels across looks best.
  217. </p>
  218. <InputError message={upload.errors.photo} />
  219. </form>
  220. </div>
  221. );
  222. }
  223. function PhotoButton({
  224. label,
  225. hint,
  226. url,
  227. selected,
  228. disabled,
  229. onClick,
  230. }: {
  231. label: string;
  232. hint?: string;
  233. url?: string;
  234. selected: boolean;
  235. disabled: boolean;
  236. onClick: () => void;
  237. }) {
  238. return (
  239. <button
  240. type="button"
  241. onClick={onClick}
  242. disabled={disabled}
  243. aria-pressed={selected}
  244. title={hint ?? label}
  245. className={cn(
  246. 'group relative block w-full overflow-hidden rounded-md border text-left disabled:opacity-50',
  247. selected && 'ring-2 ring-foreground ring-offset-2',
  248. )}
  249. >
  250. {url ? (
  251. <img
  252. src={url}
  253. alt=""
  254. className="aspect-square w-full object-cover"
  255. />
  256. ) : (
  257. <span className="flex aspect-square w-full items-center justify-center bg-muted p-2 text-center text-xs text-muted-foreground">
  258. {hint}
  259. </span>
  260. )}
  261. <span className="block truncate px-1.5 py-1 text-xs">{label}</span>
  262. {selected && (
  263. <span className="absolute top-1 right-1 rounded-full bg-foreground p-0.5 text-background">
  264. <Check className="size-3" />
  265. </span>
  266. )}
  267. </button>
  268. );
  269. }