@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/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 -22
- package/states/apiData.ts +18 -14
- package/states/appContext.ts +424 -0
- package/states/connected.ts +135 -11
- package/states/index.ts +5 -0
- package/states/keyDown.ts +167 -0
- package/states/keyboardLayer.ts +57 -0
- package/states/modal.ts +403 -240
- package/states/popOver.ts +381 -233
- package/states/reactiveEvents.ts +99 -0
- package/states/uploads.ts +70 -0
- package/types/PopOver.d.ts +680 -0
- package/utils/hooks.ts +399 -251
- package/utils/lazy.ts +183 -0
- package/icons/Icon.tsx +0 -207
- package/icons/IconSVGs.tsx +0 -1486
- package/types/dom.d.ts +0 -7
- package/utils/dom.ts +0 -303
- package/utils/priorityTimeout.ts +0 -57
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
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
|
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
|
|
44
|
+
export type CloseModalPopUpFn = (modalSessionKey?: string) => void;
|
|
25
45
|
|
|
26
46
|
export interface ModalScreenProps {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
47
|
+
screen: ModalProps | null;
|
|
48
|
+
openModalScreen: OpenModalScreenFn;
|
|
49
|
+
closeModalScreen: () => void;
|
|
30
50
|
}
|
|
31
51
|
|
|
32
52
|
export interface ModalPopUpProps {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
53
|
+
popUp: ModalProps | null;
|
|
54
|
+
openModalPopUp: OpenModalPopUpFn;
|
|
55
|
+
closeModalPopUp: CloseModalPopUpFn;
|
|
36
56
|
}
|
|
37
57
|
|
|
38
58
|
export interface ModalHandlerProps {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
|
|
329
|
+
return () => {
|
|
330
|
+
setScreen(screenClass.close());
|
|
331
|
+
};
|
|
245
332
|
}
|
|
246
333
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
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
|
-
|
|
323
|
-
|
|
485
|
+
const setPopUp = useSetAtom(popUpClass.state);
|
|
486
|
+
const openModalPopUp = useCallback(composeOpenModalPopUpFn(setPopUp), []);
|
|
324
487
|
|
|
325
|
-
|
|
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
|
-
|
|
334
|
-
|
|
496
|
+
const screenProps = useModalScreen();
|
|
497
|
+
const popUpProps = useModalPopUp();
|
|
335
498
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
499
|
+
return {
|
|
500
|
+
...screenProps,
|
|
501
|
+
...popUpProps,
|
|
502
|
+
};
|
|
340
503
|
}
|