'use client'; import { useEffect, useRef, useState } from 'react'; import { Play } from 'lucide-react'; import { sendGAEvent } from '@/app/utils/analytics'; import { WALKTHROUGH_CHAPTERS, WALKTHROUGH_FILM, filmSrc, formatTime } from './media'; import type { Chapter, Film } from './media'; type Props = { film?: Film; chapters?: Chapter[]; /** Accessible name of the video. */ label?: string; startTitle?: string; chaptersLabel?: string; /** Analytics: event prefix and location. */ event?: string; location?: string; }; /** * A narrated film with chapters (the home walkthrough by default; the tutorials page reuses it). * It shows the poster until someone presses play, then plays the R2 film in place. * Chapters jump straight to each part and follow the video's playhead. */ export default function Walkthrough({ film = WALKTHROUGH_FILM, chapters = WALKTHROUGH_CHAPTERS, label = 'Jotley walkthrough: capture, review, summarize and organize on Mac, iPhone and iPad', startTitle = 'Watch the walkthrough', chaptersLabel = 'Walkthrough chapters', event = 'walkthrough', location = 'home', }: Props = {}) { const videoRef = useRef(null); const chaptersRef = useRef(null); const [startAt, setStartAt] = useState(null); const [time, setTime] = useState(0); const started = startAt !== null; const duration = film.duration; const activeChapter = chapters.reduce((current, chapter, index) => (time >= chapter.start ? index : current), 0); useEffect(() => { const list = chaptersRef.current; const chapter = list?.children[activeChapter]; if (!started || !list || !chapter || list.scrollWidth <= list.clientWidth) return; // Move only the horizontal chapter strip, keeping the video and page in place. list.scrollTo({ left: list.scrollLeft + chapter.getBoundingClientRect().left - list.getBoundingClientRect().left, behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'instant' : 'smooth', }); }, [activeChapter, started]); const playFrom = (seconds: number) => { setTime(seconds); if (!started) { setStartAt(seconds); return; } const video = videoRef.current; if (!video) return; video.currentTime = seconds; video.play().catch(() => {}); }; const startWalkthrough = () => { playFrom(0); sendGAEvent(`${event}_play`, { location }); }; const jumpTo = (index: number) => { playFrom(chapters[index].start); sendGAEvent(`${event}_chapter`, { chapter: chapters[index].title, location }); }; return (
{started ? ( ) : ( <> {/* eslint-disable-next-line @next/next/no-img-element */} )}
); }