@@ -14,7 +14,7 @@ import {
1414import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider" ;
1515import { useScaledTextRole } from "../../features/settings/appearance/useScaledTextRole" ;
1616
17- import { useCallback , useEffect , useRef , useState } from "react" ;
17+ import { useSyncExternalStore } from "react" ;
1818import { themeColorWithContrast } from "../../lib/mobileTheme" ;
1919import type { Alert , AlertButton } from "react-native" ;
2020import type { ConfirmDialogRequest , TextInputDialogRequest } from "./Dialog.types" ;
@@ -33,63 +33,78 @@ type DialogRequest =
3333 | { readonly kind : "confirm" ; readonly request : ConfirmDialogRequest }
3434 | { readonly kind : "text-input" ; readonly request : TextInputDialogRequest } ;
3535
36- let presentRequest : ( ( request : DialogRequest ) => void ) | null = null ;
36+ type PresentedDialog = DialogRequest & { readonly id : number } ;
37+
38+ // App services raise dialogs before the root mounts, so the request lives in
39+ // the module and the Host renders whatever is current. One dialog shows at a
40+ // time: a new request replaces the presented one.
41+ let presented : PresentedDialog | null = null ;
42+ let nextPresentationId = 0 ;
43+ const listeners = new Set < ( ) => void > ( ) ;
44+
45+ function setPresented ( next : PresentedDialog | null ) {
46+ presented = next ;
47+ for ( const listener of listeners ) listener ( ) ;
48+ }
49+
50+ function submitRequest ( dialog : DialogRequest ) {
51+ setPresented ( { ...dialog , id : ++ nextPresentationId } ) ;
52+ }
53+
54+ /** Closes the dialog only if it is still the one the action came from. */
55+ function dismiss ( id : number ) {
56+ if ( presented ?. id === id ) setPresented ( null ) ;
57+ }
58+
59+ function subscribe ( listener : ( ) => void ) {
60+ listeners . add ( listener ) ;
61+ return ( ) => {
62+ listeners . delete ( listener ) ;
63+ } ;
64+ }
65+
66+ function getPresented ( ) {
67+ return presented ;
68+ }
3769
3870export function alert ( ...[ title , message , buttons , options ] : Parameters < typeof Alert . alert > ) : void {
3971 const actions = buttons ?. length ? buttons . slice ( 0 , 3 ) : [ { text : "OK" } ] ;
4072 const normalized = actions . map ( ( action , index ) =>
4173 index === actions . length - 1 ? { ...action , text : action . text || "OK" } : action ,
4274 ) ;
43- presentRequest ?. ( {
75+ submitRequest ( {
4476 kind : "alert" ,
4577 request : { title, message, buttons : normalized , options } ,
4678 } ) ;
4779}
4880
4981export function confirm ( request : ConfirmDialogRequest ) : void {
50- presentRequest ?. ( { kind : "confirm" , request } ) ;
82+ submitRequest ( { kind : "confirm" , request } ) ;
5183}
5284
5385export function prompt ( request : TextInputDialogRequest ) : void {
54- presentRequest ?. ( { kind : "text-input" , request } ) ;
86+ submitRequest ( { kind : "text-input" , request } ) ;
5587}
5688
5789export function Host ( ) {
58- const [ presented , setPresented ] = useState < ( DialogRequest & { readonly id : number } ) | null > (
59- null ,
60- ) ;
61- const [ inputValue , setInputValue ] = useState ( "" ) ;
62- const nextPresentationId = useRef ( 0 ) ;
63- useEffect ( ( ) => {
64- const present = ( request : DialogRequest ) => {
65- setInputValue ( request . kind === "text-input" ? request . request . initialValue : "" ) ;
66- setPresented ( { ...request , id : ++ nextPresentationId . current } ) ;
67- } ;
68- presentRequest = present ;
69- return ( ) => {
70- // When Android replaces the activity, the new root's Host registers before the old root
71- // unmounts. Clearing unconditionally would leave the live Host without dialogs.
72- if ( presentRequest === present ) presentRequest = null ;
73- } ;
74- } , [ ] ) ;
90+ const presented = useSyncExternalStore ( subscribe , getPresented , getPresented ) ;
7591
76- const handleCancel = useCallback ( ( ) => {
77- setPresented ( null ) ;
78- if ( presented ?. kind === "alert" ) presented . request . options ?. onDismiss ?.( ) ;
79- else presented ?. request . onCancel ?.( ) ;
80- } , [ presented ] ) ;
92+ const handleCancel = ( ) => {
93+ if ( ! presented ) return ;
94+ dismiss ( presented . id ) ;
95+ if ( presented . kind === "alert" ) presented . request . options ?. onDismiss ?.( ) ;
96+ else presented . request . onCancel ?.( ) ;
97+ } ;
8198
82- const handleConfirm = useCallback (
83- ( nativeInputValue ?: string ) => {
84- setPresented ( null ) ;
85- if ( presented ?. kind === "confirm" ) {
86- presented . request . onConfirm ( ) ;
87- } else if ( presented ?. kind === "text-input" ) {
88- presented . request . onConfirm ( nativeInputValue ?? inputValue ) ;
89- }
90- } ,
91- [ inputValue , presented ] ,
92- ) ;
99+ const handleConfirm = ( nativeInputValue ?: string ) => {
100+ if ( ! presented ) return ;
101+ dismiss ( presented . id ) ;
102+ if ( presented . kind === "confirm" ) {
103+ presented . request . onConfirm ( ) ;
104+ } else if ( presented . kind === "text-input" ) {
105+ presented . request . onConfirm ( nativeInputValue ?? presented . request . initialValue ) ;
106+ }
107+ } ;
93108
94109 if ( presented ?. kind === "alert" ) {
95110 return (
@@ -103,7 +118,7 @@ export function Host() {
103118 alertActions = { presented . request . buttons ?? [ { text : "OK" } ] }
104119 dismissable = { presented . request . options ?. cancelable ?? false }
105120 onAlertAction = { ( action ) => {
106- setPresented ( null ) ;
121+ dismiss ( presented . id ) ;
107122 action . onPress ?.( ) ;
108123 } }
109124 onCancel = { handleCancel }
@@ -120,7 +135,6 @@ export function Host() {
120135 presented . kind === "text-input" ? presented . request . initialValue : undefined
121136 }
122137 inputPlaceholder = { presented . kind === "text-input" ? presented . request . placeholder : undefined }
123- onInputChange = { setInputValue }
124138 onCancel = { handleCancel }
125139 onConfirm = { handleConfirm }
126140 />
@@ -212,7 +226,6 @@ function DialogPresentation(props: DialogPresentationProps) {
212226 singleLine
213227 value = { inputState }
214228 selection = { inputSelection }
215- onValueChange = { props . onInputChange }
216229 textStyle = { inputTypography }
217230 keyboardOptions = { { imeAction : "done" } }
218231 keyboardActions = { {
@@ -267,7 +280,6 @@ interface DialogPresentationProps {
267280 > ;
268281 readonly inputInitialValue ?: string ;
269282 readonly inputPlaceholder ?: string ;
270- readonly onInputChange ?: ( value : string ) => void ;
271283 readonly onCancel : ( ) => void ;
272284 readonly onConfirm : ( inputValue ?: string ) => void ;
273285}
0 commit comments