@jsb188/react 1.0.4 → 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';
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,80 +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
-
147
- if (data) {
148
- const { preset, name, props } = data;
149
- const values = this.defaultValues?.[name ?? ''];
150
-
151
- let updatedAlertValue: ModalProps = {
152
- // Template values
153
- name,
154
- preset: preset || 'normal',
155
- // Alert props
156
- id: `alert-${name || 'normal'}`,
157
- iconName: null,
158
- title: '',
159
- message: '',
160
- };
161
-
162
- if (values?.iconName) {
163
- updatedAlertValue.iconName = values.iconName;
164
- }
165
-
166
- // NOTE: i18n() isn't ready yet, so I have to use i18n.t(..) in the logic
167
- ['title', 'message', 'confirmText', 'cancelText'].forEach((key) => {
168
- if (values?.[key]) {
169
- updatedAlertValue[key] = i18n.t(values[key]);
170
- } else if (key === 'message' && i18n.has(`error.${name}`)) {
171
- updatedAlertValue[key] = i18n.t(`error.${name}`);
172
- } else if (i18n.has(`error.${name}_${key}`)) {
173
- updatedAlertValue[key] = i18n.t(`error.${name}_${key}`);
174
- }
175
- });
176
-
177
- if (values?.preset) {
178
- updatedAlertValue.preset = values.preset;
179
- }
180
-
181
- if (props) {
182
- updatedAlertValue = {
183
- ...updatedAlertValue,
184
- ...props,
185
- };
186
- }
187
-
188
- if (!updatedAlertValue.title && !isNaN(Number(name))) {
189
- updatedAlertValue.title = i18n.t('error.error');
190
- }
191
-
192
- return updatedAlertValue;
193
-
194
- } else if (error) {
195
- const { errorCode } = error;
196
- const { iconName: defIconName, title: defTitle, message: defMessage, ...values } = this.defaultValues?.[errorCode ?? ''] || {};
197
-
198
- return {
199
- // Template values
200
- name: 'ERROR',
201
- preset: 'normal',
202
- // Alert props
203
- id: `gql-${errorCode}`,
204
- iconName: error.iconName || defIconName,
205
- title: error.title || defTitle || i18n.t('error.error'),
206
- message: error.message || defMessage || i18n.t('error.unknown_error'),
207
- ...values,
208
- };
209
- }
210
-
211
- return null;
212
- }
213
-
214
- close() {
215
- return null;
216
- }
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
+ }
217
273
  }
218
274
 
219
275
  /**
@@ -223,42 +279,89 @@ class ModalPopUp extends ModalScreen {
223
279
  const screenClass = new ModalScreen('screen');
224
280
  const popUpClass = new ModalPopUp('popup', POPUP_DEFAULT_VALUES);
225
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
+
226
289
  /**
227
290
  * Create handler functions
228
291
  */
229
292
 
230
- function composeOpenModalScreenFn(setScreen: (value: ModalProps | null) => void) {
231
- return (data: ModalRequestParams) => {
232
- const nextScreenState = screenClass.open(data);
233
- if (nextScreenState) {
234
- setScreen(nextScreenState);
235
- } else {
236
- console.warn('Invalid {screen} data:', data);
237
- }
238
- };
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
+ };
239
326
  }
240
327
 
241
328
  function composeCloseModalScreenFn(setScreen: (value: ModalProps | null) => void) {
242
- return () => {
243
- setScreen(screenClass.close());
244
- };
329
+ return () => {
330
+ setScreen(screenClass.close());
331
+ };
245
332
  }
246
333
 
247
- function composeOpenModalPopUpFn(setPopUp: (value: ModalProps | null) => void) {
248
- return (data: ModalRequestParams | null, err?: ServerErrorObj) => {
249
- const nextPopUpState = popUpClass.open(data, err);
250
- if (nextPopUpState) {
251
- setPopUp(nextPopUpState);
252
- } else {
253
- console.warn('Invalid {popUp} data:', data, err);
254
- }
255
- };
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
+ };
256
348
  }
257
349
 
258
- function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void) {
259
- return () => {
260
- setPopUp(popUpClass.close());
261
- };
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
+ };
262
365
  }
263
366
 
264
367
  /**
@@ -266,15 +369,67 @@ function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void)
266
369
  */
267
370
 
268
371
  export function useModalScreen(): ModalScreenProps {
269
- const [screen, setScreen] = useAtom(screenClass.state);
270
- const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen), []);
271
- const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
272
-
273
- return {
274
- screen,
275
- openModalScreen,
276
- closeModalScreen,
277
- };
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;
278
433
  }
279
434
 
280
435
  /**
@@ -282,15 +437,19 @@ export function useModalScreen(): ModalScreenProps {
282
437
  */
283
438
 
284
439
  export function useModalPopUp(): ModalPopUpProps {
285
- const [popUp, setPopUp] = useAtom(popUpClass.state);
286
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
287
- const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
288
-
289
- return {
290
- popUp,
291
- openModalPopUp,
292
- closeModalPopUp,
293
- };
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
+ };
294
453
  }
295
454
 
296
455
  /**
@@ -298,20 +457,24 @@ export function useModalPopUp(): ModalPopUpProps {
298
457
  */
299
458
 
300
459
  export function useModalHandlers(): ModalHandlerProps {
301
- const setScreen = useSetAtom(screenClass.state);
302
- const setPopUp = useSetAtom(popUpClass.state);
303
-
304
- const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen), []);
305
- const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
306
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
307
- const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
308
-
309
- return {
310
- openModalScreen,
311
- closeModalScreen,
312
- openModalPopUp,
313
- closeModalPopUp,
314
- };
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
+ };
315
478
  }
316
479
 
317
480
  /**
@@ -319,10 +482,10 @@ export function useModalHandlers(): ModalHandlerProps {
319
482
  */
320
483
 
321
484
  export function useOpenModalPopUp(): OpenModalPopUpFn {
322
- const setPopUp = useSetAtom(popUpClass.state);
323
- const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
485
+ const setPopUp = useSetAtom(popUpClass.state);
486
+ const openModalPopUp = useCallback(composeOpenModalPopUpFn(setPopUp), []);
324
487
 
325
- return openModalPopUp;
488
+ return openModalPopUp;
326
489
  }
327
490
 
328
491
  /**
@@ -330,11 +493,11 @@ export function useOpenModalPopUp(): OpenModalPopUpFn {
330
493
  */
331
494
 
332
495
  export function useModal(): ModalHookProps {
333
- const screenProps = useModalScreen();
334
- const popUpProps = useModalPopUp();
496
+ const screenProps = useModalScreen();
497
+ const popUpProps = useModalPopUp();
335
498
 
336
- return {
337
- ...screenProps,
338
- ...popUpProps,
339
- };
499
+ return {
500
+ ...screenProps,
501
+ ...popUpProps,
502
+ };
340
503
  }