@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/libs/README.md +75 -0
- package/libs/page-filters.helpers.ts +65 -0
- package/libs/page-filters.ts +223 -0
- package/libs/use-page-filters.ts +147 -0
- package/package.json +24 -20
- package/states/apiData.ts +18 -16
- package/states/appContext.ts +327 -312
- package/states/connected.ts +135 -11
- package/states/index.ts +2 -1
- package/states/keyDown.ts +148 -47
- package/states/keyboardLayer.ts +57 -0
- package/states/modal.ts +369 -268
- package/states/popOver.ts +374 -244
- package/states/reactiveEvents.ts +99 -0
- package/states/uploads.ts +39 -27
- package/types/PopOver.d.ts +552 -223
- package/utils/hooks.ts +387 -272
- package/utils/lazy.ts +183 -0
- package/states/toasts.ts +0 -30
- package/utils/schema.ts +0 -158
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
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
screen: ModalProps | null;
|
|
48
|
+
openModalScreen: OpenModalScreenFn;
|
|
49
|
+
closeModalScreen: () => void;
|
|
44
50
|
}
|
|
45
51
|
|
|
46
52
|
export interface ModalPopUpProps {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
53
|
+
popUp: ModalProps | null;
|
|
54
|
+
openModalPopUp: OpenModalPopUpFn;
|
|
55
|
+
closeModalPopUp: CloseModalPopUpFn;
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
export interface ModalHandlerProps {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
|
157
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
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
|
-
|
|
294
|
-
|
|
295
|
-
|
|
329
|
+
return () => {
|
|
330
|
+
setScreen(screenClass.close());
|
|
331
|
+
};
|
|
296
332
|
}
|
|
297
333
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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
|
-
|
|
337
|
-
|
|
429
|
+
const setScreen = useSetAtom(screenClass.state);
|
|
430
|
+
const openModalScreen = useCallback(composeOpenModalScreenFn(setScreen), [setScreen]);
|
|
338
431
|
|
|
339
|
-
|
|
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
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
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
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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
|
-
|
|
385
|
-
|
|
485
|
+
const setPopUp = useSetAtom(popUpClass.state);
|
|
486
|
+
const openModalPopUp = useCallback(composeOpenModalPopUpFn(setPopUp), []);
|
|
386
487
|
|
|
387
|
-
|
|
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
|
-
|
|
396
|
-
|
|
496
|
+
const screenProps = useModalScreen();
|
|
497
|
+
const popUpProps = useModalPopUp();
|
|
397
498
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
499
|
+
return {
|
|
500
|
+
...screenProps,
|
|
501
|
+
...popUpProps,
|
|
502
|
+
};
|
|
402
503
|
}
|