| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- 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';
- import { SectionName } from '@/components/stores/preview-highlight';
-
- 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 (
- <details className="group rounded-xl border p-4" data-test="video-box">
- <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
- <Clapperboard className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
- <div>
- <h3 className="font-medium">
- Video{' '}
- {videos.length > 0 && (
- <span className="text-muted-foreground">
- ({videos.length})
- </span>
- )}
- </h3>
- <p className="mt-0.5 text-sm text-muted-foreground">
- Show a YouTube or Vimeo video, such as how your product
- is made. Open to add or change one.
- </p>
- </div>
- <ChevronDown
- className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
- aria-hidden="true"
- />
- </summary>
-
- <div className="mt-4 space-y-6">
- {videos.map((video) => (
- <VideoForm
- key={video.section_id}
- store={store}
- video={video}
- homeSections={homeSections}
- busy={busy}
- />
- ))}
-
- <div className="space-y-3 border-t pt-4">
- <p className="text-sm font-medium">
- {videos.length > 0
- ? 'Add another video'
- : 'Add a video'}
- </p>
- <VideoForm
- store={store}
- video={null}
- homeSections={homeSections}
- busy={busy}
- />
- </div>
- </div>
- </details>
- );
- }
-
- 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<string, string | undefined>;
-
- 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 onSubmit={submit} className="space-y-3" noValidate>
- {video && (
- <SectionName
- target={{
- sectionId: video.section_id,
- pageId: 'home',
- label: 'Video',
- }}
- className="text-sm font-medium"
- >
- {video.heading || 'Video'}
- </SectionName>
- )}
- <div className="grid gap-3 sm:grid-cols-[1fr_auto] sm:items-start">
- <div className="grid gap-1.5">
- <Label htmlFor={`video-url-${key}`}>
- YouTube or Vimeo link
- </Label>
- <Input
- id={`video-url-${key}`}
- value={form.data.video_url}
- onChange={(event) =>
- form.setData('video_url', event.target.value)
- }
- placeholder="https://youtu.be/abc123"
- inputMode="url"
- />
- <InputError message={form.errors.video_url} />
- </div>
- {picture && (
- <img
- src={picture}
- alt="Video preview"
- className="aspect-video w-full rounded-md object-cover sm:w-40"
- />
- )}
- </div>
-
- <div className="grid gap-1.5">
- <Label htmlFor={`video-heading-${key}`}>Heading</Label>
- <Input
- id={`video-heading-${key}`}
- value={form.data.heading}
- maxLength={80}
- onChange={(event) =>
- form.setData('heading', event.target.value)
- }
- placeholder="See how we make it"
- />
- <InputError message={form.errors.heading} />
- </div>
-
- <div className="grid gap-1.5">
- <Label htmlFor={`video-text-${key}`}>
- Short text (optional)
- </Label>
- <textarea
- id={`video-text-${key}`}
- value={form.data.text}
- maxLength={300}
- rows={2}
- onChange={(event) =>
- form.setData('text', event.target.value)
- }
- 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"
- />
- <InputError message={form.errors.text} />
- </div>
-
- <div className="grid gap-3 sm:grid-cols-2">
- <div className="grid gap-1.5">
- <Label htmlFor={`video-variant-${key}`}>Layout</Label>
- <select
- id={`video-variant-${key}`}
- value={form.data.variant}
- onChange={(event) =>
- form.setData(
- 'variant',
- event.target.value as 'wide' | 'split',
- )
- }
- className={selectClass}
- >
- <option value="wide">Wide video</option>
- <option value="split">Words beside the video</option>
- </select>
- </div>
- {!video && (
- <div className="grid gap-1.5">
- <Label htmlFor={`video-after-${key}`}>
- Put it after
- </Label>
- <select
- id={`video-after-${key}`}
- value={form.data.after}
- onChange={(event) =>
- form.setData('after', event.target.value)
- }
- className={selectClass}
- >
- {homeSections.map((section) => (
- <option key={section.id} value={section.id}>
- {section.label}
- </option>
- ))}
- </select>
- </div>
- )}
- </div>
-
- <InputError message={errors.section} />
-
- <div className="flex flex-wrap items-center gap-2">
- <Button size="sm" disabled={disabled}>
- {video ? 'Save video' : 'Add video'}
- </Button>
- {video &&
- (removing ? (
- <>
- <Button
- type="button"
- size="sm"
- variant="destructive"
- onClick={remove}
- disabled={disabled}
- >
- Yes, remove it
- </Button>
- <Button
- type="button"
- size="sm"
- variant="ghost"
- onClick={() => setRemoving(false)}
- >
- Keep it
- </Button>
- </>
- ) : (
- <Button
- type="button"
- size="sm"
- variant="ghost"
- onClick={() => setRemoving(true)}
- >
- <Trash2 />
- Remove
- </Button>
- ))}
- </div>
- </form>
- );
- }
|