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

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