AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

hero-picker.tsx 9.5KB

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