@jsb188/react 1.1.10 → 1.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/states/modal.ts CHANGED
@@ -1,60 +1,66 @@
1
1
  import i18n from '@jsb188/app/i18n/index.ts';
2
2
  import type { ColorEnum, ServerErrorObj } from '@jsb188/app/types/app.d.ts';
3
- import { atom, useAtom, useSetAtom } from 'jotai';
4
- import { useCallback } from 'react';
3
+ import { atom, useAtom, useAtomValue, useSetAtom, useStore } from 'jotai';
4
+ import { useCallback, useEffect } from 'react';
5
+ import { setKeyboardLayerOpenState } from './keyboardLayer';
5
6
 
6
7
  /**
7
8
  * Types
8
9
  */
9
10
 
10
- interface ModalRequestParams {
11
- name: string;
12
- preset?: string;
13
- color?: ColorEnum | null;
14
- props?: Record<string, any>;
11
+ export interface ModalInitialContextProps<Context = Record<string, any>> {
12
+ // Already available display data; each screen keeps its live data source authoritative.
13
+ initialContext?: Context;
15
14
  }
16
15
 
17
- export interface ModalProps {
18
- name: string;
19
- preset: string | null;
20
- modalSessionKey?: string;
21
- [key: string]: any;
16
+ interface ModalRequestParams extends ModalInitialContextProps {
17
+ name: string;
18
+ preset?: string;
19
+ color?: ColorEnum | null;
20
+ props?: Record<string, any>;
21
+ }
22
+
23
+ export interface ModalProps extends ModalInitialContextProps {
24
+ name: string;
25
+ preset: string | null;
26
+ modalSessionKey?: string;
27
+ [key: string]: any;
22
28
  }
23
29
 
24
30
  export interface ModalPopUpComponentProps {
25
- // Can't extend ModalProps because [key: string]: any; nullifies the type checking
26
- name: string;
27
- preset: string | null;
31
+ // Can't extend ModalProps because [key: string]: any; nullifies the type checking
32
+ name: string;
33
+ preset: string | null;
28
34
  onCloseModal: () => void;
29
35
  }
30
36
 
31
37
  type ModalPropsFn = (prev: ModalProps | null) => ModalProps | null;
32
- type ModalScreenSetter = (
33
- value: ModalProps | null | ((prev: ModalProps | null) => ModalProps | null)
34
- ) => void;
38
+ type ModalScreenSetter = (value: ModalProps | null | ((prev: ModalProps | null) => ModalProps | null)) => void;
35
39
 
36
40
  export type OpenModalScreenFn = (data: ModalRequestParams | ModalPropsFn) => void;
37
41
 
38
42
  export type OpenModalPopUpFn = (data: ModalRequestParams | null, err?: Partial<ServerErrorObj>) => void;
39
43
 
44
+ export type CloseModalPopUpFn = (modalSessionKey?: string) => void;
45
+
40
46
  export interface ModalScreenProps {
41
- screen: ModalProps | null;
42
- openModalScreen: OpenModalScreenFn;
43
- closeModalScreen: () => void;
47
+ screen: ModalProps | null;
48
+ openModalScreen: OpenModalScreenFn;
49
+ closeModalScreen: () => void;
44
50
  }
45
51
 
46
52
  export interface ModalPopUpProps {
47
- popUp: ModalProps | null;
48
- openModalPopUp: OpenModalPopUpFn;
49
- closeModalPopUp: () => void;
53
+ popUp: ModalProps | null;
54
+ openModalPopUp: OpenModalPopUpFn;
55
+ closeModalPopUp: CloseModalPopUpFn;
50
56
  }
51
57
 
52
58
  export interface ModalHandlerProps {
53
- color?: ColorEnum | null;
54
- openModalScreen: OpenModalScreenFn;
55
- openModalPopUp: OpenModalPopUpFn;
56
- closeModalScreen: () => void;
57
- closeModalPopUp: () => void;
59
+ color?: ColorEnum | null;
60
+ openModalScreen: OpenModalScreenFn;
61
+ openModalPopUp: OpenModalPopUpFn;
62
+ closeModalScreen: () => void;
63
+ closeModalPopUp: CloseModalPopUpFn;
58
64
  }
59
65
 
60
66
  export interface ModalHookProps extends ModalScreenProps, ModalPopUpProps {}
@@ -65,96 +71,115 @@ export interface ModalHookProps extends ModalScreenProps, ModalPopUpProps {}
65
71
  */
66
72
 
67
73
  export const POPUP_DEFAULT_VALUES = {
68
- app_error: {
69
- iconName: 'file-broken',
70
- title: 'error.app_error_title',
71
- message: 'error.app_error',
72
- },
73
- chat_not_live_feature: {
74
- iconName: 'message-cancel',
75
- title: 'chat.is_offline',
76
- message: 'chat.must_be_live_feature',
77
- },
78
- not_verified_email: {
79
- iconName: 'mailbox',
80
- title: 'form.check_email',
81
- message: 'auth.not_verified_yet',
82
- },
83
- not_allowed_permission: {
84
- iconName: 'lock',
85
- title: 'error.not_allowed',
86
- message: 'error.not_allowed_perms',
87
- },
88
- network_error: {
89
- iconName: 'wifi-off',
90
- title: 'error.network_error_title',
91
- message: 'error.network_error',
92
- },
93
- external_link: {
94
- iconName: 'outbound',
95
- title: 'app.leaving_app',
96
- message: 'app.leaving_app_desc',
97
- confirmText: 'app.link_yes',
98
- },
99
- '20020': {
100
- // Not allowed in chat
101
- iconName: 'lock-square-rounded-filled',
102
- },
103
- '10000': {
104
- preset: 'force_reload',
105
- // iconName: '..', // This is controlled via i18n now
106
- },
107
- '10012': {
108
- preset: 'force_reload',
109
- title: 'error.login_changed',
110
- iconName: 'question-circle', // Deprecated; icon names are controlled via i18n now
111
- },
74
+ app_error: {
75
+ iconName: 'file-broken',
76
+ title: 'error.app_error_title',
77
+ message: 'error.app_error',
78
+ },
79
+ chat_not_live_feature: {
80
+ iconName: 'message-cancel',
81
+ title: 'chat.is_offline',
82
+ message: 'chat.must_be_live_feature',
83
+ },
84
+ not_verified_email: {
85
+ iconName: 'mailbox',
86
+ title: 'c.check_email',
87
+ message: 'auth.not_verified_yet',
88
+ },
89
+ not_allowed_permission: {
90
+ iconName: 'lock',
91
+ title: 'error.not_allowed',
92
+ message: 'error.not_allowed_perms',
93
+ },
94
+ network_error: {
95
+ iconName: 'wifi-off',
96
+ title: 'error.network_error_title',
97
+ message: 'error.network_error',
98
+ },
99
+ external_link: {
100
+ iconName: 'outbound',
101
+ title: 'app.leaving_app',
102
+ message: 'app.leaving_app_desc',
103
+ confirmText: 'app.link_yes',
104
+ },
112
105
  } as Record<string, Record<string, string>>;
113
106
 
107
+ const modalScreenCloseGuardsState = atom<string[]>([]);
108
+
109
+ /*
110
+ * Add or remove one mounted modal-screen close guard without disturbing other owners.
111
+ */
112
+ const updateModalScreenCloseGuardState = atom(null, (get, set, p: { active: boolean; guardId: string }) => {
113
+ const closeGuards = get(modalScreenCloseGuardsState);
114
+ set(
115
+ modalScreenCloseGuardsState,
116
+ p.active
117
+ ? closeGuards.includes(p.guardId)
118
+ ? closeGuards
119
+ : [p.guardId, ...closeGuards]
120
+ : closeGuards.filter((guardId) => guardId !== p.guardId),
121
+ );
122
+ });
123
+
114
124
  /**
115
125
  * Modal screen class
116
126
  */
117
127
 
118
128
  class ModalScreen {
119
- readonly name: string;
120
- readonly state = atom<ModalProps | null>(null);
121
- readonly defaultValues?: Record<string, any>;
122
-
123
- constructor(name: string, defaultValues?: Record<string, any>) {
124
- this.name = `modal_${name}_state`;
125
- if (defaultValues) {
126
- this.defaultValues = defaultValues;
127
- }
128
- }
129
-
130
- open(data: ModalRequestParams): ModalProps | null {
131
- if (data) {
132
- const { name, color, props } = data;
133
- const values = this.defaultValues?.[name ?? ''];
134
-
135
- return {
136
- name,
137
- color,
138
- ...values,
139
- ...props,
140
- modalSessionKey: getModalScreenSessionKey(),
141
- };
142
- }
143
-
144
- return null;
145
- }
146
-
147
- close() {
148
- return null;
149
- }
129
+ readonly name: string;
130
+ readonly state;
131
+ readonly defaultValues?: Record<string, any>;
132
+
133
+ constructor(name: string, defaultValues?: Record<string, any>) {
134
+ this.name = `modal_${name}_state`;
135
+ const valueState = atom<ModalProps | null>(null);
136
+ this.state = atom(
137
+ (get) => get(valueState),
138
+ (get, set, value: ModalProps | null | ((previous: ModalProps | null) => ModalProps | null)) => {
139
+ const previous = get(valueState);
140
+ const next = typeof value === 'function' ? value(previous) : value;
141
+ set(valueState, next);
142
+ set(setKeyboardLayerOpenState, {
143
+ layer: name === 'screen' ? 'modal' : 'popup',
144
+ open: !!next,
145
+ reset: !!previous && !!next && (name === 'screen' ? previous.modalSessionKey !== next.modalSessionKey : previous !== next),
146
+ });
147
+ },
148
+ );
149
+ if (defaultValues) {
150
+ this.defaultValues = defaultValues;
151
+ }
152
+ }
153
+
154
+ open(data: ModalRequestParams): ModalProps | null {
155
+ if (data) {
156
+ const { name, color, props, initialContext } = data;
157
+ const values = this.defaultValues?.[name ?? ''];
158
+
159
+ return {
160
+ name,
161
+ color,
162
+ ...values,
163
+ ...props,
164
+ initialContext,
165
+ modalSessionKey: getModalSessionKey(),
166
+ };
167
+ }
168
+
169
+ return null;
170
+ }
171
+
172
+ close() {
173
+ return null;
174
+ }
150
175
  }
151
176
 
152
177
  /*
153
- * Create a unique session key for each fresh modal screen open request.
178
+ * Create a unique session key for each fresh modal screen or popup open request.
154
179
  */
155
180
 
156
- function getModalScreenSessionKey() {
157
- return `modal-screen-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
181
+ function getModalSessionKey() {
182
+ return `modal-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
158
183
  }
159
184
 
160
185
  /**
@@ -162,79 +187,89 @@ function getModalScreenSessionKey() {
162
187
  */
163
188
 
164
189
  class ModalPopUp extends ModalScreen {
165
- readonly name: string = 'modal_popup_state';
166
-
167
- open(data: ModalRequestParams | null, error?: Partial<ServerErrorObj>): ModalProps | null {
168
- if (data) {
169
- const { preset, name, props } = data;
170
- const values = this.defaultValues?.[name ?? ''];
171
-
172
- let updatedAlertValue: ModalProps = {
173
- // Template values
174
- name,
175
- preset: preset || 'normal',
176
- // Alert props
177
- id: `alert-${name || 'normal'}`,
178
- iconName: null,
179
- title: '',
180
- message: '',
181
- };
182
-
183
- if (values?.iconName) {
184
- updatedAlertValue.iconName = values.iconName;
185
- }
186
-
187
- // NOTE: i18n() isn't ready yet, so I have to use i18n.t(..) in the logic
188
- ['title', 'message', 'confirmText', 'cancelText'].forEach((key) => {
189
- if (values?.[key]) {
190
- updatedAlertValue[key] = i18n.t(values[key]);
191
- } else if (key === 'message' && i18n.has(`error.${name}`)) {
192
- updatedAlertValue[key] = i18n.t(`error.${name}`);
193
- } else if (i18n.has(`error.${name}_${key}`)) {
194
- updatedAlertValue[key] = i18n.t(`error.${name}_${key}`);
195
- }
196
- });
197
-
198
- if (values?.preset) {
199
- updatedAlertValue.preset = values.preset;
200
- }
201
-
202
- if (props) {
203
- updatedAlertValue = {
204
- ...updatedAlertValue,
205
- ...props,
206
- };
207
- }
208
-
209
- if (!updatedAlertValue.title && !isNaN(Number(name))) {
210
- updatedAlertValue.title = i18n.t('error.error');
211
- }
212
-
213
- return updatedAlertValue;
214
-
215
- } else if (error) {
216
- const { errorCode } = error;
217
- const { iconName: defIconName, title: defTitle, message: defMessage, ...values } = this.defaultValues?.[errorCode ?? ''] || {};
218
-
219
- return {
220
- // Template values
221
- name: 'ERROR',
222
- preset: 'normal',
223
- // Alert props
224
- id: `gql-${errorCode}`,
225
- iconName: error.iconName || defIconName,
226
- title: error.title || defTitle || i18n.t('error.error'),
227
- message: error.message || defMessage || i18n.t('error.unknown_error_msg'),
228
- ...values,
229
- };
230
- }
231
-
232
- return null;
233
- }
234
-
235
- close() {
236
- return null;
237
- }
190
+ readonly name: string = 'modal_popup_state';
191
+
192
+ open(data: ModalRequestParams | null, error?: Partial<ServerErrorObj>): ModalProps | null {
193
+ if (data) {
194
+ const { preset, name, props, initialContext } = data;
195
+ const values = this.defaultValues?.[name ?? ''];
196
+ const translatedPreset = i18n.has(`error.${name}_preset`) ? i18n.t(`error.${name}_preset`) : null;
197
+
198
+ let updatedAlertValue: ModalProps = {
199
+ // Template values
200
+ name,
201
+ preset: preset || translatedPreset || 'normal',
202
+ // Alert props
203
+ id: `alert-${name || 'normal'}`,
204
+ iconName: null,
205
+ title: '',
206
+ message: '',
207
+ };
208
+
209
+ if (values?.iconName) {
210
+ updatedAlertValue.iconName = values.iconName;
211
+ } else if (i18n.has(`error.${name}_icon`)) {
212
+ updatedAlertValue.iconName = i18n.t(`error.${name}_icon`);
213
+ }
214
+
215
+ // NOTE: i18n() isn't ready yet, so I have to use i18n.t(..) in the logic
216
+ ['title', 'message', 'confirmText', 'cancelText'].forEach((key) => {
217
+ if (values?.[key]) {
218
+ updatedAlertValue[key] = i18n.t(values[key]);
219
+ } else if (key === 'message' && i18n.has(`error.${name}`)) {
220
+ updatedAlertValue[key] = i18n.t(`error.${name}`);
221
+ } else if (i18n.has(`error.${name}_${key}`)) {
222
+ updatedAlertValue[key] = i18n.t(`error.${name}_${key}`);
223
+ }
224
+ });
225
+
226
+ if (values?.preset) {
227
+ updatedAlertValue.preset = values.preset;
228
+ }
229
+
230
+ if (props) {
231
+ updatedAlertValue = {
232
+ ...updatedAlertValue,
233
+ ...props,
234
+ };
235
+ }
236
+
237
+ if (!updatedAlertValue.title && !Number.isNaN(Number(name))) {
238
+ updatedAlertValue.title = i18n.t('error.error');
239
+ }
240
+
241
+ return { ...updatedAlertValue, initialContext };
242
+ } else if (error) {
243
+ const { errorCode } = error;
244
+ const {
245
+ iconName: defIconName,
246
+ preset: defPreset,
247
+ title: defTitle,
248
+ message: defMessage,
249
+ ...values
250
+ } = this.defaultValues?.[errorCode ?? ''] || {};
251
+ const translatedIconName = errorCode && i18n.has(`error.${errorCode}_icon`) ? i18n.t(`error.${errorCode}_icon`) : undefined; // Use undefined to show the default icon; null intentionally hides it.
252
+ const translatedPreset = errorCode && i18n.has(`error.${errorCode}_preset`) ? i18n.t(`error.${errorCode}_preset`) : null;
253
+
254
+ return {
255
+ // Template values
256
+ name: 'ERROR',
257
+ preset: translatedPreset || defPreset || 'normal',
258
+ // Alert props
259
+ id: `gql-${errorCode}`,
260
+ iconName: error.iconName || defIconName || translatedIconName,
261
+ title: error.title || defTitle || i18n.t('error.error'),
262
+ message: error.message || defMessage || i18n.t('error.unknown_error_msg'),
263
+ ...values,
264
+ };
265
+ }
266
+
267
+ return null;
268
+ }
269
+
270
+ close() {
271
+ return null;
272
+ }
238
273
  }
239
274
 
240
275
  /**
@@ -244,6 +279,13 @@ class ModalPopUp extends ModalScreen {
244
279
  const screenClass = new ModalScreen('screen');
245
280
  const popUpClass = new ModalPopUp('popup', POPUP_DEFAULT_VALUES);
246
281
 
282
+ /*
283
+ * Return the resolved preset of a pop up request (e.g. 'force_reload') without opening it.
284
+ */
285
+ export function getModalPopUpPreset(data: ModalRequestParams | null): string | null {
286
+ return popUpClass.open(data)?.preset || null;
287
+ }
288
+
247
289
  /**
248
290
  * Create handler functions
249
291
  */
@@ -252,22 +294,16 @@ const popUpClass = new ModalPopUp('popup', POPUP_DEFAULT_VALUES);
252
294
  * Build the next modal screen state from either a direct open request or a state updater function.
253
295
  */
254
296
 
255
- function getNextModalScreenState(
256
- data: ModalRequestParams | ModalPropsFn,
257
- prevScreen: ModalProps | null
258
- ) {
259
- const isStateUpdater = typeof data === 'function';
260
- const nextScreenValue = isStateUpdater ? data(prevScreen) : screenClass.open(data);
261
-
262
- return (
263
- isStateUpdater &&
264
- nextScreenValue &&
265
- prevScreen?.modalSessionKey &&
266
- !nextScreenValue.modalSessionKey
267
- ) ? {
268
- ...nextScreenValue,
269
- modalSessionKey: prevScreen.modalSessionKey,
270
- } : nextScreenValue;
297
+ function getNextModalScreenState(data: ModalRequestParams | ModalPropsFn, prevScreen: ModalProps | null) {
298
+ const isStateUpdater = typeof data === 'function';
299
+ const nextScreenValue = isStateUpdater ? data(prevScreen) : screenClass.open(data);
300
+
301
+ return isStateUpdater && nextScreenValue && prevScreen?.modalSessionKey && !nextScreenValue.modalSessionKey
302
+ ? {
303
+ ...nextScreenValue,
304
+ modalSessionKey: prevScreen.modalSessionKey,
305
+ }
306
+ : nextScreenValue;
271
307
  }
272
308
 
273
309
  /*
@@ -275,41 +311,57 @@ function getNextModalScreenState(
275
311
  */
276
312
 
277
313
  function composeOpenModalScreenFn(setScreen: ModalScreenSetter) {
278
- return (data: ModalRequestParams | ModalPropsFn) => {
279
- setScreen((prevScreen) => {
280
- const nextScreenState = getNextModalScreenState(data, prevScreen);
281
-
282
- if (nextScreenState) {
283
- return nextScreenState;
284
- }
285
-
286
- console.warn('Invalid {screen} data:', data);
287
- return prevScreen;
288
- });
289
- };
314
+ return (data: ModalRequestParams | ModalPropsFn) => {
315
+ setScreen((prevScreen) => {
316
+ const nextScreenState = getNextModalScreenState(data, prevScreen);
317
+
318
+ if (nextScreenState) {
319
+ return nextScreenState;
320
+ }
321
+
322
+ console.warn('Invalid {screen} data:', data);
323
+ return prevScreen;
324
+ });
325
+ };
290
326
  }
291
327
 
292
328
  function composeCloseModalScreenFn(setScreen: (value: ModalProps | null) => void) {
293
- return () => {
294
- setScreen(screenClass.close());
295
- };
329
+ return () => {
330
+ setScreen(screenClass.close());
331
+ };
296
332
  }
297
333
 
298
- function composeOpenModalPopUpFn(setPopUp: (value: ModalProps | null) => void) {
299
- return (data: ModalRequestParams | null, err?: Partial<ServerErrorObj>) => {
300
- const nextPopUpState = popUpClass.open(data, err);
301
- if (nextPopUpState) {
302
- setPopUp(nextPopUpState);
303
- } else {
304
- console.warn('Invalid {popUp} data:', data, err);
305
- }
306
- };
334
+ /*
335
+ * Open a popup without letting background alerts replace a required browser reload.
336
+ */
337
+ function composeOpenModalPopUpFn(setPopUp: ModalScreenSetter) {
338
+ return (data: ModalRequestParams | null, err?: Partial<ServerErrorObj>) => {
339
+ const nextPopUpState = popUpClass.open(data, err);
340
+ if (nextPopUpState) {
341
+ setPopUp((previous) =>
342
+ previous?.preset === 'force_reload' ? previous : { ...nextPopUpState, modalSessionKey: getModalSessionKey() },
343
+ );
344
+ } else {
345
+ console.warn('Invalid {popUp} data:', data, err);
346
+ }
347
+ };
307
348
  }
308
349
 
309
- function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void) {
310
- return () => {
311
- setPopUp(popUpClass.close());
312
- };
350
+ /*
351
+ * Dismiss the active popup or a matching instance, retaining reload popups if the browser cancels navigation.
352
+ */
353
+ function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void, getPopUp: () => ModalProps | null) {
354
+ return (modalSessionKey?: string) => {
355
+ const popUp = getPopUp();
356
+ if (modalSessionKey !== undefined && popUp?.modalSessionKey !== modalSessionKey) {
357
+ return;
358
+ }
359
+ if (popUp?.preset === 'force_reload') {
360
+ globalThis.location?.reload();
361
+ return;
362
+ }
363
+ setPopUp(popUpClass.close());
364
+ };
313
365
  }
314
366
 
315
367
  /**
@@ -317,15 +369,56 @@ function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void)
317
369
  */
318
370
 
319
371
  export function useModalScreen(): ModalScreenProps {
320
- const [screen, setScreen] = useAtom(screenClass.state);
321
- const openModalScreen = useCallback(composeOpenModalScreenFn(setScreen), [setScreen]);
322
- const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
323
-
324
- return {
325
- screen,
326
- openModalScreen,
327
- closeModalScreen,
328
- };
372
+ const [screen, setScreen] = useAtom(screenClass.state);
373
+ const openModalScreen = useCallback(composeOpenModalScreenFn(setScreen), [setScreen]);
374
+ const closeModalScreen = useCallback(composeCloseModalScreenFn(setScreen), []);
375
+
376
+ return {
377
+ screen,
378
+ openModalScreen,
379
+ closeModalScreen,
380
+ };
381
+ }
382
+
383
+ /*
384
+ * Read the current modal screen during deferred cleanup without capturing an outdated render.
385
+ */
386
+ export function useGetModalScreen() {
387
+ const store = useStore();
388
+ return useCallback(() => store.get(screenClass.state), [store]);
389
+ }
390
+
391
+ /*
392
+ * Read popup ownership synchronously before delivering events to a potentially replaced popup.
393
+ */
394
+ export function useGetModalPopUp() {
395
+ const store = useStore();
396
+ return useCallback(() => store.get(popUpClass.state), [store]);
397
+ }
398
+
399
+ /*
400
+ * Keep global user-driven dismissal from closing a modal screen during a protected operation.
401
+ */
402
+ export function useModalScreenCloseGuard(guardId: string, active: boolean) {
403
+ const updateCloseGuard = useSetAtom(updateModalScreenCloseGuardState);
404
+
405
+ useEffect(() => {
406
+ if (!active) {
407
+ return;
408
+ }
409
+
410
+ updateCloseGuard({ active: true, guardId });
411
+ return () => {
412
+ updateCloseGuard({ active: false, guardId });
413
+ };
414
+ }, [active, guardId, updateCloseGuard]);
415
+ }
416
+
417
+ /*
418
+ * Return whether any mounted flow currently protects the active modal screen from user dismissal.
419
+ */
420
+ export function useModalScreenCloseGuarded() {
421
+ return useAtomValue(modalScreenCloseGuardsState).length > 0;
329
422
  }
330
423
 
331
424
  /**
@@ -333,10 +426,10 @@ export function useModalScreen(): ModalScreenProps {
333
426
  */
334
427
 
335
428
  export function useOpenModalScreen(): OpenModalScreenFn {
336
- const setScreen = useSetAtom(screenClass.state);
337
- const openModalScreen = useCallback(composeOpenModalScreenFn(setScreen), [setScreen]);
429
+ const setScreen = useSetAtom(screenClass.state);
430
+ const openModalScreen = useCallback(composeOpenModalScreenFn(setScreen), [setScreen]);
338
431
 
339
- return openModalScreen;
432
+ return openModalScreen;
340
433
  }
341
434
 
342
435
  /**
@@ -344,15 +437,19 @@ export function useOpenModalScreen(): OpenModalScreenFn {
344
437
  */
345
438
 
346
439
  export function useModalPopUp(): ModalPopUpProps {
347
- const [popUp, setPopUp] = useAtom(popUpClass.state);
348
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
349
- const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
350
-
351
- return {
352
- popUp,
353
- openModalPopUp,
354
- closeModalPopUp,
355
- };
440
+ const store = useStore();
441
+ const [popUp, setPopUp] = useAtom(popUpClass.state);
442
+ const openModalPopUp = useCallback(composeOpenModalPopUpFn(setPopUp), []);
443
+ const closeModalPopUp = useCallback(
444
+ composeCloseModalPopUpFn(setPopUp, () => store.get(popUpClass.state)),
445
+ [store],
446
+ );
447
+
448
+ return {
449
+ popUp,
450
+ openModalPopUp,
451
+ closeModalPopUp,
452
+ };
356
453
  }
357
454
 
358
455
  /**
@@ -360,20 +457,24 @@ export function useModalPopUp(): ModalPopUpProps {
360
457
  */
361
458
 
362
459
  export function useModalHandlers(): ModalHandlerProps {
363
- const setScreen = useSetAtom(screenClass.state);
364
- const setPopUp = useSetAtom(popUpClass.state);
365
-
366
- const openModalScreen = useCallback(composeOpenModalScreenFn(setScreen), [setScreen]);
367
- const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
368
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
369
- const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
370
-
371
- return {
372
- openModalScreen,
373
- closeModalScreen,
374
- openModalPopUp,
375
- closeModalPopUp,
376
- };
460
+ const store = useStore();
461
+ const setScreen = useSetAtom(screenClass.state);
462
+ const setPopUp = useSetAtom(popUpClass.state);
463
+
464
+ const openModalScreen = useCallback(composeOpenModalScreenFn(setScreen), [setScreen]);
465
+ const closeModalScreen = useCallback(composeCloseModalScreenFn(setScreen), []);
466
+ const openModalPopUp = useCallback(composeOpenModalPopUpFn(setPopUp), []);
467
+ const closeModalPopUp = useCallback(
468
+ composeCloseModalPopUpFn(setPopUp, () => store.get(popUpClass.state)),
469
+ [store],
470
+ );
471
+
472
+ return {
473
+ openModalScreen,
474
+ closeModalScreen,
475
+ openModalPopUp,
476
+ closeModalPopUp,
477
+ };
377
478
  }
378
479
 
379
480
  /**
@@ -381,10 +482,10 @@ export function useModalHandlers(): ModalHandlerProps {
381
482
  */
382
483
 
383
484
  export function useOpenModalPopUp(): OpenModalPopUpFn {
384
- const setPopUp = useSetAtom(popUpClass.state);
385
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
485
+ const setPopUp = useSetAtom(popUpClass.state);
486
+ const openModalPopUp = useCallback(composeOpenModalPopUpFn(setPopUp), []);
386
487
 
387
- return openModalPopUp;
488
+ return openModalPopUp;
388
489
  }
389
490
 
390
491
  /**
@@ -392,11 +493,11 @@ export function useOpenModalPopUp(): OpenModalPopUpFn {
392
493
  */
393
494
 
394
495
  export function useModal(): ModalHookProps {
395
- const screenProps = useModalScreen();
396
- const popUpProps = useModalPopUp();
496
+ const screenProps = useModalScreen();
497
+ const popUpProps = useModalPopUp();
397
498
 
398
- return {
399
- ...screenProps,
400
- ...popUpProps,
401
- };
499
+ return {
500
+ ...screenProps,
501
+ ...popUpProps,
502
+ };
402
503
  }