1- import { useNavigation } from "@react-navigation/native" ;
2- import { useCallback , useRef , useState } from "react" ;
1+ import {
2+ type NavigationProp ,
3+ type ParamListBase ,
4+ type RouteProp ,
5+ useNavigation ,
6+ useRoute ,
7+ } from "@react-navigation/native" ;
8+ import { useCallback , useState } from "react" ;
39import { ActivityIndicator , Linking , Pressable , View } from "react-native" ;
410import { WebView } from "react-native-webview" ;
511
612import { AppText as Text } from "../../../components/AppText" ;
713import { LoadingStrip } from "../../../components/LoadingStrip" ;
814import { SymbolView } from "../../../components/AppSymbol" ;
915import { useThemeColor } from "../../../lib/useThemeColor" ;
10- import { LEGAL_URL } from "../lib/legal-document-url" ;
16+ import { isLegalDocumentUrl , LEGAL_URL } from "../lib/legal-document-url" ;
17+
18+ type SettingsLegalRouteParams = {
19+ SettingsLegal : { readonly externalUrl ?: string } | undefined ;
20+ } ;
1121
1222export function SettingsLegalDocumentCloseHeaderButton ( ) {
1323 const navigation = useNavigation ( ) ;
@@ -34,13 +44,18 @@ export function SettingsLegalDocumentCloseHeaderButton() {
3444
3545export function SettingsLegalDocumentExternalHeaderButton ( ) {
3646 const iconColor = useThemeColor ( "--color-icon" ) ;
47+ const route = useRoute < RouteProp < SettingsLegalRouteParams , "SettingsLegal" > > ( ) ;
48+ const externalUrl =
49+ route . params ?. externalUrl && isLegalDocumentUrl ( route . params . externalUrl )
50+ ? route . params . externalUrl
51+ : LEGAL_URL ;
3752
3853 return (
3954 < Pressable
4055 accessibilityLabel = "Open legal documents in external browser"
4156 accessibilityRole = "button"
4257 hitSlop = { 12 }
43- onPress = { ( ) => void Linking . openURL ( LEGAL_URL ) . catch ( ( ) => undefined ) }
58+ onPress = { ( ) => void Linking . openURL ( externalUrl ) . catch ( ( ) => undefined ) }
4459 className = "p-2 active:opacity-60"
4560 >
4661 < SymbolView
@@ -54,34 +69,17 @@ export function SettingsLegalDocumentExternalHeaderButton() {
5469 ) ;
5570}
5671
57- function webDocumentIdentity ( value : string ) : string | null {
58- try {
59- const url = new URL ( value ) ;
60- if ( url . protocol !== "https:" && url . protocol !== "http:" ) return null ;
61-
62- const pathname = url . pathname . replace ( / \/ + $ / , "" ) || "/" ;
63- return `${ url . origin } ${ pathname } ` ;
64- } catch {
65- return null ;
66- }
67- }
68-
6972interface SettingsLegalDocumentRouteScreenProps {
7073 readonly documentName : string ;
7174 readonly documentUrl : string ;
72- readonly allowedDocumentUrls : readonly string [ ] ;
7375}
7476
7577export function SettingsLegalDocumentRouteScreen ( {
7678 documentName,
7779 documentUrl,
78- allowedDocumentUrls,
7980} : SettingsLegalDocumentRouteScreenProps ) {
81+ const navigation = useNavigation < NavigationProp < ParamListBase > > ( ) ;
8082 const iconColor = useThemeColor ( "--color-icon" ) ;
81- const allowedDocumentIdentities = new Set (
82- allowedDocumentUrls . map ( webDocumentIdentity ) . filter ( ( value ) : value is string => value !== null ) ,
83- ) ;
84- const isInitialLoadRef = useRef ( true ) ;
8583 const [ reloadKey , setReloadKey ] = useState ( 0 ) ;
8684 const [ loadProgress , setLoadProgress ] = useState ( 0 ) ;
8785 const [ loadError , setLoadError ] = useState < string | null > ( null ) ;
@@ -111,7 +109,6 @@ export function SettingsLegalDocumentRouteScreen({
111109 < Pressable
112110 accessibilityRole = "button"
113111 onPress = { ( ) => {
114- isInitialLoadRef . current = true ;
115112 setLoadError ( null ) ;
116113 setReloadKey ( ( value ) => value + 1 ) ;
117114 } }
@@ -149,11 +146,7 @@ export function SettingsLegalDocumentRouteScreen({
149146 thirdPartyCookiesEnabled = { false }
150147 startInLoadingState
151148 onShouldStartLoadWithRequest = { ( request ) => {
152- const requestIdentity = webDocumentIdentity ( request . url ) ;
153- const isConfiguredDocument =
154- requestIdentity !== null && allowedDocumentIdentities . has ( requestIdentity ) ;
155- const isInitialRedirect = isInitialLoadRef . current && request . navigationType === "other" ;
156- if ( isConfiguredDocument || isInitialRedirect ) return true ;
149+ if ( isLegalDocumentUrl ( request . url ) ) return true ;
157150
158151 openExternalUrl ( request . url ) ;
159152 return false ;
@@ -165,17 +158,18 @@ export function SettingsLegalDocumentRouteScreen({
165158 setLoadProgress ( 0.05 ) ;
166159 setLoadError ( null ) ;
167160 } }
168- onLoadEnd = { ( ) => {
169- isInitialLoadRef . current = false ;
161+ onLoadEnd = { ( event ) => {
162+ if ( isLegalDocumentUrl ( event . nativeEvent . url ) ) {
163+ navigation . setParams ( { externalUrl : event . nativeEvent . url } ) ;
164+ }
170165 setLoadProgress ( 0 ) ;
171166 } }
172167 onError = { ( event ) => {
173- isInitialLoadRef . current = false ;
174168 setLoadProgress ( 0 ) ;
175169 setLoadError ( event . nativeEvent . description || "The page could not be loaded." ) ;
176170 } }
177171 onHttpError = { ( event ) => {
178- isInitialLoadRef . current = false ;
172+ if ( ! isLegalDocumentUrl ( event . nativeEvent . url ) ) return ;
179173 setLoadProgress ( 0 ) ;
180174 setLoadError ( `The server returned status ${ event . nativeEvent . statusCode } .` ) ;
181175 } }
0 commit comments