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.

video-box.tsx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { router, useForm } from '@inertiajs/react';
  2. import { Trash2 } from 'lucide-react';
  3. import type { FormEvent } from 'react';
  4. import { useState } from 'react';
  5. import VideoSectionController from '@/actions/App/Http/Controllers/VideoSectionController';
  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 { HomeSectionInfo, Store, VideoSectionInfo } from '@/types';
  11. import { SectionName } from '@/components/stores/preview-highlight';
  12. const selectClass =
  13. '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';
  14. /** The picture YouTube shows for a video, to preview the link. Null for Vimeo or a link that is not a video yet. */
  15. function youtubePicture(url: string): string | null {
  16. const match = url.match(
  17. /^https:\/\/(?:(?:www\.|m\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{6,20})/,
  18. );
  19. return match ? `https://i.ytimg.com/vi/${match[1]}/mqdefault.jpg` : null;
  20. }
  21. /**
  22. * The Design tab's Video box: add a YouTube or Vimeo video, change it, or remove it.
  23. */
  24. export default function VideoBox({
  25. store,
  26. videos,
  27. homeSections,
  28. busy,
  29. }: {
  30. store: Store;
  31. videos: VideoSectionInfo[];
  32. homeSections: HomeSectionInfo[];
  33. busy: boolean;
  34. }) {
  35. return (
  36. <div data-test="video-box">
  37. <p className="text-sm text-muted-foreground">
  38. Show a YouTube or Vimeo video, such as how your product is made.
  39. </p>
  40. <div className="mt-4 space-y-6">
  41. {videos.map((video) => (
  42. <VideoForm
  43. key={video.section_id}
  44. store={store}
  45. video={video}
  46. homeSections={homeSections}
  47. busy={busy}
  48. />
  49. ))}
  50. <div className="space-y-3 border-t pt-4">
  51. <p className="text-sm font-medium">
  52. {videos.length > 0
  53. ? 'Add another video'
  54. : 'Add a video'}
  55. </p>
  56. <VideoForm
  57. store={store}
  58. video={null}
  59. homeSections={homeSections}
  60. busy={busy}
  61. />
  62. </div>
  63. </div>
  64. </div>
  65. );
  66. }
  67. function VideoForm({
  68. store,
  69. video,
  70. homeSections,
  71. busy,
  72. }: {
  73. store: Store;
  74. video: VideoSectionInfo | null;
  75. homeSections: HomeSectionInfo[];
  76. busy: boolean;
  77. }) {
  78. const key = video?.section_id ?? 'new';
  79. const about = homeSections.find((section) =>
  80. section.label.startsWith('about'),
  81. );
  82. const form = useForm({
  83. video_url: video?.videoUrl ?? '',
  84. heading: video?.heading ?? '',
  85. text: video?.text ?? '',
  86. variant: video?.variant ?? 'wide',
  87. after: about?.id ?? homeSections[homeSections.length - 1]?.id ?? '',
  88. });
  89. const [removing, setRemoving] = useState(false);
  90. const picture = youtubePicture(form.data.video_url);
  91. const errors = form.errors as Record<string, string | undefined>;
  92. function submit(event: FormEvent) {
  93. event.preventDefault();
  94. const options = {
  95. preserveScroll: true,
  96. onSuccess: () => {
  97. if (!video) {
  98. form.reset();
  99. }
  100. },
  101. };
  102. if (video) {
  103. form.put(
  104. VideoSectionController.update.url({
  105. slug: store.slug,
  106. sectionId: video.section_id,
  107. }),
  108. options,
  109. );
  110. } else {
  111. form.post(VideoSectionController.store.url(store.slug), options);
  112. }
  113. }
  114. function remove() {
  115. if (!video) {
  116. return;
  117. }
  118. router.delete(
  119. VideoSectionController.destroy.url({
  120. slug: store.slug,
  121. sectionId: video.section_id,
  122. }),
  123. { preserveScroll: true },
  124. );
  125. }
  126. const disabled = busy || form.processing;
  127. return (
  128. <form onSubmit={submit} className="space-y-3" noValidate>
  129. {video && (
  130. <SectionName
  131. target={{
  132. sectionId: video.section_id,
  133. pageId: 'home',
  134. label: 'Video',
  135. }}
  136. className="text-sm font-medium"
  137. >
  138. {video.heading || 'Video'}
  139. </SectionName>
  140. )}
  141. <div className="grid gap-3 sm:grid-cols-[1fr_auto] sm:items-start">
  142. <div className="grid gap-1.5">
  143. <Label htmlFor={`video-url-${key}`}>
  144. YouTube or Vimeo link
  145. </Label>
  146. <Input
  147. id={`video-url-${key}`}
  148. value={form.data.video_url}
  149. onChange={(event) =>
  150. form.setData('video_url', event.target.value)
  151. }
  152. placeholder="https://youtu.be/abc123"
  153. inputMode="url"
  154. />
  155. <InputError message={form.errors.video_url} />
  156. </div>
  157. {picture && (
  158. <img
  159. src={picture}
  160. alt="Video preview"
  161. className="aspect-video w-full rounded-md object-cover sm:w-40"
  162. />
  163. )}
  164. </div>
  165. <div className="grid gap-1.5">
  166. <Label htmlFor={`video-heading-${key}`}>Heading</Label>
  167. <Input
  168. id={`video-heading-${key}`}
  169. value={form.data.heading}
  170. maxLength={80}
  171. onChange={(event) =>
  172. form.setData('heading', event.target.value)
  173. }
  174. placeholder="See how we make it"
  175. />
  176. <InputError message={form.errors.heading} />
  177. </div>
  178. <div className="grid gap-1.5">
  179. <Label htmlFor={`video-text-${key}`}>
  180. Short text (optional)
  181. </Label>
  182. <textarea
  183. id={`video-text-${key}`}
  184. value={form.data.text}
  185. maxLength={300}
  186. rows={2}
  187. onChange={(event) =>
  188. form.setData('text', event.target.value)
  189. }
  190. className="w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 md:text-sm"
  191. />
  192. <InputError message={form.errors.text} />
  193. </div>
  194. <div className="grid gap-3 sm:grid-cols-2">
  195. <div className="grid gap-1.5">
  196. <Label htmlFor={`video-variant-${key}`}>Layout</Label>
  197. <select
  198. id={`video-variant-${key}`}
  199. value={form.data.variant}
  200. onChange={(event) =>
  201. form.setData(
  202. 'variant',
  203. event.target.value as 'wide' | 'split',
  204. )
  205. }
  206. className={selectClass}
  207. >
  208. <option value="wide">Wide video</option>
  209. <option value="split">Words beside the video</option>
  210. </select>
  211. </div>
  212. {!video && (
  213. <div className="grid gap-1.5">
  214. <Label htmlFor={`video-after-${key}`}>
  215. Put it after
  216. </Label>
  217. <select
  218. id={`video-after-${key}`}
  219. value={form.data.after}
  220. onChange={(event) =>
  221. form.setData('after', event.target.value)
  222. }
  223. className={selectClass}
  224. >
  225. {homeSections.map((section) => (
  226. <option key={section.id} value={section.id}>
  227. {section.label}
  228. </option>
  229. ))}
  230. </select>
  231. </div>
  232. )}
  233. </div>
  234. <InputError message={errors.section} />
  235. <div className="flex flex-wrap items-center gap-2">
  236. <Button size="sm" disabled={disabled}>
  237. {video ? 'Save video' : 'Add video'}
  238. </Button>
  239. {video &&
  240. (removing ? (
  241. <>
  242. <Button
  243. type="button"
  244. size="sm"
  245. variant="destructive"
  246. onClick={remove}
  247. disabled={disabled}
  248. >
  249. Yes, remove it
  250. </Button>
  251. <Button
  252. type="button"
  253. size="sm"
  254. variant="ghost"
  255. onClick={() => setRemoving(false)}
  256. >
  257. Keep it
  258. </Button>
  259. </>
  260. ) : (
  261. <Button
  262. type="button"
  263. size="sm"
  264. variant="ghost"
  265. onClick={() => setRemoving(true)}
  266. >
  267. <Trash2 />
  268. Remove
  269. </Button>
  270. ))}
  271. </div>
  272. </form>
  273. );
  274. }