import { router, useForm } from '@inertiajs/react'; import { ChevronDown, Clapperboard, Trash2 } from 'lucide-react'; import type { FormEvent } from 'react'; import { useState } from 'react'; import VideoSectionController from '@/actions/App/Http/Controllers/VideoSectionController'; import InputError from '@/components/input-error'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import type { HomeSectionInfo, Store, VideoSectionInfo } from '@/types'; const selectClass = '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'; /** The picture YouTube shows for a video, to preview the link. Null for Vimeo or a link that is not a video yet. */ function youtubePicture(url: string): string | null { const match = url.match( /^https:\/\/(?:(?:www\.|m\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{6,20})/, ); return match ? `https://i.ytimg.com/vi/${match[1]}/mqdefault.jpg` : null; } /** * The Design tab's Video box: add a YouTube or Vimeo video, change it, or remove it. */ export default function VideoBox({ store, videos, homeSections, busy, }: { store: Store; videos: VideoSectionInfo[]; homeSections: HomeSectionInfo[]; busy: boolean; }) { return (

Video{' '} {videos.length > 0 && ( ({videos.length}) )}

Show a YouTube or Vimeo video, such as how your product is made. Open to add or change one.

{videos.map((video) => ( ))}

{videos.length > 0 ? 'Add another video' : 'Add a video'}

); } function VideoForm({ store, video, homeSections, busy, }: { store: Store; video: VideoSectionInfo | null; homeSections: HomeSectionInfo[]; busy: boolean; }) { const key = video?.section_id ?? 'new'; const about = homeSections.find((section) => section.label.startsWith('about'), ); const form = useForm({ video_url: video?.videoUrl ?? '', heading: video?.heading ?? '', text: video?.text ?? '', variant: video?.variant ?? 'wide', after: about?.id ?? homeSections[homeSections.length - 1]?.id ?? '', }); const [removing, setRemoving] = useState(false); const picture = youtubePicture(form.data.video_url); const errors = form.errors as Record; function submit(event: FormEvent) { event.preventDefault(); const options = { preserveScroll: true, onSuccess: () => { if (!video) { form.reset(); } }, }; if (video) { form.put( VideoSectionController.update.url({ slug: store.slug, sectionId: video.section_id, }), options, ); } else { form.post(VideoSectionController.store.url(store.slug), options); } } function remove() { if (!video) { return; } router.delete( VideoSectionController.destroy.url({ slug: store.slug, sectionId: video.section_id, }), { preserveScroll: true }, ); } const disabled = busy || form.processing; return (
form.setData('video_url', event.target.value) } placeholder="https://youtu.be/abc123" inputMode="url" />
{picture && ( Video preview )}
form.setData('heading', event.target.value) } placeholder="See how we make it" />