Jotley / website · jotley.ai / src / app / components / home / Walkthrough.tsx
| 1 | 'use client'; |
| 2 | |
| 3 | import { useEffect, useRef, useState } from 'react'; |
| 4 | import { Play } from 'lucide-react'; |
| 5 | import { sendGAEvent } from '@/app/utils/analytics'; |
| 6 | import { WALKTHROUGH_CHAPTERS, WALKTHROUGH_FILM, filmSrc, formatTime } from './media'; |
| 7 | import type { Chapter, Film } from './media'; |
| 8 | |
| 9 | type Props = { |
| 10 | film?: Film; |
| 11 | chapters?: Chapter[]; |
| 12 | /** Accessible name of the video. */ |
| 13 | label?: string; |
| 14 | startTitle?: string; |
| 15 | chaptersLabel?: string; |
| 16 | /** Analytics: event prefix and location. */ |
| 17 | event?: string; |
| 18 | location?: string; |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * A narrated film with chapters (the home walkthrough by default; the tutorials page reuses it). |
| 23 | * It shows the poster until someone presses play, then plays the R2 film in place. |
| 24 | * Chapters jump straight to each part and follow the video's playhead. |
| 25 | */ |
| 26 | export default function Walkthrough({ |
| 27 | film = WALKTHROUGH_FILM, |
| 28 | chapters = WALKTHROUGH_CHAPTERS, |
| 29 | label = 'Jotley walkthrough: capture, review, summarize and organize on Mac, iPhone and iPad', |
| 30 | startTitle = 'Watch the walkthrough', |
| 31 | chaptersLabel = 'Walkthrough chapters', |
| 32 | event = 'walkthrough', |
| 33 | location = 'home', |
| 34 | }: Props = {}) { |
| 35 | const videoRef = useRef<HTMLVideoElement>(null); |
| 36 | const chaptersRef = useRef<HTMLOListElement>(null); |
| 37 | const [startAt, setStartAt] = useState<number | null>(null); |
| 38 | const [time, setTime] = useState(0); |
| 39 | |
| 40 | const started = startAt !== null; |
| 41 | const duration = film.duration; |
| 42 | const activeChapter = chapters.reduce((current, chapter, index) => (time >= chapter.start ? index : current), 0); |
| 43 | |
| 44 | useEffect(() => { |
| 45 | const list = chaptersRef.current; |
| 46 | const chapter = list?.children[activeChapter]; |
| 47 | if (!started || !list || !chapter || list.scrollWidth <= list.clientWidth) return; |
| 48 | |
| 49 | // Move only the horizontal chapter strip, keeping the video and page in place. |
| 50 | list.scrollTo({ |
| 51 | left: list.scrollLeft + chapter.getBoundingClientRect().left - list.getBoundingClientRect().left, |
| 52 | behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'instant' : 'smooth', |
| 53 | }); |
| 54 | }, [activeChapter, started]); |
| 55 | |
| 56 | const playFrom = (seconds: number) => { |
| 57 | setTime(seconds); |
| 58 | if (!started) { |
| 59 | setStartAt(seconds); |
| 60 | return; |
| 61 | } |
| 62 | const video = videoRef.current; |
| 63 | if (!video) return; |
| 64 | video.currentTime = seconds; |
| 65 | video.play().catch(() => {}); |
| 66 | }; |
| 67 | |
| 68 | const startWalkthrough = () => { |
| 69 | playFrom(0); |
| 70 | sendGAEvent(`${event}_play`, { location }); |
| 71 | }; |
| 72 | |
| 73 | const jumpTo = (index: number) => { |
| 74 | playFrom(chapters[index].start); |
| 75 | sendGAEvent(`${event}_chapter`, { chapter: chapters[index].title, location }); |
| 76 | }; |
| 77 | |
| 78 | return ( |
| 79 | <div className="walk-grid"> |
| 80 | <div className="walk-player"> |
| 81 | <div className="walk-screen"> |
| 82 | {started ? ( |
| 83 | <video |
| 84 | ref={videoRef} |
| 85 | src={`${filmSrc(film)}#t=${startAt ?? 0}`} |
| 86 | poster={film.poster} |
| 87 | title={label} |
| 88 | aria-label={label} |
| 89 | controls |
| 90 | autoPlay |
| 91 | playsInline |
| 92 | onTimeUpdate={(e) => setTime(e.currentTarget.currentTime)} |
| 93 | > |
| 94 | {film.captions && <track kind="captions" src={film.captions} srcLang="en" label="English" />} |
| 95 | </video> |
| 96 | ) : ( |
| 97 | <> |
| 98 | {/* eslint-disable-next-line @next/next/no-img-element */} |
| 99 | <img src={film.poster} alt="" loading="lazy" /> |
| 100 | <button type="button" className="walk-start" onClick={startWalkthrough} aria-label={`${startTitle}: ${label}`}> |
| 101 | <span className="walk-start-ring"><Play size={26} fill="currentColor" aria-hidden="true" /></span> |
| 102 | <span className="walk-start-copy"> |
| 103 | <span className="walk-start-title">{startTitle}</span> |
| 104 | <span className="walk-start-meta">{formatTime(duration)} · with narration</span> |
| 105 | </span> |
| 106 | </button> |
| 107 | </> |
| 108 | )} |
| 109 | </div> |
| 110 | </div> |
| 111 | |
| 112 | <nav className="walk-chapters" aria-label={chaptersLabel}> |
| 113 | <div className="walk-chapters-head"> |
| 114 | <span>Chapters</span> |
| 115 | <span>{chapters.length}</span> |
| 116 | </div> |
| 117 | <ol ref={chaptersRef}> |
| 118 | {chapters.map((chapter, index) => { |
| 119 | const end = chapters[index + 1]?.start ?? duration; |
| 120 | const isActive = started && index === activeChapter; |
| 121 | const fill = isActive ? Math.min(100, ((time - chapter.start) / (end - chapter.start)) * 100) : started && index < activeChapter ? 100 : 0; |
| 122 | return ( |
| 123 | <li key={chapter.start}> |
| 124 | <button type="button" className={isActive ? 'on' : ''} onClick={() => jumpTo(index)} aria-current={isActive ? 'step' : undefined}> |
| 125 | <span className="walk-chapter-time">{formatTime(chapter.start)}</span> |
| 126 | <span className="walk-chapter-title">{chapter.title}</span> |
| 127 | <span className="walk-chapter-bar" aria-hidden="true"><span style={{ width: `${fill}%` }} /></span> |
| 128 | </button> |
| 129 | </li> |
| 130 | ); |
| 131 | })} |
| 132 | </ol> |
| 133 | </nav> |
| 134 | </div> |
| 135 | ); |
| 136 | } |