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