AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

hero-picker.tsx 9.7KB

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