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 { T, useGT } from "gt-react-native"; |
| 2 | import { useState } from "react"; |
| 3 | import { View } from "react-native"; |
| 4 | import { WebView } from "react-native-webview"; |
| 5 | import { SafeAreaView } from "react-native-safe-area-context"; |
| 6 | |
| 7 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
| 8 | import { AppText as Text } from "../../components/ui/AppText"; |
| 9 | import { FilePreviewLoading } from "./FilePreviewFeedback"; |
| 10 | import { LoadingStrip } from "../../components/ui/LoadingStrip"; |
| 11 | import { PdfPreview } from "../../components/PdfPreview"; |
| 12 | import { ControlPill } from "../../components/ui/ControlPill"; |
| 13 | import { useHardwareBack } from "../../lib/backHandler"; |
| 14 | import { isPdfPreviewUri } from "../../lib/pdfPreview"; |
| 15 | import { webViewDecelerationRate } from "../../lib/webViewDecelerationRate"; |
| 16 | import { useContentTopInset } from "../../components/ui/useBarInset"; |
| 17 | |
| 18 | export function WorkspaceFileWebPreview(props: { readonly uri: string | null }) { |
| 19 | const gt = useGT(); |
| 20 | if (props.uri === null) { |
| 21 | return <FilePreviewLoading message={gt("Preparing preview...")} background="card" />; |
| 22 | } |
| 23 | return <DocumentWebPreview key={props.uri} uri={props.uri} />; |
| 24 | } |
| 25 | |
| 26 | function DocumentWebPreview(props: { readonly uri: string }) { |
| 27 | const headerInset = useContentTopInset(); |
| 28 | const gt = useGT(); |
| 29 | const [loadProgress, setLoadProgress] = useState(0); |
| 30 | const [loadError, setLoadError] = useState<string | null>(null); |
| 31 | const [linkedPdf, setLinkedPdf] = useState<string | null>(null); |
| 32 | |
| 33 | useHardwareBack(linkedPdf !== null, () => { |
| 34 | setLinkedPdf(null); |
| 35 | return true; |
| 36 | }); |
| 37 | |
| 38 | if (isPdfPreviewUri(props.uri)) { |
| 39 | return ( |
| 40 | <View className="flex-1" style={{ paddingTop: headerInset }}> |
| 41 | <PdfPreview uri={props.uri} /> |
| 42 | </View> |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | return ( |
| 47 | <View className="relative flex-1 bg-card"> |
| 48 | {linkedPdf ? ( |
| 49 | <View className="flex-1" style={{ paddingTop: headerInset }}> |
| 50 | <PdfPreview key={linkedPdf} uri={linkedPdf} /> |
| 51 | <SafeAreaView edges={["bottom"]} className="border-t border-border px-4 py-2"> |
| 52 | <ControlPill |
| 53 | label={gt("Back to document")} |
| 54 | variant="pill" |
| 55 | onPress={() => setLinkedPdf(null)} |
| 56 | /> |
| 57 | </SafeAreaView> |
| 58 | </View> |
| 59 | ) : null} |
| 60 | <View className="flex-1" style={linkedPdf ? { display: "none" } : undefined}> |
| 61 | <WebView |
| 62 | source={{ uri: props.uri }} |
| 63 | // react-native-webview defaults to "never", which starts the page under the header. |
| 64 | contentInsetAdjustmentBehavior="automatic" |
| 65 | decelerationRate={webViewDecelerationRate} |
| 66 | originWhitelist={["*"]} |
| 67 | allowsBackForwardNavigationGestures |
| 68 | allowsFullscreenVideo |
| 69 | allowsInlineMediaPlayback |
| 70 | setSupportMultipleWindows={false} |
| 71 | onShouldStartLoadWithRequest={(request) => { |
| 72 | if (request.isTopFrame !== false && isPdfPreviewUri(request.url)) { |
| 73 | setLinkedPdf(request.url); |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | }} |
| 78 | startInLoadingState |
| 79 | onLoadProgress={(event) => { |
| 80 | setLoadProgress(event.nativeEvent.progress); |
| 81 | }} |
| 82 | onLoadStart={() => { |
| 83 | setLoadProgress(0.05); |
| 84 | setLoadError(null); |
| 85 | }} |
| 86 | onLoadEnd={() => { |
| 87 | setLoadProgress(0); |
| 88 | }} |
| 89 | onError={(event) => { |
| 90 | setLoadProgress(0); |
| 91 | setLoadError(event.nativeEvent.description || gt("The file could not be rendered.")); |
| 92 | }} |
| 93 | renderLoading={() => ( |
| 94 | <View className="absolute inset-0 items-center justify-center bg-card"> |
| 95 | <AppActivityIndicator /> |
| 96 | </View> |
| 97 | )} |
| 98 | style={{ flex: 1, backgroundColor: "transparent" }} |
| 99 | /> |
| 100 | <View pointerEvents="box-none" className="absolute inset-x-0" style={{ top: headerInset }}> |
| 101 | {loadProgress > 0 && loadProgress < 1 ? <LoadingStrip progress={loadProgress} /> : null} |
| 102 | {loadError ? ( |
| 103 | <View className="border-b border-border bg-card px-4 py-2"> |
| 104 | <Text className="text-xs font-t3-bold text-foreground"> |
| 105 | <T>Preview failed</T> |
| 106 | </Text> |
| 107 | <Text className="mt-0.5 text-xs leading-snug text-foreground-muted">{loadError}</Text> |
| 108 | </View> |
| 109 | ) : null} |
| 110 | </View> |
| 111 | </View> |
| 112 | </View> |
| 113 | ); |
| 114 | } |