|
1 | 1 | import { settlePromise } from "@t3tools/client-runtime/state/runtime"; |
2 | 2 | import { AsyncResult } from "effect/reactivity"; |
3 | 3 | import { PlayIcon, RotateCwIcon, TriangleAlertIcon } from "lucide-react"; |
4 | | -import { useEffect, useState, type CSSProperties } from "react"; |
| 4 | +import { useState, type CSSProperties } from "react"; |
5 | 5 |
|
| 6 | +import { observeIntersection } from "../../lib/observeIntersection"; |
6 | 7 | import { cn } from "../../lib/utils"; |
7 | 8 | import { prepareVideoFirstFrame } from "../../lib/videoFirstFrame"; |
8 | 9 | import { Button } from "../ui/button"; |
@@ -44,13 +45,11 @@ export function MediaVideoPlayer({ |
44 | 45 | onRetry, |
45 | 46 | actionsSource, |
46 | 47 | }: MediaVideoPlayerProps) { |
47 | | - // Use state so effects rebind when retry or failure remounts the video. |
48 | | - const [video, setVideo] = useState<HTMLVideoElement | null>(null); |
49 | 48 | const [playbackSource, setPlaybackSource] = useState<{ |
50 | 49 | src: string; |
51 | 50 | revision: string | null; |
52 | 51 | } | null>(null); |
53 | | - const [playingVideo, setPlayingVideo] = useState<HTMLVideoElement | null>(null); |
| 52 | + const [playing, setPlaying] = useState(false); |
54 | 53 | const [failedSrc, setFailedSrc] = useState<string | null>(null); |
55 | 54 | const [retrying, setRetrying] = useState(false); |
56 | 55 | const [loadAttempt, setLoadAttempt] = useState(0); |
@@ -65,33 +64,15 @@ export function MediaVideoPlayer({ |
65 | 64 | playbackSource !== null && |
66 | 65 | playbackSource.revision !== revision && |
67 | 66 | (latestSrc !== null || sourceFailed) && |
68 | | - (video === null || playingVideo !== video) |
| 67 | + !playing |
69 | 68 | ) { |
70 | 69 | setPlaybackSource( |
71 | 70 | revision !== null && latestSrc !== null ? { src: latestSrc, revision } : null, |
72 | 71 | ); |
73 | 72 | } |
74 | 73 |
|
75 | | - useEffect(() => { |
76 | | - if (!video || preload === "metadata" || preloaded) return; |
77 | | - let active = true; |
78 | | - const observer = new IntersectionObserver( |
79 | | - (entries) => { |
80 | | - if (!active || !entries.some((entry) => entry.isIntersecting)) return; |
81 | | - setPreloadedSrc(src); |
82 | | - observer.disconnect(); |
83 | | - }, |
84 | | - { rootMargin: "200px" }, |
85 | | - ); |
86 | | - observer.observe(video); |
87 | | - return () => { |
88 | | - active = false; |
89 | | - observer.disconnect(); |
90 | | - }; |
91 | | - }, [video, src, preload, preloaded]); |
92 | | - |
93 | | - useEffect(() => { |
94 | | - if (!video) return; |
| 74 | + // Retry and failure remount the element, so its listeners live and die with it. |
| 75 | + const attachVideo = (video: HTMLVideoElement) => { |
95 | 76 | const pauseWhenHidden = () => { |
96 | 77 | // Native fullscreen can hide the inline page while this video is still visible. |
97 | 78 | const fullscreen = |
@@ -107,8 +88,22 @@ export function MediaVideoPlayer({ |
107 | 88 | document.removeEventListener("fullscreenchange", pauseWhenHidden); |
108 | 89 | video.removeEventListener("webkitendfullscreen", pauseWhenHidden); |
109 | 90 | video.pause(); |
| 91 | + // A detached element's pause event never reaches onPause. |
| 92 | + setPlaying(false); |
110 | 93 | }; |
111 | | - }, [video]); |
| 94 | + }; |
| 95 | + |
| 96 | + // On the wrapper, not the video: re-running the video's ref callback would pause playback. |
| 97 | + const observePreload = (player: HTMLSpanElement) => { |
| 98 | + if (src === null || failed || preload === "metadata" || preloaded) return; |
| 99 | + return observeIntersection( |
| 100 | + player, |
| 101 | + (entry) => { |
| 102 | + if (entry.isIntersecting) setPreloadedSrc(src); |
| 103 | + }, |
| 104 | + { rootMargin: "200px" }, |
| 105 | + ); |
| 106 | + }; |
112 | 107 |
|
113 | 108 | const retry = async () => { |
114 | 109 | if (retrying) return; |
@@ -128,6 +123,7 @@ export function MediaVideoPlayer({ |
128 | 123 |
|
129 | 124 | const player = ( |
130 | 125 | <span |
| 126 | + ref={observePreload} |
131 | 127 | className={cn("relative inline-block align-middle", className)} |
132 | 128 | style={style} |
133 | 129 | data-markdown-copy={copyMarkdown} |
@@ -162,7 +158,7 @@ export function MediaVideoPlayer({ |
162 | 158 | ) : src !== null && !failed ? ( |
163 | 159 | <video |
164 | 160 | key={loadAttempt} |
165 | | - ref={setVideo} |
| 161 | + ref={attachVideo} |
166 | 162 | src={src} |
167 | 163 | aria-label={label || "Video preview"} |
168 | 164 | aria-hidden={onOpen ? true : undefined} |
@@ -181,14 +177,14 @@ export function MediaVideoPlayer({ |
181 | 177 | prepareVideoFirstFrame(event.currentTarget); |
182 | 178 | if (sourceRevision !== null) setPlaybackSource({ src, revision: sourceRevision }); |
183 | 179 | }} |
184 | | - onPlay={(event) => { |
185 | | - setPlayingVideo(event.currentTarget); |
| 180 | + onPlay={() => { |
| 181 | + setPlaying(true); |
186 | 182 | setPlaybackSource({ src, revision: sourceRevision }); |
187 | 183 | }} |
188 | | - onPause={() => setPlayingVideo(null)} |
189 | | - onEnded={() => setPlayingVideo(null)} |
| 184 | + onPause={() => setPlaying(false)} |
| 185 | + onEnded={() => setPlaying(false)} |
190 | 186 | onError={() => { |
191 | | - setPlayingVideo(null); |
| 187 | + setPlaying(false); |
192 | 188 | setPlaybackSource(null); |
193 | 189 | if (latestSrc === null || src === latestSrc) setFailedSrc(src); |
194 | 190 | }} |
|