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