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.

hero-picker.tsx 10KB

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