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

SettingsLegalDocumentRouteScreen.tsx

149 lines5.2 KB

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