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 { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { videoMimeType } from "@t3tools/shared/video"; |
| 3 | import { T, useGT, useMessages } from "gt-react-native"; |
| 4 | import { Modal, Pressable, View } from "react-native"; |
| 5 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 6 | |
| 7 | import { AppActivityIndicator } from "./AppActivityIndicator"; |
| 8 | import { useLocalAttachmentPreview } from "../../lib/localAttachmentPreview"; |
| 9 | import { usePreviewPresentation, videoPreviewKey } from "../../lib/previewPresentation"; |
| 10 | import { useMediaActions } from "../../state/mediaActions"; |
| 11 | import { type MediaActionsSource } from "../../lib/mediaActionsSource"; |
| 12 | import { |
| 13 | mediaVideoPreviewUri, |
| 14 | mediaVideoThumbnailKey, |
| 15 | type LocalVideoPreviewSource, |
| 16 | type MediaVideoPreviewSource, |
| 17 | type VideoPreviewSource, |
| 18 | } from "../../lib/videoPreviewSource"; |
| 19 | import { useAssetUrlState, useRefreshAssetUrl } from "../../state/assets"; |
| 20 | import { usePreparedConnection } from "../../state/session"; |
| 21 | import { AppText } from "./AppText"; |
| 22 | import { MediaPreviewHeader } from "../MediaPreviewHeader"; |
| 23 | import { MediaVideoPlayer } from "../MediaVideoPlayer"; |
| 24 | |
| 25 | export type { VideoPreviewSource } from "../../lib/videoPreviewSource"; |
| 26 | |
| 27 | interface PlaybackState { |
| 28 | readonly uri: string | null; |
| 29 | readonly resolvePlaybackUri?: () => Promise<string | null>; |
| 30 | readonly unavailable: boolean; |
| 31 | readonly unavailableDetail?: string | undefined; |
| 32 | readonly error: string | null; |
| 33 | readonly actionsSource: MediaActionsSource | undefined; |
| 34 | } |
| 35 | |
| 36 | function useMediaPlayback(source: MediaVideoPreviewSource): PlaybackState { |
| 37 | const environmentId = "environmentId" in source ? source.environmentId : null; |
| 38 | const resource = "resource" in source ? source.resource : null; |
| 39 | const connection = usePreparedConnection(environmentId); |
| 40 | const asset = useAssetUrlState(environmentId, resource); |
| 41 | const gt = useGT(); |
| 42 | const refreshAssetUrl = useRefreshAssetUrl(environmentId, resource); |
| 43 | const uri = mediaVideoPreviewUri(source, asset._tag === "Success" ? asset.url : null); |
| 44 | return { |
| 45 | uri, |
| 46 | ...(resource !== null |
| 47 | ? { resolvePlaybackUri: async () => mediaVideoPreviewUri(source, await refreshAssetUrl()) } |
| 48 | : {}), |
| 49 | unavailable: |
| 50 | uri === null && |
| 51 | environmentId !== null && |
| 52 | (connection._tag === "None" || asset._tag === "Failure"), |
| 53 | unavailableDetail: |
| 54 | asset._tag === "Failure" && asset.reason === "missing-file-access" |
| 55 | ? gt("This connection cannot read host files. Pair again with a new link to view it.") |
| 56 | : undefined, |
| 57 | error: null, |
| 58 | actionsSource: source.actionsSource, |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | function useLocalPlayback(source: LocalVideoPreviewSource): PlaybackState { |
| 63 | const gt = useGT(); |
| 64 | const messages = useMessages(); |
| 65 | const { attachment } = source; |
| 66 | const preview = useLocalAttachmentPreview(attachment); |
| 67 | const { uri } = preview; |
| 68 | const error = preview.error |
| 69 | ? errorDisplayMessage(preview.error, messages) || gt("Could not load this video.") |
| 70 | : null; |
| 71 | const mimeType = videoMimeType(attachment) ?? attachment.mimeType; |
| 72 | return { |
| 73 | uri, |
| 74 | unavailable: error !== null, |
| 75 | error, |
| 76 | actionsSource: uri === null ? undefined : { name: attachment.name, mimeType, uri }, |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | function MediaPreviewModal(props: { |
| 81 | readonly source: MediaVideoPreviewSource; |
| 82 | readonly onRequestClose: () => void; |
| 83 | }) { |
| 84 | return ( |
| 85 | <OpenVideoPreviewModal |
| 86 | name={props.source.name} |
| 87 | thumbnailKey={mediaVideoThumbnailKey(props.source)} |
| 88 | playback={useMediaPlayback(props.source)} |
| 89 | onRequestClose={props.onRequestClose} |
| 90 | /> |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | function LocalPreviewModal(props: { |
| 95 | readonly source: LocalVideoPreviewSource; |
| 96 | readonly onRequestClose: () => void; |
| 97 | }) { |
| 98 | return ( |
| 99 | <OpenVideoPreviewModal |
| 100 | name={props.source.attachment.name} |
| 101 | thumbnailKey={`local:${props.source.attachment.id}`} |
| 102 | playback={useLocalPlayback(props.source)} |
| 103 | onRequestClose={props.onRequestClose} |
| 104 | /> |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | function OpenVideoPreviewModal(props: { |
| 109 | readonly name: string; |
| 110 | readonly thumbnailKey: string; |
| 111 | readonly playback: PlaybackState; |
| 112 | readonly onRequestClose: () => void; |
| 113 | }) { |
| 114 | const gt = useGT(); |
| 115 | const { playback } = props; |
| 116 | const insets = useSafeAreaInsets(); |
| 117 | const mediaActions = useMediaActions(playback.actionsSource, props.onRequestClose); |
| 118 | |
| 119 | return ( |
| 120 | <Modal |
| 121 | visible |
| 122 | animationType="fade" |
| 123 | presentationStyle="fullScreen" |
| 124 | onRequestClose={props.onRequestClose} |
| 125 | statusBarTranslucent |
| 126 | navigationBarTranslucent |
| 127 | > |
| 128 | <View |
| 129 | className="flex-1 bg-black" |
| 130 | style={{ paddingTop: insets.top, paddingBottom: insets.bottom }} |
| 131 | > |
| 132 | <MediaPreviewHeader |
| 133 | title={props.name} |
| 134 | media={mediaActions} |
| 135 | closeAccessibilityLabel={gt("Close video")} |
| 136 | onClose={props.onRequestClose} |
| 137 | /> |
| 138 | {playback.uri === null && !playback.unavailable ? ( |
| 139 | <View className="flex-1 items-center justify-center gap-3 px-6"> |
| 140 | <AppActivityIndicator color="#ffffff" /> |
| 141 | <AppText className="text-sm text-white/80"> |
| 142 | <T>Loading video...</T> |
| 143 | </AppText> |
| 144 | </View> |
| 145 | ) : ( |
| 146 | <MediaVideoPlayer |
| 147 | uri={playback.uri} |
| 148 | resolvePlaybackUri={playback.resolvePlaybackUri} |
| 149 | name={props.name} |
| 150 | thumbnailKey={props.thumbnailKey} |
| 151 | unavailable={playback.unavailable} |
| 152 | unavailableDetail={playback.unavailableDetail} |
| 153 | paused={mediaActions.sharing} |
| 154 | expanded |
| 155 | autoPlay |
| 156 | /> |
| 157 | )} |
| 158 | {playback.error ? ( |
| 159 | <AppText accessibilityRole="alert" className="px-4 pb-3 text-sm text-white/80"> |
| 160 | {playback.error} |
| 161 | </AppText> |
| 162 | ) : null} |
| 163 | <Pressable |
| 164 | accessibilityRole="button" |
| 165 | accessibilityLabel={gt("Save or share video")} |
| 166 | disabled={playback.uri === null || mediaActions.sharing} |
| 167 | onPress={mediaActions.share} |
| 168 | className="mx-4 my-3 min-h-12 items-center justify-center rounded-xl bg-white/15 px-4" |
| 169 | > |
| 170 | <AppText className="font-t3-medium text-base text-white"> |
| 171 | {mediaActions.sharing ? gt("Opening share sheet...") : gt("Save or share video")} |
| 172 | </AppText> |
| 173 | </Pressable> |
| 174 | </View> |
| 175 | </Modal> |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | export function VideoPreviewModal(props: { |
| 180 | readonly source: VideoPreviewSource | null; |
| 181 | readonly onRequestClose: () => void; |
| 182 | }) { |
| 183 | usePreviewPresentation( |
| 184 | props.source === null ? null : videoPreviewKey(props.source), |
| 185 | props.onRequestClose, |
| 186 | ); |
| 187 | const { source } = props; |
| 188 | if (source === null) return null; |
| 189 | return source.type === "local" ? ( |
| 190 | <LocalPreviewModal |
| 191 | key={videoPreviewKey(source)} |
| 192 | source={source} |
| 193 | onRequestClose={props.onRequestClose} |
| 194 | /> |
| 195 | ) : ( |
| 196 | <MediaPreviewModal |
| 197 | key={videoPreviewKey(source)} |
| 198 | source={source} |
| 199 | onRequestClose={props.onRequestClose} |
| 200 | /> |
| 201 | ); |
| 202 | } |