@monorise/react 0.0.2-dev.1 → 1.0.1

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.
Files changed (63) hide show
  1. package/actions/app.action.ts +218 -0
  2. package/actions/auth.action.ts +71 -0
  3. package/actions/core.action.ts +878 -0
  4. package/dist/actions/auth.action.d.ts +0 -1
  5. package/dist/actions/auth.action.d.ts.map +1 -1
  6. package/dist/actions/auth.action.js +6 -23
  7. package/dist/actions/auth.action.js.map +1 -1
  8. package/dist/actions/core.action.d.ts +1 -3
  9. package/dist/actions/core.action.d.ts.map +1 -1
  10. package/dist/actions/core.action.js +5 -38
  11. package/dist/actions/core.action.js.map +1 -1
  12. package/dist/index.d.ts +57 -430
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +9 -16
  15. package/dist/index.js.map +1 -1
  16. package/dist/lib/api.d.ts +1 -6
  17. package/dist/lib/api.d.ts.map +1 -1
  18. package/dist/lib/{api.js → api.jsx} +20 -26
  19. package/dist/lib/api.jsx.map +1 -0
  20. package/dist/lib/config.d.ts +5 -0
  21. package/dist/lib/config.d.ts.map +1 -0
  22. package/dist/lib/config.js +24 -0
  23. package/dist/lib/config.js.map +1 -0
  24. package/dist/lib/entity.d.ts +1 -2
  25. package/dist/lib/entity.d.ts.map +1 -1
  26. package/dist/lib/entity.js.map +1 -1
  27. package/dist/services/auth.service.d.ts +0 -1
  28. package/dist/services/auth.service.d.ts.map +1 -1
  29. package/dist/services/auth.service.js +0 -4
  30. package/dist/services/auth.service.js.map +1 -1
  31. package/dist/services/core.service.d.ts +2 -4
  32. package/dist/services/core.service.d.ts.map +1 -1
  33. package/dist/services/core.service.js +10 -9
  34. package/dist/services/core.service.js.map +1 -1
  35. package/dist/store/monorise.store.d.ts +3 -10
  36. package/dist/store/monorise.store.d.ts.map +1 -1
  37. package/dist/store/monorise.store.js +11 -3
  38. package/dist/store/monorise.store.js.map +1 -1
  39. package/index.ts +146 -0
  40. package/lib/api.tsx +132 -0
  41. package/lib/config.ts +36 -0
  42. package/lib/constant.ts +1 -0
  43. package/lib/entity.ts +81 -0
  44. package/lib/utils.ts +12 -0
  45. package/package.json +7 -11
  46. package/services/auth.service.ts +59 -0
  47. package/services/core.service.ts +416 -0
  48. package/services/filestore.service.ts +136 -0
  49. package/store/monorise.store.ts +111 -0
  50. package/tsconfig.json +114 -0
  51. package/types/api.type.ts +57 -0
  52. package/types/axios.type.ts +14 -0
  53. package/types/monorise.type.ts +8 -0
  54. package/dist/actions/config.action.d.ts +0 -10
  55. package/dist/actions/config.action.d.ts.map +0 -1
  56. package/dist/actions/config.action.js +0 -31
  57. package/dist/actions/config.action.js.map +0 -1
  58. package/dist/lib/api.js.map +0 -1
  59. package/dist/tsconfig.tsbuildinfo +0 -1
  60. package/dist/types/mutual.type.d.ts +0 -21
  61. package/dist/types/mutual.type.d.ts.map +0 -1
  62. package/dist/types/mutual.type.js +0 -2
  63. package/dist/types/mutual.type.js.map +0 -1
@@ -0,0 +1,416 @@
1
+ import type {
2
+ CreatedEntity,
3
+ DraftEntity,
4
+ Entity,
5
+ Mutual,
6
+ MutualData,
7
+ } from '@monorise/base';
8
+ import type { AxiosRequestConfig } from 'axios';
9
+ import { getEntityConfig } from '../lib/config';
10
+ import type { AxiosInterceptor } from '../types/api.type';
11
+
12
+ const ENTITY_API_BASE_URL = '/api/core/entity';
13
+ const MUTUAL_API_BASE_URL = '/api/core/mutual';
14
+ const TAG_API_BASE_URL = '/api/core/tag';
15
+
16
+ type ListEntitiesPayload = {
17
+ limit?: number;
18
+ lastKey?: string;
19
+ start?: string;
20
+ end?: string;
21
+ };
22
+
23
+ export type ListEntitiesByTagParams = {
24
+ group?: string;
25
+ query?: string;
26
+ start?: string;
27
+ end?: string;
28
+ limit?: number;
29
+ };
30
+
31
+ type ConfigOptions = {
32
+ entityApiBaseUrl?: string;
33
+ mutualApiBaseUrl?: string;
34
+ tagApiBaseUrl?: string;
35
+ };
36
+
37
+ export type CommonOptions = Partial<AxiosRequestConfig> & {
38
+ customUrl?: string;
39
+ isInterruptive?: boolean;
40
+ feedback?: {
41
+ success?: string;
42
+ failure?: string;
43
+ loading?: string;
44
+ };
45
+ };
46
+
47
+ const initCoreService = (axios: AxiosInterceptor, opts?: ConfigOptions) => {
48
+ let options: ConfigOptions = opts || {};
49
+
50
+ const listEntities = <T extends Entity>(
51
+ entityType: T,
52
+ payload?: ListEntitiesPayload,
53
+ opts: CommonOptions = {},
54
+ ) => {
55
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
56
+ return axios.get<{
57
+ data: CreatedEntity<T>[];
58
+ lastKey?: string;
59
+ totalCount: number;
60
+ }>(opts.customUrl || `${entityApiBaseUrl}/${entityType}`, {
61
+ requestKey: `entity/${entityType}/list`,
62
+ params: payload ?? undefined,
63
+ isInterruptive: opts.isInterruptive,
64
+ feedback: opts.feedback,
65
+ });
66
+ };
67
+
68
+ const searchEntities = <T extends Entity>(
69
+ entityType: T,
70
+ query: string,
71
+ opts: CommonOptions = {},
72
+ ) => {
73
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
74
+ return axios.get<{ data: CreatedEntity<T>[] }>(
75
+ opts.customUrl || `${entityApiBaseUrl}/${entityType}`,
76
+ {
77
+ requestKey: `entity/${entityType}/search`,
78
+ params: { query },
79
+ isInterruptive: opts.isInterruptive,
80
+ feedback: opts.feedback,
81
+ },
82
+ );
83
+ };
84
+
85
+ const listEntitiesByTag = <T extends Entity>(
86
+ entityType: T,
87
+ tagName: string,
88
+ opts: CommonOptions & { params?: ListEntitiesByTagParams } = {},
89
+ ) => {
90
+ const { tagApiBaseUrl = TAG_API_BASE_URL } = options;
91
+ return axios.get<{ entities: CreatedEntity<T>[]; lastKey: string }>(
92
+ opts.customUrl || `${tagApiBaseUrl}/${entityType}/${tagName}`,
93
+ {
94
+ requestKey: `tag/${entityType}/${tagName}/${opts.params?.group || ''}/list`,
95
+ params: opts.params,
96
+ isInterruptive: opts.isInterruptive,
97
+ feedback: opts.feedback,
98
+ },
99
+ );
100
+ };
101
+
102
+ const getEntity = <T extends Entity>(
103
+ entityType: T,
104
+ id: string,
105
+ opts: CommonOptions = {},
106
+ ) => {
107
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
108
+ return axios.get<CreatedEntity<T>>(
109
+ opts.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
110
+ {
111
+ requestKey: `entity/${entityType}/get/${id}`,
112
+ isInterruptive: opts.isInterruptive,
113
+ feedback: {
114
+ loading: `Retrieving ${entityType}`,
115
+ ...(opts.feedback || {}),
116
+ },
117
+ },
118
+ );
119
+ };
120
+
121
+ const createEntity = async <T extends Entity>(
122
+ entityType: T,
123
+ values: DraftEntity<T>,
124
+ opts: CommonOptions = {},
125
+ ) => {
126
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
127
+ const entityConfig = await getEntityConfig();
128
+ return axios.post<CreatedEntity<T>>(
129
+ opts.customUrl || `${entityApiBaseUrl}/${entityType}`,
130
+ values,
131
+ {
132
+ requestKey: `entity/${entityType}/create`,
133
+ isInterruptive: opts.isInterruptive ?? true,
134
+ feedback: {
135
+ loading: `Creating ${entityConfig[entityType].displayName}`,
136
+ success: `${entityConfig[entityType].displayName} created`,
137
+ ...(opts.feedback || {}),
138
+ },
139
+ },
140
+ );
141
+ };
142
+
143
+ const upsertEntity = async <T extends Entity>(
144
+ entityType: T,
145
+ id: string,
146
+ values: DraftEntity<T>,
147
+ opts: CommonOptions = {},
148
+ ) => {
149
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
150
+ const entityConfig = await getEntityConfig();
151
+ return axios.put<CreatedEntity<T>>(
152
+ opts.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
153
+ values,
154
+ {
155
+ requestKey: `entity/${entityType}/upsert/${id}`,
156
+ isInterruptive: opts.isInterruptive ?? true,
157
+ feedback: {
158
+ loading: `Updating ${entityConfig[entityType].displayName}`,
159
+ success: `${entityConfig[entityType].displayName} updated`,
160
+ ...(opts.feedback || {}),
161
+ },
162
+ },
163
+ );
164
+ };
165
+
166
+ const editEntity = async <T extends Entity>(
167
+ entityType: T,
168
+ id: string,
169
+ values: Partial<DraftEntity<T>>,
170
+ opts: CommonOptions = {},
171
+ ) => {
172
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
173
+ const entityConfig = await getEntityConfig();
174
+ return axios.patch<CreatedEntity<T>>(
175
+ opts.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
176
+ values,
177
+ {
178
+ requestKey: `entity/${entityType}/edit/${id}`,
179
+ isInterruptive: opts.isInterruptive ?? true,
180
+ feedback: {
181
+ loading: `Updating ${entityConfig[entityType].displayName}`,
182
+ success: `${entityConfig[entityType].displayName} updated`,
183
+ ...(opts.feedback || {}),
184
+ },
185
+ },
186
+ );
187
+ };
188
+
189
+ const deleteEntity = async <T extends Entity>(
190
+ entityType: T,
191
+ id: string,
192
+ opts: CommonOptions = {},
193
+ ) => {
194
+ const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
195
+ const entityConfig = await getEntityConfig();
196
+ return axios.delete(
197
+ opts.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
198
+ {
199
+ requestKey: `entity/${entityType}/delete/${id}`,
200
+ isInterruptive: opts.isInterruptive ?? true,
201
+ feedback: {
202
+ loading: `Deleting ${entityConfig[entityType].displayName}`,
203
+ success: `${entityConfig[entityType].displayName} deleted`,
204
+ ...(opts.feedback || {}),
205
+ },
206
+ },
207
+ );
208
+ };
209
+
210
+ const listEntitiesByEntity = <B extends Entity, T extends Entity>(
211
+ byEntityType: B,
212
+ entityType: T,
213
+ byEntityId: string,
214
+ opts: CommonOptions = {},
215
+ chainEntityQuery?: string,
216
+ ) => {
217
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
218
+ return axios.get<{ entities: Mutual<B, T>[]; lastKey: string }>(
219
+ opts.customUrl ||
220
+ `${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}${chainEntityQuery ? `?chainEntityQuery=${chainEntityQuery}` : ''}`,
221
+ {
222
+ requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/list${chainEntityQuery ? `?${chainEntityQuery}` : ''}`,
223
+ isInterruptive: opts.isInterruptive,
224
+ feedback: opts.feedback,
225
+ },
226
+ );
227
+ };
228
+
229
+ const getMutual = <B extends Entity, T extends Entity>(
230
+ byEntityType: B,
231
+ entityType: T,
232
+ byEntityId: string,
233
+ entityId: string,
234
+ opts: CommonOptions = {},
235
+ ) => {
236
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
237
+ return axios.get<Mutual<B, T>>(
238
+ opts.customUrl ||
239
+ `${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
240
+ {
241
+ requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/${entityId}/get`,
242
+ isInterruptive: opts.isInterruptive ?? false,
243
+ feedback: opts.feedback,
244
+ },
245
+ );
246
+ };
247
+
248
+ const createMutual = <B extends Entity, T extends Entity>(
249
+ byEntityType: B,
250
+ entityType: T,
251
+ byEntityId: string,
252
+ entityId: string,
253
+ payload: MutualData<B, T> | Record<string, unknown>,
254
+ opts: CommonOptions = {},
255
+ ) => {
256
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
257
+ return axios.post<Mutual<B, T>>(
258
+ opts.customUrl ||
259
+ `${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
260
+ payload,
261
+ {
262
+ requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/create`,
263
+ isInterruptive: opts.isInterruptive ?? true,
264
+ feedback: {
265
+ loading: 'Creating linkage',
266
+ success: 'Linkage created',
267
+ ...(opts.feedback || {}),
268
+ },
269
+ },
270
+ );
271
+ };
272
+
273
+ const editMutual = <B extends Entity, T extends Entity>(
274
+ byEntityType: B,
275
+ entityType: T,
276
+ byEntityId: string,
277
+ entityId: string,
278
+ payload: MutualData<B, T> | Record<string, unknown>,
279
+ opts: CommonOptions = {},
280
+ ) => {
281
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
282
+ return axios.put<Mutual<B, T>>(
283
+ opts.customUrl ||
284
+ `${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
285
+ payload,
286
+ {
287
+ requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/${entityId}/update`,
288
+ isInterruptive: opts.isInterruptive ?? true,
289
+ feedback: {
290
+ loading: 'Updating linkage',
291
+ success: 'Linkage updated',
292
+ ...(opts.feedback || {}),
293
+ },
294
+ },
295
+ );
296
+ };
297
+
298
+ const deleteMutual = <B extends Entity, T extends Entity>(
299
+ byEntityType: B,
300
+ entityType: T,
301
+ byEntityId: string,
302
+ entityId: string,
303
+ opts: CommonOptions = {},
304
+ ) => {
305
+ const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
306
+ return axios.delete(
307
+ opts.customUrl ||
308
+ `${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
309
+ {
310
+ requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/${entityId}/delete`,
311
+ isInterruptive: opts.isInterruptive ?? true,
312
+ feedback: {
313
+ loading: 'Removing linkage',
314
+ success: 'Linkage removed',
315
+ ...(opts.feedback || {}),
316
+ },
317
+ },
318
+ );
319
+ };
320
+
321
+ const makeEntityService = <T extends Entity>(entityType: T) => ({
322
+ listEntities: (payload: ListEntitiesPayload, opts: CommonOptions = {}) =>
323
+ listEntities(entityType, payload, opts),
324
+ searchEntities: (query: string, opts: CommonOptions = {}) =>
325
+ searchEntities(entityType, query, opts),
326
+ listEntitiesByTag: (tagName: string, opts: CommonOptions = {}) =>
327
+ listEntitiesByTag(entityType, tagName, opts),
328
+ getEntity: (id: string, opts: CommonOptions = {}) =>
329
+ getEntity(entityType, id, opts),
330
+ createEntity: (values: DraftEntity<T>, opts: CommonOptions = {}) =>
331
+ createEntity(entityType, values, opts),
332
+ upsertEntity: (
333
+ id: string,
334
+ values: DraftEntity<T>,
335
+ opts: CommonOptions = {},
336
+ ) => upsertEntity(entityType, id, values, opts),
337
+ editEntity: (
338
+ id: string,
339
+ values: Partial<DraftEntity<T>>,
340
+ opts: CommonOptions = {},
341
+ ) => editEntity(entityType, id, values, opts),
342
+ updateEntity: (
343
+ id: string,
344
+ values: DraftEntity<T>,
345
+ opts: CommonOptions = {},
346
+ ) => editEntity(entityType, id, values, opts),
347
+ deleteEntity: (id: string, opts: CommonOptions = {}) =>
348
+ deleteEntity(entityType, id, opts),
349
+ });
350
+
351
+ const makeMutualService = <B extends Entity, T extends Entity>(
352
+ byEntityType: B,
353
+ entityType: T,
354
+ ) => ({
355
+ listEntitiesByEntity: (
356
+ byEntityId: string,
357
+ opts: CommonOptions = {},
358
+ chainEntityQuery?: string,
359
+ ) =>
360
+ listEntitiesByEntity(
361
+ byEntityType,
362
+ entityType,
363
+ byEntityId,
364
+ opts,
365
+ chainEntityQuery,
366
+ ),
367
+ getMutual: (
368
+ byEntityId: string,
369
+ entityId: string,
370
+ opts: CommonOptions = {},
371
+ ) => getMutual(byEntityType, entityType, byEntityId, entityId, opts),
372
+ createMutual: (
373
+ byEntityId: string,
374
+ entityId: string,
375
+ payload: MutualData<B, T> | Record<string, unknown>,
376
+ opts = {},
377
+ ) =>
378
+ createMutual(
379
+ byEntityType,
380
+ entityType,
381
+ byEntityId,
382
+ entityId,
383
+ payload,
384
+ opts,
385
+ ),
386
+ editMutual: (
387
+ byEntityId: string,
388
+ entityId: string,
389
+ payload: MutualData<B, T> | Record<string, unknown>,
390
+ opts = {},
391
+ ) =>
392
+ editMutual(byEntityType, entityType, byEntityId, entityId, payload, opts),
393
+ deleteMutual: (
394
+ byEntityId: string,
395
+ entityId: string,
396
+ opts: CommonOptions = {},
397
+ ) => deleteMutual(byEntityType, entityType, byEntityId, entityId, opts),
398
+ });
399
+
400
+ const setOptions = (opts: ConfigOptions) => {
401
+ options = {
402
+ ...options,
403
+ ...opts,
404
+ };
405
+ };
406
+
407
+ return {
408
+ makeEntityService,
409
+ makeMutualService,
410
+ setOptions,
411
+ };
412
+ };
413
+
414
+ export default initCoreService;
415
+
416
+ export type CoreService = ReturnType<typeof initCoreService>;
@@ -0,0 +1,136 @@
1
+ import type { AxiosInterceptor } from '../types/api.type';
2
+
3
+ type UploadFileProps = {
4
+ file: File;
5
+ scope: string;
6
+ directory: string;
7
+ name: string;
8
+ };
9
+
10
+ type Options = {
11
+ apiBaseUrl?: string;
12
+ };
13
+
14
+ const initFilestoreService = (axios: AxiosInterceptor) => {
15
+ let options: Options = {};
16
+ const { apiBaseUrl = '/api/file' } = options;
17
+
18
+ const uploadFile = async ({
19
+ file,
20
+ scope,
21
+ directory,
22
+ name,
23
+ }: UploadFileProps) => {
24
+ const { data } = await axios.get(`${apiBaseUrl}/upload`, {
25
+ requestKey: `filestore/presign/file/${name}`,
26
+ isInterruptive: true,
27
+ feedback: {
28
+ loading: 'Preparing upload',
29
+ },
30
+ params: {
31
+ scope,
32
+ directory,
33
+ filename: name,
34
+ },
35
+ });
36
+
37
+ return axios.put(data.url, file, {
38
+ requestKey: `filestore/upload/${name}`,
39
+ isInterruptive: true,
40
+ feedback: {
41
+ loading: 'Uploading file',
42
+ },
43
+ headers: {
44
+ 'Content-Type': file.type,
45
+ },
46
+ });
47
+ };
48
+
49
+ const deleteFiles = async (keys: string[]) => {
50
+ const { data } = await axios.post(
51
+ '/file/delete',
52
+ {
53
+ keys,
54
+ },
55
+ {
56
+ requestKey: 'filestore/delete',
57
+ },
58
+ );
59
+
60
+ return data;
61
+ };
62
+
63
+ const uploadVideo = async ({
64
+ file,
65
+ scope,
66
+ directory,
67
+ name,
68
+ }: UploadFileProps) => {
69
+ const { data: presign } = await axios.get(`${apiBaseUrl}/upload`, {
70
+ requestKey: `filestore/presign/video/${name}`,
71
+ isInterruptive: true,
72
+ feedback: {
73
+ loading: 'Preparing upload',
74
+ },
75
+ params: {
76
+ scope,
77
+ directory,
78
+ filename: name,
79
+ },
80
+ });
81
+
82
+ await axios.put(presign.url, file, {
83
+ requestKey: `filestore/upload/video/${name}`,
84
+ isInterruptive: true,
85
+ feedback: {
86
+ loading: 'Uploading video',
87
+ },
88
+ headers: {
89
+ 'Content-Type': file.type,
90
+ },
91
+ });
92
+
93
+ const { data } = await axios.post(
94
+ `${apiBaseUrl}/video/convert`,
95
+ {
96
+ scope,
97
+ directory,
98
+ filename: name,
99
+ },
100
+ {
101
+ requestKey: 'filestore/convert/video',
102
+ isInterruptive: true,
103
+ feedback: {
104
+ loading: 'Triggering video conversion',
105
+ },
106
+ },
107
+ );
108
+
109
+ return data;
110
+ };
111
+
112
+ const getJobStatus = async (id: string) => {
113
+ const { data } = await axios.get(`${apiBaseUrl}/job/${id}`, {
114
+ requestKey: `filestore/get-job/${id}`,
115
+ });
116
+
117
+ return data;
118
+ };
119
+
120
+ const setOptions = (opts: Options) => {
121
+ options = {
122
+ ...options,
123
+ ...opts,
124
+ };
125
+ };
126
+
127
+ return {
128
+ uploadFile,
129
+ deleteFiles,
130
+ uploadVideo,
131
+ getJobStatus,
132
+ setOptions,
133
+ };
134
+ };
135
+
136
+ export default initFilestoreService;
@@ -0,0 +1,111 @@
1
+ import type { CreatedEntity, Entity, Mutual } from '@monorise/base';
2
+ import { enableMapSet } from 'immer';
3
+ import type React from 'react';
4
+ import { create } from 'zustand';
5
+ import { devtools } from 'zustand/middleware';
6
+ import { getEntityConfig } from '../lib/config';
7
+ import type { ApplicationRequestError, ModalProps } from '../types/api.type';
8
+ import type { CommonStore } from '../types/monorise.type';
9
+
10
+ enableMapSet();
11
+
12
+ const configureDevtools = (storeName: string) => ({
13
+ name: `${storeName} Store`,
14
+ anonymousActionType: `${storeName.toLowerCase()}/action`,
15
+ enabled: !['prod', 'dev'].includes(process.env.NEXT_PUBLIC_ENVIRONMENT ?? ''),
16
+ serialize: {
17
+ options: {
18
+ map: true,
19
+ },
20
+ },
21
+ });
22
+
23
+ type Options = {
24
+ modals?: Record<string, React.ComponentType<unknown>>;
25
+ };
26
+
27
+ const initMonoriseStore = async () => {
28
+ let options: Options = {};
29
+ const { modals = {} } = options;
30
+
31
+ const entityMaps = (Object.keys(await getEntityConfig()) as string[]).reduce(
32
+ (acc, entity) => {
33
+ acc[entity as any] = {
34
+ dataMap: new Map<string, CreatedEntity<Entity>>(),
35
+ isFirstFetched: false,
36
+ lastKey: '',
37
+ } as CommonStore<CreatedEntity<Entity>>;
38
+ return acc;
39
+ },
40
+ {} as Record<Entity, CommonStore<CreatedEntity<Entity>>>,
41
+ );
42
+
43
+ type AppModalProps = ModalProps<typeof modals>;
44
+
45
+ const monoriseStore = create(
46
+ devtools(
47
+ (): {
48
+ app: {
49
+ ongoingRequests: Map<string, Promise<unknown>>;
50
+ loadStack: Map<string, number>;
51
+ intLoadStack: Map<string, number>;
52
+ loadingMessage: string;
53
+ errorStack: Map<string, ApplicationRequestError>;
54
+ modal: {
55
+ name: keyof AppModalProps | null;
56
+ context?: AppModalProps[keyof AppModalProps];
57
+ };
58
+ };
59
+ entity: Record<Entity, CommonStore<CreatedEntity<Entity>>>;
60
+ mutual: Record<string, CommonStore<Mutual>>;
61
+ tag: Record<string, CommonStore<CreatedEntity<Entity>>>;
62
+ auth: {
63
+ isUnauthorized: boolean;
64
+ profile: {
65
+ displayName: string;
66
+ email: string;
67
+ accountId: string;
68
+ };
69
+ };
70
+ } => ({
71
+ app: {
72
+ ongoingRequests: new Map(),
73
+ loadStack: new Map(),
74
+ intLoadStack: new Map(),
75
+ loadingMessage: '',
76
+ errorStack: new Map(),
77
+ modal: {
78
+ name: null,
79
+ },
80
+ },
81
+ entity: entityMaps,
82
+ mutual: {},
83
+ tag: {},
84
+ auth: {
85
+ isUnauthorized: false,
86
+ profile: {
87
+ displayName: '',
88
+ email: '',
89
+ accountId: '',
90
+ },
91
+ },
92
+ }),
93
+ configureDevtools('monorise'),
94
+ ),
95
+ );
96
+
97
+ const setOptions = (opts: Options) => {
98
+ options = {
99
+ ...options,
100
+ ...opts,
101
+ };
102
+ };
103
+
104
+ return { setOptions, monoriseStore };
105
+ };
106
+
107
+ export { initMonoriseStore };
108
+
109
+ export type MonoriseStore = Awaited<
110
+ ReturnType<typeof initMonoriseStore>
111
+ >['monoriseStore'];