@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.
@@ -9,21 +9,20 @@ import { createContext, useContext } from 'react';
9
9
  */
10
10
 
11
11
  type AppContextAction = {
12
- __type: (
13
- // App initialization
14
- | 'APP_INIT'
15
- // Auth setup
16
- | 'APP_LOGIN'
17
- | 'APP_GUEST'
18
- | 'APP_EDIT_AUTH'
19
- | 'APP_EDIT_ACCOUNT'
20
- | 'APP_SWITCH_PRIMARY_ORGANIZATION'
21
- // Account theme
22
- | 'APP_THEME'
23
- | 'APP_SYSTEM_LIGHT_MODE'
24
- | 'APP_LIGHT_MODE'
25
- );
26
- data?: any;
12
+ __type: // App initialization
13
+ | 'APP_INIT'
14
+ // Auth setup
15
+ | 'APP_LOGIN'
16
+ | 'APP_GUEST'
17
+ | 'APP_REFRESH_AUTH'
18
+ | 'APP_EDIT_AUTH'
19
+ | 'APP_EDIT_ACCOUNT'
20
+ | 'APP_SWITCH_PRIMARY_ORGANIZATION'
21
+ // Account theme
22
+ | 'APP_THEME'
23
+ | 'APP_SYSTEM_LIGHT_MODE'
24
+ | 'APP_LIGHT_MODE';
25
+ data?: any;
27
26
  };
28
27
 
29
28
  export type AppContextDispatch = (action: AppContextAction) => void;
@@ -35,33 +34,34 @@ export type CurrentAccountObj = ReturnType<typeof useCurrentAccount>;
35
34
  */
36
35
 
37
36
  export type AppAppearanceType = {
38
- realLightMode: LightModeEnum;
39
- lightMode: LightModeEnum;
40
- previewLightMode: LightModeEnum;
41
- previewTheme: string | null;
37
+ realLightMode: LightModeEnum;
38
+ lightMode: LightModeEnum;
39
+ previewLightMode: LightModeEnum;
40
+ previewTheme: Record<string, any> | null;
42
41
  };
43
42
 
44
43
  export type AppContextData = {
45
- // Required for app
46
- init: 0 | 1 | 2; // 0 = not initialized,; 1 = loading; 2 = initialized
47
- authorization: string; // This is token
48
- networkError: boolean;
49
-
50
- // Authentication data
51
- account: AccountData | null;
52
- primaryOrganizationId: string | null;
53
-
54
- // Account settings & state
55
- settings: AccountSettings;
56
- activated: boolean;
57
- hasPassword: boolean;
58
- webVersion: string | null;
59
- alertUpdatesOnMount: boolean;
60
-
61
- // Preview theme overrides active theme
62
- systemLightMode: 'LIGHT' | 'DARK';
63
- previewLightMode: LightModeEnum | null;
64
- previewTheme: string | null;
44
+ // Required for app
45
+ init: 0 | 1 | 2; // 0 = not initialized,; 1 = loading; 2 = initialized
46
+ authorization: string; // This is token
47
+ networkError: boolean;
48
+
49
+ // Authentication data
50
+ account: AccountData | null;
51
+ primaryOrganizationId: string | null;
52
+ cacheAuthorizationEpoch: string | null;
53
+
54
+ // Account settings & state
55
+ settings: AccountSettings;
56
+ activated: boolean;
57
+ hasPassword: boolean;
58
+ webVersion: string | null;
59
+ alertUpdatesOnMount: boolean;
60
+
61
+ // Preview theme overrides active theme
62
+ systemLightMode: 'LIGHT' | 'DARK';
63
+ previewLightMode: LightModeEnum | null;
64
+ previewTheme: Record<string, any> | null;
65
65
  };
66
66
 
67
67
  /**
@@ -69,29 +69,27 @@ export type AppContextData = {
69
69
  */
70
70
 
71
71
  export const DEFAULT_APP_CONTEXT = {
72
- init: 0,
73
- authorization: '',
74
- networkError: false,
75
-
76
- account: null,
77
- primaryOrganizationId: null,
78
-
79
- settings: {
80
- theme: null,
81
- lightMode: 'LIGHT',
82
- timeZone: null,
83
- showSelfAvatar: false,
84
- isBubbleOther: false,
85
- showOtherAvatar: false,
86
- },
87
- activated: false,
88
- hasPassword: false,
89
- webVersion: null,
90
- alertUpdatesOnMount: false,
91
-
92
- systemLightMode: 'LIGHT',
93
- previewLightMode: null,
94
- previewTheme: null,
72
+ init: 0,
73
+ authorization: '',
74
+ networkError: false,
75
+
76
+ account: null,
77
+ primaryOrganizationId: null,
78
+ cacheAuthorizationEpoch: null,
79
+
80
+ settings: {
81
+ theme: null,
82
+ lightMode: 'LIGHT',
83
+ timeZone: null,
84
+ },
85
+ activated: false,
86
+ hasPassword: false,
87
+ webVersion: null,
88
+ alertUpdatesOnMount: false,
89
+
90
+ systemLightMode: 'LIGHT',
91
+ previewLightMode: null,
92
+ previewTheme: null,
95
93
  } as AppContextData;
96
94
 
97
95
  /**
@@ -99,7 +97,7 @@ export const DEFAULT_APP_CONTEXT = {
99
97
  */
100
98
 
101
99
  function ensureLightModeSetting(lightMode?: LightModeEnum): LightModeEnum {
102
- return lightMode && ['LIGHT', 'DARK', 'SYSTEM'].includes(lightMode || '') ? lightMode : 'LIGHT';
100
+ return lightMode && ['LIGHT', 'DARK', 'SYSTEM'].includes(lightMode || '') ? lightMode : 'LIGHT';
103
101
  }
104
102
 
105
103
  /**
@@ -107,24 +105,24 @@ function ensureLightModeSetting(lightMode?: LightModeEnum): LightModeEnum {
107
105
  */
108
106
 
109
107
  export function onAccountLogin(auth: any, dispatchApp: AppContextDispatch, otherCB?: (auth: any) => void) {
110
- if (auth && auth?.token && auth.activated && auth.account) {
111
- console.log('.. 🥝 logged into account ..');
112
-
113
- saveAuthTokenToPlatform(auth.token, () => {
114
- clearAppCache();
115
- dispatchApp({
116
- __type: 'APP_LOGIN',
117
- data: auth,
118
- });
119
-
120
- if (otherCB) {
121
- otherCB(auth);
122
- }
123
- });
124
- } else {
125
- console.log(auth);
126
- console.warn('Developer error in onAccountLogin()');
127
- }
108
+ if (auth && auth?.token && auth.activated && auth.account) {
109
+ console.log('.. 🥝 logged into account ..');
110
+
111
+ saveAuthTokenToPlatform(auth.token, () => {
112
+ clearAppCache();
113
+ dispatchApp({
114
+ __type: 'APP_LOGIN',
115
+ data: auth,
116
+ });
117
+
118
+ if (otherCB) {
119
+ otherCB(auth);
120
+ }
121
+ });
122
+ } else {
123
+ console.log(auth);
124
+ console.warn('Developer error in onAccountLogin()');
125
+ }
128
126
  }
129
127
 
130
128
  /**
@@ -132,40 +130,33 @@ export function onAccountLogin(auth: any, dispatchApp: AppContextDispatch, other
132
130
  */
133
131
 
134
132
  export function onGuestInit(error: any, dispatchApp: AppContextDispatch) {
135
- fetchLightModeSetting(null, (lightMode) => {
136
- dispatchApp({
137
- __type: 'APP_GUEST',
138
- data: {
139
- error,
140
- lightMode,
141
- },
142
- });
143
- });
133
+ fetchLightModeSetting(null, (lightMode) => {
134
+ dispatchApp({
135
+ __type: 'APP_GUEST',
136
+ data: {
137
+ error,
138
+ lightMode,
139
+ },
140
+ });
141
+ });
144
142
  }
145
143
 
146
144
  /**
147
145
  * Get active/current or preview theme
148
146
  */
149
147
 
150
- export function getTheme(
151
- appState: AppContextData,
152
- priority: 'PREVIEW' | 'ACCOUNT' | 'ACTIVE'
153
- ) {
154
- let lightMode = appState.previewLightMode || appState.settings.lightMode;
155
- if (lightMode === 'SYSTEM') {
156
- lightMode = appState.systemLightMode;
157
- }
158
-
159
- const theme = (
160
- (priority === 'PREVIEW' && appState.previewTheme) ||
161
- appState.settings.theme ||
162
- ''
163
- );
164
-
165
- return {
166
- lightMode: lightMode || 'LIGHT',
167
- theme,
168
- };
148
+ export function getTheme(appState: AppContextData, priority: 'PREVIEW' | 'ACCOUNT' | 'ACTIVE') {
149
+ let lightMode = appState.previewLightMode || appState.settings.lightMode;
150
+ if (lightMode === 'SYSTEM') {
151
+ lightMode = appState.systemLightMode;
152
+ }
153
+
154
+ const theme = (priority === 'PREVIEW' && appState.previewTheme) || appState.settings.theme || '';
155
+
156
+ return {
157
+ lightMode: lightMode || 'LIGHT',
158
+ theme,
159
+ };
169
160
  }
170
161
 
171
162
  /**
@@ -173,69 +164,67 @@ export function getTheme(
173
164
  */
174
165
 
175
166
  export function getPreviewAppearance(appState: AppContextData) {
176
- let realLightMode = appState.previewLightMode || appState.settings.lightMode;
177
- if (realLightMode === 'SYSTEM') {
178
- realLightMode = appState.systemLightMode;
179
- }
180
-
181
- return {
182
- realLightMode,
183
- lightMode: appState.settings.lightMode,
184
- previewLightMode: appState.previewLightMode,
185
- previewTheme: appState.previewTheme,
186
- };
167
+ let realLightMode = appState.previewLightMode || appState.settings.lightMode;
168
+ if (realLightMode === 'SYSTEM') {
169
+ realLightMode = appState.systemLightMode;
170
+ }
171
+
172
+ return {
173
+ realLightMode,
174
+ lightMode: appState.settings.lightMode,
175
+ previewLightMode: appState.previewLightMode,
176
+ previewTheme: appState.previewTheme,
177
+ };
187
178
  }
188
179
 
189
180
  /**
190
181
  * Merge sign in mutation auth values to context
191
182
  */
192
183
 
193
- export function mergeAuthToDefaultValues(
194
- auth?: AuthenticationData | null,
195
- authToken?: string | null,
196
- ) {
197
- if (auth?.token && auth?.account?.id) {
198
- let systemLightMode = 'LIGHT';
199
- if (isSystemDarkMode()) {
200
- systemLightMode = 'DARK';
201
- }
202
-
203
- setAuthToken(auth.token);
204
-
205
- const authSettings = {
206
- ...DEFAULT_APP_CONTEXT.settings,
207
- ...auth.account.settings
208
- };
209
-
210
- authSettings.lightMode = ensureLightModeSetting(authSettings.lightMode);
211
-
212
- return {
213
- ...DEFAULT_APP_CONTEXT,
214
- init: 1, // Always 1 because we're always using SSR for authentication
215
- authorization: auth.token,
216
- account: auth.account,
217
- primaryOrganizationId: auth.primaryOrganizationId || null,
218
- settings: authSettings,
219
-
220
- alertUpdatesOnMount: auth.alertUpdatesOnMount,
221
- hasPassword: auth.hasPassword,
222
- webVersion: auth.webVersion,
223
-
224
- // Theme
225
- systemLightMode,
226
- previewLightMode: null,
227
- previewTheme: null,
228
- };
229
- } else if (authToken) {
230
- setAuthToken(authToken);
231
- return {
232
- ...DEFAULT_APP_CONTEXT,
233
- authorization: authToken || ''
234
- };
235
- }
236
-
237
- setAuthToken(null);
238
- return DEFAULT_APP_CONTEXT;
184
+ export function mergeAuthToDefaultValues(auth?: AuthenticationData | null, authToken?: string | null) {
185
+ if (auth?.token && auth?.account?.id) {
186
+ let systemLightMode = 'LIGHT';
187
+ if (isSystemDarkMode()) {
188
+ systemLightMode = 'DARK';
189
+ }
190
+
191
+ setAuthToken(auth.token);
192
+
193
+ const authSettings = {
194
+ ...DEFAULT_APP_CONTEXT.settings,
195
+ ...auth.account.settings,
196
+ };
197
+
198
+ authSettings.lightMode = ensureLightModeSetting(authSettings.lightMode);
199
+
200
+ return {
201
+ ...DEFAULT_APP_CONTEXT,
202
+ init: 1, // Always 1 because we're always using SSR for authentication
203
+ authorization: auth.token,
204
+ account: auth.account,
205
+ primaryOrganizationId: auth.primaryOrganizationId || null,
206
+ cacheAuthorizationEpoch: auth.cacheAuthorizationEpoch || null,
207
+ settings: authSettings,
208
+
209
+ alertUpdatesOnMount: auth.alertUpdatesOnMount,
210
+ hasPassword: auth.hasPassword,
211
+ webVersion: auth.webVersion,
212
+
213
+ // Theme
214
+ systemLightMode,
215
+ previewLightMode: null,
216
+ previewTheme: null,
217
+ };
218
+ } else if (authToken) {
219
+ setAuthToken(authToken);
220
+ return {
221
+ ...DEFAULT_APP_CONTEXT,
222
+ authorization: authToken || '',
223
+ };
224
+ }
225
+
226
+ setAuthToken(null);
227
+ return DEFAULT_APP_CONTEXT;
239
228
  }
240
229
 
241
230
  /**
@@ -243,129 +232,149 @@ export function mergeAuthToDefaultValues(
243
232
  */
244
233
 
245
234
  export const appReducer = (state: AppContextData, action: AppContextAction) => {
246
- const { __type, data } = action;
247
-
248
- switch (__type) {
249
- case 'APP_INIT': {
250
- return {
251
- ...state,
252
- init: state.init === 1 ? 2 : 1,
253
- };
254
- }
255
- case 'APP_LOGIN': {
256
- return mergeAuthToDefaultValues(data);
257
- }
258
- case 'APP_GUEST': {
259
- const networkError = data.error?.errorCode === 'network_error';
260
- if (networkError) {
261
- console.log('.. ❌ network error ..');
262
- } else {
263
- console.log('.. 🥝 guest entered ..');
264
- }
265
-
266
- clearAppCache();
267
- setAuthToken(null);
268
-
269
- return {
270
- ...DEFAULT_APP_CONTEXT,
271
- init: 2,
272
- authorization: '',
273
- networkError,
274
-
275
- // Theme
276
- previewLightMode: null,
277
- previewTheme: null,
278
- };
279
- }
280
- case 'APP_EDIT_AUTH': {
281
- if (data.id === state.account?.id) {
282
- return {
283
- ...state,
284
- hasPassword: typeof data.hasPassword === 'boolean' ? data.hasPassword : state.hasPassword,
285
- account: !data.email ? state.account : {
286
- ...state.account,
287
- email: data.email
288
- },
289
- };
290
- }
291
- return state;
292
- }
293
- case 'APP_EDIT_ACCOUNT': {
294
- if (data.id === state.account?.id) {
295
- return {
296
- ...state,
297
-
298
- // Account
299
- account: {
300
- ...state.account,
301
- ...data,
302
- plus: !data.plus ? state.account?.plus : {
303
- ...state.account?.plus,
304
- ...data.plus,
305
- },
306
- },
307
- settings: !data.settings ? state.settings : {
308
- ...DEFAULT_APP_CONTEXT.settings,
309
- ...data.settings,
310
- }
311
- };
312
- }
313
- return state;
314
- }
315
- case 'APP_SWITCH_PRIMARY_ORGANIZATION': {
316
- return {
317
- ...state,
318
- primaryOrganizationId: data.primaryOrganizationId
319
- };
320
- }
321
- case 'APP_THEME': {
322
- if (!data?.preview) {
323
- // Active theme
324
- return {
325
- ...state,
326
- previewTheme: null,
327
- };
328
- }
329
-
330
- // Is preview theme
331
- return {
332
- ...state,
333
- previewTheme: data.theme,
334
- };
335
- }
336
- case 'APP_LIGHT_MODE': {
337
- if (!data.lightMode && !data.preview) {
338
- console.warn('Developer error in setLightMode(): You cannot set active lightMode to null.');
339
- return state;
340
- } else if (data.preview) {
341
- return {
342
- ...state,
343
- previewLightMode: data.lightMode,
344
- };
345
- }
346
-
347
- return {
348
- ...state,
349
- previewLightMode: null,
350
- };
351
- }
352
- case 'APP_SYSTEM_LIGHT_MODE': {
353
- return {
354
- ...state,
355
- systemLightMode: data.lightMode,
356
- };
357
- }
358
- default:
359
- console.dev('Unknown app reducer action type:', __type);
360
- return state;
361
- }
235
+ const { __type, data } = action;
236
+
237
+ switch (__type) {
238
+ case 'APP_INIT': {
239
+ return {
240
+ ...state,
241
+ init: state.init === 1 ? 2 : 1,
242
+ };
243
+ }
244
+ case 'APP_LOGIN': {
245
+ const nextState = mergeAuthToDefaultValues(data);
246
+ // Inline sign-in keeps the mounted app's completed initialization.
247
+ return { ...nextState, init: state.init === 2 ? 2 : nextState.init };
248
+ }
249
+ case 'APP_REFRESH_AUTH': {
250
+ const authorization = data?.authorization || '';
251
+ if (!authorization) {
252
+ return state;
253
+ }
254
+
255
+ setAuthToken(authorization);
256
+ return {
257
+ ...state,
258
+ authorization,
259
+ };
260
+ }
261
+ case 'APP_GUEST': {
262
+ const networkError = data.error?.errorCode === 'network_error';
263
+ if (networkError) {
264
+ console.log('.. ❌ network error ..');
265
+ } else {
266
+ console.log('.. 🥝 guest entered ..');
267
+ }
268
+
269
+ clearAppCache();
270
+ setAuthToken(null);
271
+
272
+ return {
273
+ ...DEFAULT_APP_CONTEXT,
274
+ init: 2,
275
+ authorization: '',
276
+ networkError,
277
+
278
+ // Theme
279
+ previewLightMode: null,
280
+ previewTheme: null,
281
+ };
282
+ }
283
+ case 'APP_EDIT_AUTH': {
284
+ if (data.id === state.account?.id) {
285
+ return {
286
+ ...state,
287
+ hasPassword: typeof data.hasPassword === 'boolean' ? data.hasPassword : state.hasPassword,
288
+ account: !data.email
289
+ ? state.account
290
+ : {
291
+ ...state.account,
292
+ email: data.email,
293
+ },
294
+ };
295
+ }
296
+ return state;
297
+ }
298
+ case 'APP_EDIT_ACCOUNT': {
299
+ if (data.id === state.account?.id) {
300
+ return {
301
+ ...state,
302
+
303
+ // Account
304
+ account: {
305
+ ...state.account,
306
+ ...data,
307
+ plus: !data.plus
308
+ ? state.account?.plus
309
+ : {
310
+ ...state.account?.plus,
311
+ ...data.plus,
312
+ },
313
+ },
314
+ settings: !data.settings
315
+ ? state.settings
316
+ : {
317
+ ...DEFAULT_APP_CONTEXT.settings,
318
+ ...data.settings,
319
+ },
320
+ };
321
+ }
322
+ return state;
323
+ }
324
+ case 'APP_SWITCH_PRIMARY_ORGANIZATION': {
325
+ return {
326
+ ...state,
327
+ primaryOrganizationId: data.primaryOrganizationId,
328
+ };
329
+ }
330
+ case 'APP_THEME': {
331
+ if (!data?.preview) {
332
+ // Active theme
333
+ return {
334
+ ...state,
335
+ previewTheme: null,
336
+ };
337
+ }
338
+
339
+ // Is preview theme
340
+ return {
341
+ ...state,
342
+ previewTheme: data.theme,
343
+ };
344
+ }
345
+ case 'APP_LIGHT_MODE': {
346
+ if (!data.lightMode && !data.preview) {
347
+ console.warn('Developer error in setLightMode(): You cannot set active lightMode to null.');
348
+ return state;
349
+ } else if (data.preview) {
350
+ return {
351
+ ...state,
352
+ previewLightMode: data.lightMode,
353
+ };
354
+ }
355
+
356
+ return {
357
+ ...state,
358
+ previewLightMode: null,
359
+ };
360
+ }
361
+ case 'APP_SYSTEM_LIGHT_MODE': {
362
+ return {
363
+ ...state,
364
+ systemLightMode: data.lightMode,
365
+ };
366
+ }
367
+ default:
368
+ console.dev('Unknown app reducer action type:', __type);
369
+ return state;
370
+ }
362
371
  };
363
372
 
364
373
  export const AppContext = createContext({
365
- appState: DEFAULT_APP_CONTEXT as AppContextData,
366
- dispatchApp: (() => {
367
- console.warn('AppContext has been re-initialized due to hot refresh.');
368
- }) as AppContextDispatch
374
+ appState: DEFAULT_APP_CONTEXT as AppContextData,
375
+ dispatchApp: (() => {
376
+ console.warn('AppContext has been re-initialized due to hot refresh.');
377
+ }) as AppContextDispatch,
369
378
  });
370
379
 
371
380
  /**
@@ -373,30 +382,34 @@ export const AppContext = createContext({
373
382
  */
374
383
 
375
384
  export function useCurrentAccount() {
376
- const { appState: { activated, hasPassword, webVersion, alertUpdatesOnMount, account, settings, primaryOrganizationId }, dispatchApp } = useContext(AppContext);
377
- const hasName = !!account?.profile?.firstName || !!account?.profile?.lastName;
378
- const { fullName, displayName } = getDisplayName(account?.profile?.firstName, account?.profile?.lastName);
379
-
380
- return {
381
- // Dispatch
382
- dispatchApp,
383
-
384
- // App state
385
- account,
386
- settings,
387
- activated,
388
- hasPassword,
389
- webVersion,
390
- alertUpdatesOnMount,
391
-
392
- // Computed
393
- isLoggedIn: !!account?.id,
394
- hasName,
395
- fullName,
396
- displayName,
397
- labelName: displayName || account?.email?.address || '?',
398
- primaryOrganizationId
399
- };
385
+ const {
386
+ appState: { init, activated, hasPassword, webVersion, alertUpdatesOnMount, account, settings, primaryOrganizationId },
387
+ dispatchApp,
388
+ } = useContext(AppContext);
389
+ const hasName = !!account?.profile?.firstName || !!account?.profile?.lastName;
390
+ const { fullName, displayName } = getDisplayName(account?.profile?.firstName, account?.profile?.lastName);
391
+
392
+ return {
393
+ // Dispatch
394
+ dispatchApp,
395
+
396
+ // App state
397
+ init,
398
+ account,
399
+ settings,
400
+ activated,
401
+ hasPassword,
402
+ webVersion,
403
+ alertUpdatesOnMount,
404
+
405
+ // Computed
406
+ isLoggedIn: !!account?.id,
407
+ hasName,
408
+ fullName,
409
+ displayName,
410
+ labelName: displayName || account?.email?.address || '?',
411
+ primaryOrganizationId,
412
+ };
400
413
  }
401
414
 
402
415
  /**
@@ -404,6 +417,8 @@ export function useCurrentAccount() {
404
417
  */
405
418
 
406
419
  export function useIsLoggedIn() {
407
- const { appState: { account } } = useContext(AppContext);
408
- return !!account?.id;
420
+ const {
421
+ appState: { account },
422
+ } = useContext(AppContext);
423
+ return !!account?.id;
409
424
  }