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