@jsb188/react 1.1.1 → 1.1.2

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,45 +1,66 @@
1
- import i18n from '@jsb188/app/i18n';
2
- import type { ServerErrorObj } from '@jsb188/app/types/app.d';
3
- import { atom, useAtom, useSetAtom } from 'jotai';
4
- import { useCallback } from 'react';
1
+ import i18n from '@jsb188/app/i18n/index.ts';
2
+ import type { ColorEnum, ServerErrorObj } from '@jsb188/app/types/app.d.ts';
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
- 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;
14
14
  }
15
15
 
16
- export interface ModalProps {
17
- name: string;
18
- preset: string | null;
19
- [key: string]: any;
16
+ interface ModalRequestParams extends ModalInitialContextProps {
17
+ name: string;
18
+ preset?: string;
19
+ color?: ColorEnum | null;
20
+ props?: Record<string, any>;
20
21
  }
21
22
 
22
- export type OpenModalScreenFn = (data: ModalRequestParams) => void;
23
+ export interface ModalProps extends ModalInitialContextProps {
24
+ name: string;
25
+ preset: string | null;
26
+ modalSessionKey?: string;
27
+ [key: string]: any;
28
+ }
29
+
30
+ export interface ModalPopUpComponentProps {
31
+ // Can't extend ModalProps because [key: string]: any; nullifies the type checking
32
+ name: string;
33
+ preset: string | null;
34
+ onCloseModal: () => void;
35
+ }
36
+
37
+ type ModalPropsFn = (prev: ModalProps | null) => ModalProps | null;
38
+ type ModalScreenSetter = (value: ModalProps | null | ((prev: ModalProps | null) => ModalProps | null)) => void;
39
+
40
+ export type OpenModalScreenFn = (data: ModalRequestParams | ModalPropsFn) => void;
41
+
42
+ export type OpenModalPopUpFn = (data: ModalRequestParams | null, err?: Partial<ServerErrorObj>) => void;
23
43
 
24
- export type OpenModalPopUpFn = (data: ModalRequestParams | null, err?: ServerErrorObj) => void;
44
+ export type CloseModalPopUpFn = (modalSessionKey?: string) => void;
25
45
 
26
46
  export interface ModalScreenProps {
27
- screen: ModalProps | null;
28
- openModalScreen: OpenModalScreenFn;
29
- closeModalScreen: () => void;
47
+ screen: ModalProps | null;
48
+ openModalScreen: OpenModalScreenFn;
49
+ closeModalScreen: () => void;
30
50
  }
31
51
 
32
52
  export interface ModalPopUpProps {
33
- popUp: ModalProps | null;
34
- openModalPopUp: OpenModalPopUpFn;
35
- closeModalPopUp: () => void;
53
+ popUp: ModalProps | null;
54
+ openModalPopUp: OpenModalPopUpFn;
55
+ closeModalPopUp: CloseModalPopUpFn;
36
56
  }
37
57
 
38
58
  export interface ModalHandlerProps {
39
- openModalScreen: OpenModalScreenFn;
40
- openModalPopUp: OpenModalPopUpFn;
41
- closeModalScreen: () => void;
42
- closeModalPopUp: () => void;
59
+ color?: ColorEnum | null;
60
+ openModalScreen: OpenModalScreenFn;
61
+ openModalPopUp: OpenModalPopUpFn;
62
+ closeModalScreen: () => void;
63
+ closeModalPopUp: CloseModalPopUpFn;
43
64
  }
44
65
 
45
66
  export interface ModalHookProps extends ModalScreenProps, ModalPopUpProps {}
@@ -50,89 +71,115 @@ export interface ModalHookProps extends ModalScreenProps, ModalPopUpProps {}
50
71
  */
51
72
 
52
73
  export const POPUP_DEFAULT_VALUES = {
53
- app_error: {
54
- iconName: 'file-broken',
55
- title: 'error.app_error_title',
56
- message: 'error.app_error',
57
- },
58
- chat_not_live_feature: {
59
- iconName: 'message-cancel',
60
- title: 'chat.is_offline',
61
- message: 'chat.must_be_live_feature',
62
- },
63
- not_verified_email: {
64
- iconName: 'mailbox',
65
- title: 'auth.check_email',
66
- message: 'auth.not_verified_yet',
67
- },
68
- not_allowed_permission: {
69
- iconName: 'lock',
70
- title: 'error.not_allowed',
71
- message: 'error.not_allowed_perms',
72
- },
73
- network_error: {
74
- iconName: 'wifi-off',
75
- title: 'error.network_error_title',
76
- message: 'error.network_error',
77
- },
78
- external_link: {
79
- iconName: 'outbound',
80
- title: 'app.leaving_app',
81
- message: 'app.leaving_app_desc',
82
- confirmText: 'app.link_yes',
83
- },
84
- '20020': {
85
- // Not allowed in chat
86
- iconName: 'lock-square-rounded-filled',
87
- },
88
- '20021': {
89
- iconName: 'mood-sad-dizzy',
90
- },
91
- '10000': {
92
- preset: 'force_reload',
93
- iconName: 'confetti',
94
- },
95
- '10012': {
96
- preset: 'force_reload',
97
- title: 'error.login_changed',
98
- iconName: 'user-question',
99
- },
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
+ },
100
105
  } as Record<string, Record<string, string>>;
101
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
+
102
124
  /**
103
125
  * Modal screen class
104
126
  */
105
127
 
106
128
  class ModalScreen {
107
- readonly name: string;
108
- readonly state = atom<ModalProps | null>(null);
109
- readonly defaultValues?: Record<string, any>;
110
-
111
- constructor(name: string, defaultValues?: Record<string, any>) {
112
- this.name = `modal_${name}_state`;
113
- if (defaultValues) {
114
- this.defaultValues = defaultValues;
115
- }
116
- }
117
-
118
- open(data: ModalRequestParams): ModalProps | null {
119
- if (data) {
120
- const { name, props } = data;
121
- const values = this.defaultValues?.[name ?? ''];
122
-
123
- return {
124
- name,
125
- ...values,
126
- ...props,
127
- };
128
- }
129
-
130
- return null;
131
- }
132
-
133
- close() {
134
- return null;
135
- }
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
+ }
175
+ }
176
+
177
+ /*
178
+ * Create a unique session key for each fresh modal screen or popup open request.
179
+ */
180
+
181
+ function getModalSessionKey() {
182
+ return `modal-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
136
183
  }
137
184
 
138
185
  /**
@@ -140,79 +187,89 @@ class ModalScreen {
140
187
  */
141
188
 
142
189
  class ModalPopUp extends ModalScreen {
143
- readonly name: string = 'modal_popup_state';
144
-
145
- open(data: ModalRequestParams | null, error?: ServerErrorObj): ModalProps | null {
146
- if (data) {
147
- const { preset, name, props } = data;
148
- const values = this.defaultValues?.[name ?? ''];
149
-
150
- let updatedAlertValue: ModalProps = {
151
- // Template values
152
- name,
153
- preset: preset || 'normal',
154
- // Alert props
155
- id: `alert-${name || 'normal'}`,
156
- iconName: null,
157
- title: '',
158
- message: '',
159
- };
160
-
161
- if (values?.iconName) {
162
- updatedAlertValue.iconName = values.iconName;
163
- }
164
-
165
- // NOTE: i18n() isn't ready yet, so I have to use i18n.t(..) in the logic
166
- ['title', 'message', 'confirmText', 'cancelText'].forEach((key) => {
167
- if (values?.[key]) {
168
- updatedAlertValue[key] = i18n.t(values[key]);
169
- } else if (key === 'message' && i18n.has(`error.${name}`)) {
170
- updatedAlertValue[key] = i18n.t(`error.${name}`);
171
- } else if (i18n.has(`error.${name}_${key}`)) {
172
- updatedAlertValue[key] = i18n.t(`error.${name}_${key}`);
173
- }
174
- });
175
-
176
- if (values?.preset) {
177
- updatedAlertValue.preset = values.preset;
178
- }
179
-
180
- if (props) {
181
- updatedAlertValue = {
182
- ...updatedAlertValue,
183
- ...props,
184
- };
185
- }
186
-
187
- if (!updatedAlertValue.title && !isNaN(Number(name))) {
188
- updatedAlertValue.title = i18n.t('error.error');
189
- }
190
-
191
- return updatedAlertValue;
192
-
193
- } else if (error) {
194
- const { errorCode } = error;
195
- const { iconName: defIconName, title: defTitle, message: defMessage, ...values } = this.defaultValues?.[errorCode ?? ''] || {};
196
-
197
- return {
198
- // Template values
199
- name: 'ERROR',
200
- preset: 'normal',
201
- // Alert props
202
- id: `gql-${errorCode}`,
203
- iconName: error.iconName || defIconName,
204
- title: error.title || defTitle || i18n.t('error.error'),
205
- message: error.message || defMessage || i18n.t('error.unknown_error'),
206
- ...values,
207
- };
208
- }
209
-
210
- return null;
211
- }
212
-
213
- close() {
214
- return null;
215
- }
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
+ }
216
273
  }
217
274
 
218
275
  /**
@@ -222,42 +279,89 @@ class ModalPopUp extends ModalScreen {
222
279
  const screenClass = new ModalScreen('screen');
223
280
  const popUpClass = new ModalPopUp('popup', POPUP_DEFAULT_VALUES);
224
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
+
225
289
  /**
226
290
  * Create handler functions
227
291
  */
228
292
 
229
- function composeOpenModalScreenFn(setScreen: (value: ModalProps | null) => void) {
230
- return (data: ModalRequestParams) => {
231
- const nextScreenState = screenClass.open(data);
232
- if (nextScreenState) {
233
- setScreen(nextScreenState);
234
- } else {
235
- console.warn('Invalid {screen} data:', data);
236
- }
237
- };
293
+ /*
294
+ * Build the next modal screen state from either a direct open request or a state updater function.
295
+ */
296
+
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;
307
+ }
308
+
309
+ /*
310
+ * Compose a stable modal screen open function that reads the latest screen state from the atom setter.
311
+ */
312
+
313
+ function composeOpenModalScreenFn(setScreen: ModalScreenSetter) {
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
+ };
238
326
  }
239
327
 
240
328
  function composeCloseModalScreenFn(setScreen: (value: ModalProps | null) => void) {
241
- return () => {
242
- setScreen(screenClass.close());
243
- };
329
+ return () => {
330
+ setScreen(screenClass.close());
331
+ };
244
332
  }
245
333
 
246
- function composeOpenModalPopUpFn(setPopUp: (value: ModalProps | null) => void) {
247
- return (data: ModalRequestParams | null, err?: ServerErrorObj) => {
248
- const nextPopUpState = popUpClass.open(data, err);
249
- if (nextPopUpState) {
250
- setPopUp(nextPopUpState);
251
- } else {
252
- console.warn('Invalid {popUp} data:', data, err);
253
- }
254
- };
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
+ };
255
348
  }
256
349
 
257
- function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void) {
258
- return () => {
259
- setPopUp(popUpClass.close());
260
- };
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
+ };
261
365
  }
262
366
 
263
367
  /**
@@ -265,15 +369,67 @@ function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void)
265
369
  */
266
370
 
267
371
  export function useModalScreen(): ModalScreenProps {
268
- const [screen, setScreen] = useAtom(screenClass.state);
269
- const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen), []);
270
- const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
271
-
272
- return {
273
- screen,
274
- openModalScreen,
275
- closeModalScreen,
276
- };
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;
422
+ }
423
+
424
+ /**
425
+ * Use screen open function
426
+ */
427
+
428
+ export function useOpenModalScreen(): OpenModalScreenFn {
429
+ const setScreen = useSetAtom(screenClass.state);
430
+ const openModalScreen = useCallback(composeOpenModalScreenFn(setScreen), [setScreen]);
431
+
432
+ return openModalScreen;
277
433
  }
278
434
 
279
435
  /**
@@ -281,15 +437,19 @@ export function useModalScreen(): ModalScreenProps {
281
437
  */
282
438
 
283
439
  export function useModalPopUp(): ModalPopUpProps {
284
- const [popUp, setPopUp] = useAtom(popUpClass.state);
285
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
286
- const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
287
-
288
- return {
289
- popUp,
290
- openModalPopUp,
291
- closeModalPopUp,
292
- };
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
+ };
293
453
  }
294
454
 
295
455
  /**
@@ -297,20 +457,24 @@ export function useModalPopUp(): ModalPopUpProps {
297
457
  */
298
458
 
299
459
  export function useModalHandlers(): ModalHandlerProps {
300
- const setScreen = useSetAtom(screenClass.state);
301
- const setPopUp = useSetAtom(popUpClass.state);
302
-
303
- const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen), []);
304
- const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
305
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
306
- const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
307
-
308
- return {
309
- openModalScreen,
310
- closeModalScreen,
311
- openModalPopUp,
312
- closeModalPopUp,
313
- };
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
+ };
314
478
  }
315
479
 
316
480
  /**
@@ -318,10 +482,10 @@ export function useModalHandlers(): ModalHandlerProps {
318
482
  */
319
483
 
320
484
  export function useOpenModalPopUp(): OpenModalPopUpFn {
321
- const setPopUp = useSetAtom(popUpClass.state);
322
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
485
+ const setPopUp = useSetAtom(popUpClass.state);
486
+ const openModalPopUp = useCallback(composeOpenModalPopUpFn(setPopUp), []);
323
487
 
324
- return openModalPopUp;
488
+ return openModalPopUp;
325
489
  }
326
490
 
327
491
  /**
@@ -329,11 +493,11 @@ export function useOpenModalPopUp(): OpenModalPopUpFn {
329
493
  */
330
494
 
331
495
  export function useModal(): ModalHookProps {
332
- const screenProps = useModalScreen();
333
- const popUpProps = useModalPopUp();
496
+ const screenProps = useModalScreen();
497
+ const popUpProps = useModalPopUp();
334
498
 
335
- return {
336
- ...screenProps,
337
- ...popUpProps,
338
- };
499
+ return {
500
+ ...screenProps,
501
+ ...popUpProps,
502
+ };
339
503
  }