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 { SheetHeader } from "../../../components/ui/SheetHeader"; |
| 3 | import { useNavigation } from "@react-navigation/native"; |
| 4 | import { useState } from "react"; |
| 5 | import { Pressable, View } from "react-native"; |
| 6 | import { tryOpenExternalUrl } from "../../../lib/openExternalUrl"; |
| 7 | import { WebView } from "react-native-webview"; |
| 8 | |
| 9 | import { Button } from "../../../components/ui/Button"; |
| 10 | import { AppActivityIndicator } from "../../../components/ui/AppActivityIndicator"; |
| 11 | import { AppText as Text } from "../../../components/ui/AppText"; |
| 12 | import { LoadingStrip } from "../../../components/ui/LoadingStrip"; |
| 13 | import { SymbolView } from "../../../components/ui/AppSymbol"; |
| 14 | import { isLegalDocumentUrl, LEGAL_URL } from "../lib/legal-document-url"; |
| 15 | import { webViewDecelerationRate } from "../../../lib/webViewDecelerationRate"; |
| 16 | |
| 17 | interface SettingsLegalDocumentRouteScreenProps { |
| 18 | readonly documentName: string; |
| 19 | readonly documentUrl: string; |
| 20 | } |
| 21 | |
| 22 | export function SettingsLegalDocumentRouteScreen({ |
| 23 | documentName, |
| 24 | documentUrl, |
| 25 | }: SettingsLegalDocumentRouteScreenProps) { |
| 26 | const gt = useGT(); |
| 27 | const [reloadKey, setReloadKey] = useState(0); |
| 28 | const [loadProgress, setLoadProgress] = useState(0); |
| 29 | const [loadError, setLoadError] = useState<string | null>(null); |
| 30 | const [externalUrl, setExternalUrl] = useState(documentUrl); |
| 31 | const navigation = useNavigation(); |
| 32 | const safeExternalUrl = isLegalDocumentUrl(externalUrl) ? externalUrl : LEGAL_URL; |
| 33 | const headerOptions = ( |
| 34 | <SheetHeader |
| 35 | title={documentName} |
| 36 | accessibilityLabel={gt("Close legal document")} |
| 37 | onClose={() => navigation.goBack()} |
| 38 | actions={[ |
| 39 | { |
| 40 | accessibilityLabel: gt("Open legal documents in external browser"), |
| 41 | icon: "safari", |
| 42 | onPress: () => void tryOpenExternalUrl(safeExternalUrl), |
| 43 | }, |
| 44 | ]} |
| 45 | /> |
| 46 | ); |
| 47 | |
| 48 | const openExternalUrl = (url: string) => { |
| 49 | void tryOpenExternalUrl(url); |
| 50 | }; |
| 51 | if (loadError) { |
| 52 | return ( |
| 53 | <View className="flex-1 bg-sheet"> |
| 54 | {headerOptions} |
| 55 | <View className="flex-1 items-center justify-center gap-4 px-8"> |
| 56 | <SymbolView |
| 57 | name="exclamationmark.triangle" |
| 58 | size={32} |
| 59 | tintColorClassName={"accent-icon"} |
| 60 | type="monochrome" |
| 61 | weight="regular" |
| 62 | /> |
| 63 | <View className="items-center gap-2"> |
| 64 | <Text className="text-center font-t3-bold text-lg text-foreground"> |
| 65 | {gt("Couldn't load the {document}", { document: documentName.toLowerCase() })} |
| 66 | </Text> |
| 67 | <Text selectable className="text-center text-sm leading-normal text-foreground-muted"> |
| 68 | {loadError} |
| 69 | </Text> |
| 70 | </View> |
| 71 | <View className="w-full max-w-[320px] gap-2"> |
| 72 | <Button |
| 73 | label={gt("Try Again")} |
| 74 | tone="contrast" |
| 75 | fullWidth |
| 76 | onPress={() => { |
| 77 | setLoadError(null); |
| 78 | setReloadKey((value) => value + 1); |
| 79 | }} |
| 80 | /> |
| 81 | <Pressable |
| 82 | accessibilityRole="link" |
| 83 | onPress={() => openExternalUrl(documentUrl)} |
| 84 | className="items-center rounded-xl px-4 py-3 active:bg-foreground/5" |
| 85 | > |
| 86 | <Text className="font-t3-medium text-base text-foreground-muted"> |
| 87 | <T>Open in Browser</T> |
| 88 | </Text> |
| 89 | </Pressable> |
| 90 | </View> |
| 91 | </View> |
| 92 | </View> |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | return ( |
| 97 | <View className="relative flex-1 bg-sheet"> |
| 98 | {headerOptions} |
| 99 | {loadProgress > 0 && loadProgress < 1 ? <LoadingStrip progress={loadProgress} /> : null} |
| 100 | <WebView |
| 101 | key={reloadKey} |
| 102 | source={{ uri: documentUrl }} |
| 103 | originWhitelist={["http://*", "https://*", "mailto:*"]} |
| 104 | cacheEnabled={false} |
| 105 | cacheMode="LOAD_NO_CACHE" |
| 106 | allowsBackForwardNavigationGestures |
| 107 | decelerationRate={webViewDecelerationRate} |
| 108 | domStorageEnabled={false} |
| 109 | incognito |
| 110 | javaScriptEnabled={false} |
| 111 | setSupportMultipleWindows={false} |
| 112 | sharedCookiesEnabled={false} |
| 113 | thirdPartyCookiesEnabled={false} |
| 114 | startInLoadingState |
| 115 | onShouldStartLoadWithRequest={(request) => { |
| 116 | if (isLegalDocumentUrl(request.url)) return true; |
| 117 | |
| 118 | openExternalUrl(request.url); |
| 119 | return false; |
| 120 | }} |
| 121 | onLoadProgress={(event) => { |
| 122 | setLoadProgress(event.nativeEvent.progress); |
| 123 | }} |
| 124 | onLoadStart={() => { |
| 125 | setLoadProgress(0.05); |
| 126 | setLoadError(null); |
| 127 | }} |
| 128 | onLoadEnd={(event) => { |
| 129 | if (isLegalDocumentUrl(event.nativeEvent.url)) { |
| 130 | setExternalUrl(event.nativeEvent.url); |
| 131 | } |
| 132 | setLoadProgress(0); |
| 133 | }} |
| 134 | onError={(event) => { |
| 135 | setLoadProgress(0); |
| 136 | setLoadError(event.nativeEvent.description || gt("The page could not be loaded.")); |
| 137 | }} |
| 138 | onHttpError={(event) => { |
| 139 | if (!isLegalDocumentUrl(event.nativeEvent.url)) return; |
| 140 | setLoadProgress(0); |
| 141 | setLoadError( |
| 142 | gt("The server returned status {status}.", { status: event.nativeEvent.statusCode }), |
| 143 | ); |
| 144 | }} |
| 145 | renderLoading={() => ( |
| 146 | <View className="absolute inset-0 items-center justify-center bg-sheet"> |
| 147 | <AppActivityIndicator /> |
| 148 | </View> |
| 149 | )} |
| 150 | style={{ flex: 1, backgroundColor: "transparent" }} |
| 151 | /> |
| 152 | </View> |
| 153 | ); |
| 154 | } |