The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useEvent } from "expo"; |
| 2 | import { useVideoPlayer, VideoView } from "expo-video"; |
| 3 | import { useIsFocused } from "@react-navigation/native"; |
| 4 | import { T, useGT } from "gt-react-native"; |
| 5 | import { useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from "react"; |
| 6 | import { Pressable, View } from "react-native"; |
| 7 | |
| 8 | import { AppActivityIndicator } from "./ui/AppActivityIndicator"; |
| 9 | import { AppText } from "./ui/AppText"; |
| 10 | import { SymbolView } from "./ui/AppSymbol"; |
| 11 | import { VideoThumbnailImage } from "./VideoThumbnailImage"; |
| 12 | import { useMediaActions } from "../state/mediaActions"; |
| 13 | import { fitVideoFrame } from "../lib/videoFrameFit"; |
| 14 | import { loadVideoThumbnail } from "../lib/videoThumbnails"; |
| 15 | import { type MediaActionsSource } from "../lib/mediaActionsSource"; |
| 16 | import { useMediaVideoPlayback } from "../lib/useMediaVideoPlayback"; |
| 17 | import { MediaActionsMenu } from "./MediaActionsMenu"; |
| 18 | import { createAudioPlaybackSession } from "../lib/audioSession"; |
| 19 | |
| 20 | function LoadedMediaVideo(props: { |
| 21 | readonly uri: string; |
| 22 | readonly resolvePlaybackUri?: () => Promise<string | null>; |
| 23 | readonly playRequested: boolean; |
| 24 | readonly paused: boolean; |
| 25 | readonly visible: boolean; |
| 26 | readonly onPlaybackStart?: () => void; |
| 27 | }) { |
| 28 | const gt = useGT(); |
| 29 | const [attempt, setAttempt] = useState(0); |
| 30 | // Expo's Android player also reports completed playback as idle. |
| 31 | const [loadState, setLoadState] = useState<"pending" | "complete" | "error">("pending"); |
| 32 | const player = useVideoPlayer(null, (player) => { |
| 33 | player.staysActiveInBackground = false; |
| 34 | player.bufferOptions = { preferredForwardBufferDuration: 5 }; |
| 35 | }); |
| 36 | const playback = createAudioPlaybackSession(player, true); |
| 37 | const { active, fullscreen, createPlayRequest, onFullscreenEnter, onFullscreenExit } = |
| 38 | useMediaVideoPlayback(player, props, playback); |
| 39 | const previousPlaybackRequested = useRef(false); |
| 40 | const onPlaybackStart = useEffectEvent(() => props.onPlaybackStart?.()); |
| 41 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to native video player events to keep the shared audio session in step |
| 42 | useEffect(() => { |
| 43 | const subscription = player.addListener("playingChange", ({ isPlaying }) => { |
| 44 | if (isPlaying && !active.current && !fullscreen.current) playback.pause(); |
| 45 | else playback.playingChanged(isPlaying); |
| 46 | }); |
| 47 | const requested = player.addListener("playbackRequestedChange", ({ isPlaybackRequested }) => { |
| 48 | const wasPlaybackRequested = previousPlaybackRequested.current; |
| 49 | previousPlaybackRequested.current = isPlaybackRequested; |
| 50 | if (!isPlaybackRequested) { |
| 51 | if (playback.requested) playback.pause(); |
| 52 | } else if (!active.current && !fullscreen.current) playback.pause(); |
| 53 | else { |
| 54 | if (!wasPlaybackRequested) onPlaybackStart(); |
| 55 | playback.playingChanged(true); |
| 56 | } |
| 57 | }); |
| 58 | const ended = player.addListener("playToEnd", () => playback.pause()); |
| 59 | const failed = player.addListener("statusChange", ({ status }) => { |
| 60 | if (status === "error") playback.pause(); |
| 61 | }); |
| 62 | return () => { |
| 63 | subscription.remove(); |
| 64 | requested.remove(); |
| 65 | ended.remove(); |
| 66 | failed.remove(); |
| 67 | playback.pause(); |
| 68 | previousPlaybackRequested.current = false; |
| 69 | }; |
| 70 | }, [active, fullscreen, player, playback]); |
| 71 | const { status } = useEvent(player, "statusChange", { status: player.status }); |
| 72 | const loadSource = useEffectEvent(async (signal: AbortSignal) => { |
| 73 | const playIfUninterrupted = createPlayRequest(); |
| 74 | const uri = props.resolvePlaybackUri ? await props.resolvePlaybackUri() : props.uri; |
| 75 | if (signal.aborted) return; |
| 76 | if (uri === null) throw new Error("Video unavailable"); |
| 77 | playback.pause(); |
| 78 | previousPlaybackRequested.current = false; |
| 79 | await player.replaceAsync({ uri, contentType: "progressive" }); |
| 80 | if (!signal.aborted && props.playRequested) await playIfUninterrupted(); |
| 81 | }); |
| 82 | |
| 83 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- loads the video source into the native player and aborts the load on cleanup |
| 84 | useEffect(() => { |
| 85 | const controller = new AbortController(); |
| 86 | // oxlint-disable-next-line react/set-state-in-effect -- Starting a native media load publishes pending before its async result. |
| 87 | setLoadState("pending"); |
| 88 | // A renewed signature is used on Retry, not as a reason to reset the native player. |
| 89 | void loadSource(controller.signal).then( |
| 90 | () => { |
| 91 | if (!controller.signal.aborted) setLoadState("complete"); |
| 92 | }, |
| 93 | () => { |
| 94 | if (!controller.signal.aborted) setLoadState("error"); |
| 95 | }, |
| 96 | ); |
| 97 | return () => controller.abort(); |
| 98 | // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A play request or Retry (attempt) reloads the source into the same player. |
| 99 | }, [player, props.playRequested, attempt]); |
| 100 | |
| 101 | return ( |
| 102 | <View collapsable={false} style={{ flex: 1 }}> |
| 103 | <VideoView |
| 104 | player={player} |
| 105 | style={{ width: "100%", height: "100%" }} |
| 106 | surfaceType="textureView" |
| 107 | nativeControls |
| 108 | contentFit="contain" |
| 109 | fullscreenOptions={{ enable: true }} |
| 110 | onFullscreenEnter={onFullscreenEnter} |
| 111 | onFullscreenExit={onFullscreenExit} |
| 112 | allowsPictureInPicture={false} |
| 113 | /> |
| 114 | {loadState === "error" || (loadState === "complete" && status === "error") ? ( |
| 115 | <View className="absolute inset-0 items-center justify-center gap-2 bg-black px-4"> |
| 116 | <AppText className="text-center text-sm text-white/80"> |
| 117 | <T>Video unavailable</T> |
| 118 | </AppText> |
| 119 | <Pressable |
| 120 | accessibilityRole="button" |
| 121 | accessibilityLabel={gt("Retry video")} |
| 122 | onPress={() => setAttempt((value) => value + 1)} |
| 123 | className="min-h-11 justify-center px-4" |
| 124 | > |
| 125 | <AppText className="text-sm text-white"> |
| 126 | <T>Retry</T> |
| 127 | </AppText> |
| 128 | </Pressable> |
| 129 | </View> |
| 130 | ) : loadState === "pending" || status === "loading" ? ( |
| 131 | <View pointerEvents="none" className="absolute inset-0 items-center justify-center"> |
| 132 | <AppActivityIndicator color="#ffffff" accessibilityLabel={gt("Loading video")} /> |
| 133 | </View> |
| 134 | ) : null} |
| 135 | </View> |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | interface MediaVideoPlayerProps { |
| 140 | readonly uri: string | null; |
| 141 | readonly resolvePlaybackUri?: () => Promise<string | null>; |
| 142 | readonly name: string; |
| 143 | readonly thumbnailKey: string; |
| 144 | readonly thumbnailVisible?: boolean; |
| 145 | readonly visible?: boolean; |
| 146 | readonly unavailable?: boolean; |
| 147 | readonly unavailableDetail?: string | undefined; |
| 148 | readonly expanded?: boolean; |
| 149 | /** Fill the parent and size the frame to the video's shape instead of a fixed 16:9 slot. */ |
| 150 | readonly fitToVideo?: boolean; |
| 151 | readonly autoPlay?: boolean; |
| 152 | readonly paused?: boolean; |
| 153 | readonly onPlaybackStart?: () => void; |
| 154 | readonly actionsSource?: MediaActionsSource; |
| 155 | readonly borderRadius?: number; |
| 156 | } |
| 157 | |
| 158 | function MediaVideoPlayerContent(props: MediaVideoPlayerProps) { |
| 159 | const gt = useGT(); |
| 160 | const mediaActions = useMediaActions(props.actionsSource); |
| 161 | const isFocused = useIsFocused(); |
| 162 | const [playbackUri, setPlaybackUri] = useState<string | null>(props.expanded ? props.uri : null); |
| 163 | // Keep an opened player mounted while signing or reconnecting temporarily has no usable URL. |
| 164 | if (playbackUri === null && props.expanded && props.uri !== null) setPlaybackUri(props.uri); |
| 165 | const unavailableText = props.unavailableDetail |
| 166 | ? gt("Video unavailable. {detail}", { detail: props.unavailableDetail }) |
| 167 | : gt("Video unavailable"); |
| 168 | const [videoSize, setVideoSize] = useState<{ width: number; height: number } | null>(null); |
| 169 | const [bounds, setBounds] = useState<{ width: number; height: number } | null>(null); |
| 170 | const sizeSource = props.fitToVideo && isFocused && videoSize === null ? props.uri : null; |
| 171 | // Queue before the poster so early Play cannot cancel the extraction that supplies frame dimensions. |
| 172 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- loads native thumbnail dimensions independently of the poster's lifetime |
| 173 | useLayoutEffect(() => { |
| 174 | if (sizeSource === null) return; |
| 175 | // Thumbnails are oriented like the displayed frame, unlike VideoTrack.size. |
| 176 | const controller = new AbortController(); |
| 177 | void loadVideoThumbnail( |
| 178 | props.thumbnailKey, |
| 179 | async () => ({ uri: sizeSource, dispose: () => undefined }), |
| 180 | controller.signal, |
| 181 | ).then((thumbnail) => { |
| 182 | if (thumbnail && !controller.signal.aborted) { |
| 183 | setVideoSize({ width: thumbnail.width, height: thumbnail.height }); |
| 184 | } |
| 185 | }); |
| 186 | return () => controller.abort(); |
| 187 | }, [props.thumbnailKey, sizeSource]); |
| 188 | const fittedFrame = |
| 189 | props.fitToVideo && videoSize && bounds ? fitVideoFrame(videoSize, bounds, 480) : null; |
| 190 | |
| 191 | const frame = ( |
| 192 | <View |
| 193 | collapsable={false} |
| 194 | className="overflow-hidden rounded-[10px] bg-black" |
| 195 | style={[ |
| 196 | props.expanded |
| 197 | ? { flex: 1 } |
| 198 | : (fittedFrame ?? { width: "100%", maxWidth: 480, aspectRatio: 16 / 9 }), |
| 199 | props.borderRadius === undefined ? null : { borderRadius: props.borderRadius }, |
| 200 | ]} |
| 201 | > |
| 202 | {playbackUri ? ( |
| 203 | <LoadedMediaVideo |
| 204 | uri={props.uri ?? playbackUri} |
| 205 | resolvePlaybackUri={props.resolvePlaybackUri} |
| 206 | playRequested={!props.expanded || props.autoPlay === true} |
| 207 | paused={(props.paused ?? false) || mediaActions.sharing} |
| 208 | visible={props.visible ?? true} |
| 209 | onPlaybackStart={props.onPlaybackStart} |
| 210 | /> |
| 211 | ) : ( |
| 212 | <MediaActionsMenu media={mediaActions} inModal={props.expanded} style={{ flex: 1 }}> |
| 213 | <Pressable |
| 214 | accessibilityRole="button" |
| 215 | accessibilityLabel={ |
| 216 | props.unavailable |
| 217 | ? `${props.name}. ${unavailableText}` |
| 218 | : gt("Play {name}", { name: props.name }) |
| 219 | } |
| 220 | accessibilityHint={ |
| 221 | mediaActions.actions.length > 0 ? gt("Touch and hold for media actions") : undefined |
| 222 | } |
| 223 | accessibilityState={{ disabled: props.uri === null || props.unavailable === true }} |
| 224 | // Stays pressable so the long-press menu still opens on a failed or unsigned tile. |
| 225 | onPress={() => { |
| 226 | if (props.uri !== null && !props.unavailable) { |
| 227 | props.onPlaybackStart?.(); |
| 228 | setPlaybackUri(props.uri); |
| 229 | } |
| 230 | }} |
| 231 | className="flex-1 items-center justify-center px-4" |
| 232 | > |
| 233 | {!props.unavailable ? ( |
| 234 | <VideoThumbnailImage |
| 235 | cacheKey={props.thumbnailKey} |
| 236 | source={props.thumbnailVisible === false ? null : props.uri} |
| 237 | contentFit="contain" |
| 238 | /> |
| 239 | ) : null} |
| 240 | {props.unavailable ? ( |
| 241 | <AppText className="text-center text-sm text-white/80">{unavailableText}</AppText> |
| 242 | ) : props.uri === null ? ( |
| 243 | <AppActivityIndicator color="#ffffff" accessibilityLabel={gt("Loading video")} /> |
| 244 | ) : ( |
| 245 | <View className="size-12 items-center justify-center rounded-full bg-black/60"> |
| 246 | <SymbolView name="play" size={28} tintColor="#ffffff" type="monochrome" /> |
| 247 | </View> |
| 248 | )} |
| 249 | {props.uri !== null && !props.unavailable ? ( |
| 250 | <AppText |
| 251 | className="absolute right-2 bottom-2 max-w-[75%] rounded bg-black/60 px-2 py-1 text-right text-xs text-white" |
| 252 | numberOfLines={1} |
| 253 | > |
| 254 | {props.name} |
| 255 | </AppText> |
| 256 | ) : null} |
| 257 | </Pressable> |
| 258 | </MediaActionsMenu> |
| 259 | )} |
| 260 | </View> |
| 261 | ); |
| 262 | if (!props.fitToVideo) return frame; |
| 263 | return ( |
| 264 | <View |
| 265 | className="flex-1 items-center justify-center self-stretch" |
| 266 | onLayout={(event) => { |
| 267 | const { width, height } = event.nativeEvent.layout; |
| 268 | setBounds({ width, height }); |
| 269 | }} |
| 270 | > |
| 271 | {frame} |
| 272 | </View> |
| 273 | ); |
| 274 | } |
| 275 | |
| 276 | export function MediaVideoPlayer(props: MediaVideoPlayerProps) { |
| 277 | return <MediaVideoPlayerContent key={props.thumbnailKey} {...props} />; |
| 278 | } |