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" ;
1+ import { type NavigationProp , type ParamListBase , useNavigation } from "@react-navigation/native" ;
2+ import { useCallback , useLayoutEffect , useState } from "react" ;
93import { ActivityIndicator , Linking , Pressable , View } from "react-native" ;
104import { WebView } from "react-native-webview" ;
115
@@ -15,10 +9,6 @@ import { SymbolView } from "../../../components/AppSymbol";
159import { useThemeColor } from "../../../lib/useThemeColor" ;
1610import { isLegalDocumentUrl , LEGAL_URL } from "../lib/legal-document-url" ;
1711
18- type SettingsLegalRouteParams = {
19- SettingsLegal : { readonly externalUrl ?: string } | undefined ;
20- } ;
21-
2212export function SettingsLegalDocumentCloseHeaderButton ( ) {
2313 const navigation = useNavigation ( ) ;
2414 const iconColor = useThemeColor ( "--color-icon" ) ;
@@ -42,20 +32,20 @@ export function SettingsLegalDocumentCloseHeaderButton() {
4232 ) ;
4333}
4434
45- export function SettingsLegalDocumentExternalHeaderButton ( ) {
35+ export function SettingsLegalDocumentExternalHeaderButton ( {
36+ externalUrl = LEGAL_URL ,
37+ } : {
38+ readonly externalUrl ?: string ;
39+ } ) {
4640 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 ;
41+ const safeExternalUrl = isLegalDocumentUrl ( externalUrl ) ? externalUrl : LEGAL_URL ;
5242
5343 return (
5444 < Pressable
5545 accessibilityLabel = "Open legal documents in external browser"
5646 accessibilityRole = "button"
5747 hitSlop = { 12 }
58- onPress = { ( ) => void Linking . openURL ( externalUrl ) . catch ( ( ) => undefined ) }
48+ onPress = { ( ) => void Linking . openURL ( safeExternalUrl ) . catch ( ( ) => undefined ) }
5949 className = "p-2 active:opacity-60"
6050 >
6151 < SymbolView
@@ -83,6 +73,17 @@ export function SettingsLegalDocumentRouteScreen({
8373 const [ reloadKey , setReloadKey ] = useState ( 0 ) ;
8474 const [ loadProgress , setLoadProgress ] = useState ( 0 ) ;
8575 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 ] ) ;
8687
8788 const openExternalUrl = useCallback ( ( url : string ) => {
8889 void Linking . openURL ( url ) . catch ( ( ) => undefined ) ;
@@ -160,7 +161,7 @@ export function SettingsLegalDocumentRouteScreen({
160161 } }
161162 onLoadEnd = { ( event ) => {
162163 if ( isLegalDocumentUrl ( event . nativeEvent . url ) ) {
163- navigation . setParams ( { externalUrl : event . nativeEvent . url } ) ;
164+ setExternalUrl ( event . nativeEvent . url ) ;
164165 }
165166 setLoadProgress ( 0 ) ;
166167 } }
0 commit comments