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 11KB

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