Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SettingsLegalDocumentRouteScreen.tsx

186 lines6.1 KB

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