The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useIsFocused } from "@react-navigation/native"; |
| 2 | import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments"; |
| 3 | import { useAudioPlayerStatus } from "expo-audio"; |
| 4 | import { useGT, useMessages } from "gt-react-native"; |
| 5 | import { useContext, useEffect, useRef, useState } from "react"; |
| 6 | import { AppState, Pressable, View } from "react-native"; |
| 7 | import type { FilePreviewSource } from "../../components/ui/FilePreviewModal"; |
| 8 | import { filePreviewKey } from "../../lib/filePreviewGallery"; |
| 9 | import { PierreEntryIcon } from "../../components/PierreEntryIcon"; |
| 10 | import { AppText as Text } from "../../components/ui/AppText"; |
| 11 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 12 | import { useFullscreenVideoOpen, useMediaAudioSessionHold } from "../../lib/videoAudioSession"; |
| 13 | import { useRefreshAssetUrl } from "../../state/assets"; |
| 14 | import { ThreadMediaRetainContext, ThreadMediaVisibleContext } from "./ThreadMarkdownVideo"; |
| 15 | import { |
| 16 | acquireAudioSession, |
| 17 | activateAudioSession, |
| 18 | isAudioSessionOwner, |
| 19 | pauseAudioPlayer, |
| 20 | releaseAudioSession, |
| 21 | } from "../../lib/audioSession"; |
| 22 | import { usePlaybackAudioPlayer } from "../voice-input/usePlaybackAudioPlayer"; |
| 23 | |
| 24 | type PreviewProps = { |
| 25 | readonly source: FilePreviewSource; |
| 26 | readonly sizeBytes?: number; |
| 27 | readonly onOpen: () => void; |
| 28 | readonly onPlaybackStart?: () => void; |
| 29 | readonly onLongPress?: () => void; |
| 30 | }; |
| 31 | |
| 32 | function FileCardHeader(props: PreviewProps) { |
| 33 | const gt = useGT(); |
| 34 | const messages = useMessages(); |
| 35 | return ( |
| 36 | <Pressable |
| 37 | accessibilityRole="button" |
| 38 | accessibilityLabel={gt("Open {name}", { name: props.source.name })} |
| 39 | onPress={props.onOpen} |
| 40 | onLongPress={props.onLongPress} |
| 41 | className="h-[72px] flex-row items-center gap-3 p-3 active:bg-subtle" |
| 42 | > |
| 43 | <View className="h-12 w-10 shrink-0 items-center justify-center rounded-lg bg-subtle"> |
| 44 | <PierreEntryIcon path={props.source.name ?? "file"} kind="file" size={26} /> |
| 45 | </View> |
| 46 | <View className="min-w-0 flex-1 gap-1"> |
| 47 | <Text className="text-sm font-t3-medium text-foreground" numberOfLines={1}> |
| 48 | {props.source.name} |
| 49 | </Text> |
| 50 | <Text className="text-xs text-foreground-muted" numberOfLines={1}> |
| 51 | {props.sizeBytes !== undefined |
| 52 | ? gt("Audio · {size}", { size: formatAttachmentSize(props.sizeBytes, messages) }) |
| 53 | : gt("Audio")} |
| 54 | </Text> |
| 55 | </View> |
| 56 | <SymbolView name="chevron.right" size={12} tintColorClassName="accent-foreground-muted" /> |
| 57 | </Pressable> |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | function timestamp(seconds: number) { |
| 62 | const value = Number.isFinite(seconds) ? Math.max(0, Math.floor(seconds)) : 0; |
| 63 | return `${Math.floor(value / 60)}:${String(value % 60).padStart(2, "0")}`; |
| 64 | } |
| 65 | |
| 66 | function InlineAudio( |
| 67 | props: PreviewProps & { |
| 68 | readonly resolveUri: () => Promise<string | null>; |
| 69 | readonly onPlaybackActivityChange?: (active: boolean) => void; |
| 70 | }, |
| 71 | ) { |
| 72 | const gt = useGT(); |
| 73 | const { onPlaybackActivityChange } = props; |
| 74 | // An empty source keeps native buffering off until the user explicitly plays. |
| 75 | const player = usePlaybackAudioPlayer(null); |
| 76 | const status = useAudioPlayerStatus(player); |
| 77 | const [loading, setLoading] = useState(false); |
| 78 | const [error, setError] = useState(false); |
| 79 | const active = useRef(true); |
| 80 | const requestGeneration = useRef(0); |
| 81 | const playRequested = useRef(false); |
| 82 | const audioToken = useRef<ReturnType<typeof acquireAudioSession> | null>(null); |
| 83 | const [playbackRequested, setPlaybackRequested] = useState(false); |
| 84 | const requestPlayback = (requested: boolean) => { |
| 85 | playRequested.current = requested; |
| 86 | setPlaybackRequested(requested); |
| 87 | if (!requested) { |
| 88 | requestGeneration.current += 1; |
| 89 | setLoading(false); |
| 90 | if (audioToken.current) { |
| 91 | void releaseAudioSession(audioToken.current).catch(() => {}); |
| 92 | audioToken.current = null; |
| 93 | } |
| 94 | } |
| 95 | }; |
| 96 | useMediaAudioSessionHold(status.playing); |
| 97 | const fullscreenVideoOpen = useFullscreenVideoOpen(); |
| 98 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- drops the play request on fullscreen video, finish or error, and pauses the player for fullscreen |
| 99 | useEffect(() => { |
| 100 | if (fullscreenVideoOpen || status.didJustFinish || status.error) { |
| 101 | // oxlint-disable-next-line react/set-state-in-effect -- Native completion and fullscreen takeover invalidate the pending playback request. |
| 102 | requestPlayback(false); |
| 103 | } |
| 104 | if (status.didJustFinish || status.error) onPlaybackActivityChange?.(false); |
| 105 | if (fullscreenVideoOpen) pauseAudioPlayer(player); |
| 106 | }, [ |
| 107 | fullscreenVideoOpen, |
| 108 | status.didJustFinish, |
| 109 | status.error, |
| 110 | player, |
| 111 | requestPlayback, |
| 112 | onPlaybackActivityChange, |
| 113 | ]); |
| 114 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to AppState to pause audio in the background and releases the audio session on cleanup |
| 115 | useEffect(() => { |
| 116 | active.current = true; |
| 117 | const subscription = AppState.addEventListener("change", (state) => { |
| 118 | if (state !== "active") { |
| 119 | requestPlayback(false); |
| 120 | pauseAudioPlayer(player); |
| 121 | } |
| 122 | }); |
| 123 | return () => { |
| 124 | active.current = false; |
| 125 | playRequested.current = false; |
| 126 | requestGeneration.current += 1; |
| 127 | if (audioToken.current) { |
| 128 | void releaseAudioSession(audioToken.current).catch(() => {}); |
| 129 | audioToken.current = null; |
| 130 | } |
| 131 | subscription.remove(); |
| 132 | }; |
| 133 | }, [player, requestPlayback]); |
| 134 | const toggle = () => { |
| 135 | if (status.playing || playRequested.current) { |
| 136 | requestPlayback(false); |
| 137 | return pauseAudioPlayer(player); |
| 138 | } |
| 139 | const token = acquireAudioSession( |
| 140 | "playback", |
| 141 | () => { |
| 142 | requestPlayback(false); |
| 143 | pauseAudioPlayer(player); |
| 144 | }, |
| 145 | () => { |
| 146 | try { |
| 147 | pauseAudioPlayer(player); |
| 148 | } catch {} |
| 149 | }, |
| 150 | ); |
| 151 | if (!isAudioSessionOwner(token)) return; |
| 152 | props.onPlaybackStart?.(); |
| 153 | onPlaybackActivityChange?.(true); |
| 154 | audioToken.current = token; |
| 155 | requestPlayback(true); |
| 156 | const generation = ++requestGeneration.current; |
| 157 | const isCurrentRequest = () => |
| 158 | active.current && |
| 159 | playRequested.current && |
| 160 | requestGeneration.current === generation && |
| 161 | isAudioSessionOwner(token); |
| 162 | setLoading(true); |
| 163 | setError(false); |
| 164 | void Promise.resolve() |
| 165 | .then(async () => { |
| 166 | if (!isCurrentRequest()) return; |
| 167 | if (!status.isLoaded || status.error) { |
| 168 | const uri = await props.resolveUri(); |
| 169 | if (!isCurrentRequest() || AppState.currentState !== "active") return; |
| 170 | if (!uri) { |
| 171 | requestPlayback(false); |
| 172 | onPlaybackActivityChange?.(false); |
| 173 | setError(true); |
| 174 | return; |
| 175 | } |
| 176 | player.replace({ uri }); |
| 177 | } else if (status.didJustFinish || status.currentTime >= status.duration) { |
| 178 | await player.seekTo(0); |
| 179 | } |
| 180 | if (!isCurrentRequest() || AppState.currentState !== "active") return; |
| 181 | await activateAudioSession(token); |
| 182 | if (isCurrentRequest() && AppState.currentState === "active") player.play(); |
| 183 | }) |
| 184 | .catch(() => { |
| 185 | if (isCurrentRequest()) { |
| 186 | requestPlayback(false); |
| 187 | onPlaybackActivityChange?.(false); |
| 188 | setError(true); |
| 189 | } |
| 190 | }) |
| 191 | .finally(() => { |
| 192 | if (isCurrentRequest()) setLoading(false); |
| 193 | }); |
| 194 | }; |
| 195 | const progress = |
| 196 | status.duration > 0 ? Math.min(1, Math.max(0, status.currentTime / status.duration)) : 0; |
| 197 | const playing = playbackRequested || status.playing; |
| 198 | const busy = loading || (playbackRequested && status.isBuffering); |
| 199 | return ( |
| 200 | <> |
| 201 | <FileCardHeader |
| 202 | {...props} |
| 203 | onOpen={() => { |
| 204 | requestPlayback(false); |
| 205 | pauseAudioPlayer(player); |
| 206 | props.onOpen(); |
| 207 | }} |
| 208 | /> |
| 209 | <View className="h-14 flex-row items-center gap-3 px-3 pb-2"> |
| 210 | <Pressable |
| 211 | accessibilityRole="button" |
| 212 | accessibilityLabel={ |
| 213 | playing |
| 214 | ? gt("Pause audio") |
| 215 | : error || status.error |
| 216 | ? gt("Retry audio") |
| 217 | : gt("Play audio") |
| 218 | } |
| 219 | accessibilityState={{ busy }} |
| 220 | onPress={toggle} |
| 221 | className="h-11 w-11 items-center justify-center rounded-lg bg-subtle" |
| 222 | > |
| 223 | <SymbolView |
| 224 | name={playing ? "pause" : "play"} |
| 225 | size={18} |
| 226 | tintColorClassName="accent-foreground" |
| 227 | /> |
| 228 | </Pressable> |
| 229 | <View className="min-w-0 flex-1 gap-2"> |
| 230 | <View |
| 231 | accessibilityRole="progressbar" |
| 232 | accessibilityValue={{ min: 0, max: status.duration || 0, now: status.currentTime }} |
| 233 | className="h-1 overflow-hidden rounded-full bg-subtle" |
| 234 | > |
| 235 | <View className="h-1 bg-foreground-muted" style={{ width: `${progress * 100}%` }} /> |
| 236 | </View> |
| 237 | <Text className="text-xs text-foreground-muted" numberOfLines={1}> |
| 238 | {error || status.error |
| 239 | ? gt("Unavailable · tap play to retry") |
| 240 | : busy |
| 241 | ? gt("Loading…") |
| 242 | : `${timestamp(status.currentTime)} / ${status.isLoaded ? timestamp(status.duration) : "--:--"}`} |
| 243 | </Text> |
| 244 | </View> |
| 245 | </View> |
| 246 | </> |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | function VisibleFilePreview( |
| 251 | props: PreviewProps & { readonly onPlaybackActivityChange?: (active: boolean) => void }, |
| 252 | ) { |
| 253 | // Each mounted preview owns its source; feed streaming must not restart a fetch or player. |
| 254 | const [source] = useState(props.source); |
| 255 | const refresh = useRefreshAssetUrl( |
| 256 | "environmentId" in source ? source.environmentId : null, |
| 257 | "resource" in source ? source.resource : null, |
| 258 | ); |
| 259 | const resolveUri = () => ("uri" in source ? Promise.resolve(source.uri) : refresh()); |
| 260 | return <InlineAudio {...props} resolveUri={resolveUri} />; |
| 261 | } |
| 262 | |
| 263 | export function ThreadInlineFilePreview(props: PreviewProps) { |
| 264 | const visible = useContext(ThreadMediaVisibleContext); |
| 265 | const focused = useIsFocused(); |
| 266 | const retainRow = useContext(ThreadMediaRetainContext); |
| 267 | // Unmounting disposes the native player, so a started player stays mounted off-screen |
| 268 | // (and its feed row out of recycling) until it finishes or fails. Leaving the screen |
| 269 | // disposes it, so the latch resets too; the feed drops its row pin on blur itself. |
| 270 | const [playbackActive, setPlaybackActive] = useState(false); |
| 271 | if (!focused && playbackActive) setPlaybackActive(false); |
| 272 | const onPlaybackActivityChange = (active: boolean) => { |
| 273 | setPlaybackActive(active); |
| 274 | retainRow(active); |
| 275 | }; |
| 276 | return ( |
| 277 | <View |
| 278 | className="my-1 overflow-hidden rounded-xl border border-border bg-card" |
| 279 | style={{ width: 280, maxWidth: "100%" }} |
| 280 | > |
| 281 | {focused && (visible || playbackActive) ? ( |
| 282 | <VisibleFilePreview |
| 283 | key={filePreviewKey(props.source)} |
| 284 | {...props} |
| 285 | onPlaybackActivityChange={onPlaybackActivityChange} |
| 286 | /> |
| 287 | ) : ( |
| 288 | <> |
| 289 | <FileCardHeader {...props} /> |
| 290 | <View className="h-14" /> |
| 291 | </> |
| 292 | )} |
| 293 | </View> |
| 294 | ); |
| 295 | } |