@monorise/react 1.0.11 → 3.0.0

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 (48) hide show
  1. package/dist/actions/auth.action.d.ts +4 -1
  2. package/dist/actions/auth.action.d.ts.map +1 -1
  3. package/dist/actions/auth.action.js +7 -6
  4. package/dist/actions/auth.action.js.map +1 -1
  5. package/dist/actions/core.action.d.ts +163 -12
  6. package/dist/actions/core.action.d.ts.map +1 -1
  7. package/dist/actions/core.action.js +592 -168
  8. package/dist/actions/core.action.js.map +1 -1
  9. package/dist/index.d.ts +456 -30
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +3 -2
  12. package/dist/index.js.map +1 -1
  13. package/dist/lib/api.d.ts +15 -5
  14. package/dist/lib/api.d.ts.map +1 -1
  15. package/dist/lib/api.js +12 -6
  16. package/dist/lib/api.js.map +1 -1
  17. package/dist/lib/utils.d.ts +8 -0
  18. package/dist/lib/utils.d.ts.map +1 -1
  19. package/dist/lib/utils.js +24 -0
  20. package/dist/lib/utils.js.map +1 -1
  21. package/dist/services/core.service.d.ts +9 -1
  22. package/dist/services/core.service.d.ts.map +1 -1
  23. package/dist/services/core.service.js +32 -20
  24. package/dist/services/core.service.js.map +1 -1
  25. package/dist/services/filestore.service.d.ts +4 -1
  26. package/dist/services/filestore.service.d.ts.map +1 -1
  27. package/dist/services/filestore.service.js +49 -4
  28. package/dist/services/filestore.service.js.map +1 -1
  29. package/dist/store/monorise.store.d.ts +6 -0
  30. package/dist/store/monorise.store.d.ts.map +1 -1
  31. package/dist/store/monorise.store.js +1 -0
  32. package/dist/store/monorise.store.js.map +1 -1
  33. package/dist/tsconfig.tsbuildinfo +1 -0
  34. package/dist/types/api.type.d.ts +2 -0
  35. package/dist/types/api.type.d.ts.map +1 -1
  36. package/dist/types/api.type.js.map +1 -1
  37. package/package.json +20 -10
  38. package/dist/index.esm.js +0 -1601
  39. package/dist/index.jsx +0 -48
  40. package/dist/index.jsx.map +0 -1
  41. package/dist/lib/api.jsx +0 -70
  42. package/dist/lib/api.jsx.map +0 -1
  43. package/dist/lib/config.d.ts +0 -5
  44. package/dist/lib/config.d.ts.map +0 -1
  45. package/dist/lib/config.js +0 -24
  46. package/dist/lib/config.js.map +0 -1
  47. package/index.ts +0 -194
  48. package/tsconfig.json +0 -118
@@ -1,170 +1,390 @@
1
1
  import { produce } from 'immer';
2
2
  import { useEffect, useState } from 'react';
3
3
  import { byEntityId, constructLocal, constructMutual, flipMutual, } from '../lib/entity';
4
- import { convertToMap } from '../lib/utils';
4
+ import { convertToMap, getEntityRequestKey, getMutualRequestKey, getMutualStateKey, getTagRequestKey, getTagStateKey, getUniqueFieldRequestKey, getUniqueFieldStateKey, } from '../lib/utils';
5
5
  // ===== Important tips ======
6
6
  // Should we use store.getState() or store()?
7
7
  // USE store.getState() WHEN using within an action such as getEntity, getProfile
8
8
  // USE store() WHEN using within a react hook so that it gets the benefit of reactivity, such as useProfile, useEntity
9
+ const defaultOnError = (error) => {
10
+ if (!(error instanceof Error) && error.status >= 500) {
11
+ // we only want server error to be captured by error tracking service
12
+ throw error;
13
+ }
14
+ };
9
15
  const initCoreActions = (monoriseStore, appActions, coreService) => {
10
16
  const { makeEntityService, makeMutualService } = coreService;
11
17
  const { checkIsLoading, getError, useLoadStore, useErrorStore } = appActions;
12
18
  const listEntities = async (entityType, params = {}, opts = {}) => {
19
+ var _a, _b;
13
20
  const store = monoriseStore.getState();
14
- const entityState = store.entity[entityType];
21
+ const entityState = (_a = store.entity[entityType]) !== null && _a !== void 0 ? _a : {};
15
22
  const { isFirstFetched } = entityState;
16
23
  const entityService = makeEntityService(entityType);
17
24
  const { skRange } = params;
18
- const requestKey = `entity/${entityType}/list`;
25
+ const requestKey = getEntityRequestKey('list', entityType);
19
26
  const isLoading = checkIsLoading(requestKey);
20
27
  const error = getError(requestKey);
21
- if ((isFirstFetched && !params.skRange) || isLoading || error) {
28
+ const onError = (_b = opts.onError) !== null && _b !== void 0 ? _b : defaultOnError;
29
+ if (error) {
30
+ onError(error);
22
31
  return;
23
32
  }
24
- const { data: result } = await entityService.listEntities(Object.assign(Object.assign({}, ((params === null || params === void 0 ? void 0 : params.all) ? {} : { limit: 20 })), { start: skRange === null || skRange === void 0 ? void 0 : skRange.start, end: skRange === null || skRange === void 0 ? void 0 : skRange.end }), opts);
25
- const newEntityMap = convertToMap(result.data, 'entityId');
26
- const mergedMap = new Map([
27
- ...newEntityMap,
28
- ...store.entity[entityType].dataMap,
29
- ]);
30
- monoriseStore.setState(produce((state) => {
31
- state.entity[entityType] = {
32
- dataMap: params.skRange ? newEntityMap : mergedMap,
33
- isFirstFetched: true,
34
- lastKey: result.lastKey,
35
- };
36
- }), undefined, `mr/entity/list/${entityType}`);
33
+ if (!opts.forceFetch &&
34
+ ((isFirstFetched && !params.skRange) || isLoading)) {
35
+ return;
36
+ }
37
+ try {
38
+ const { data: result } = await entityService.listEntities(Object.assign(Object.assign({}, ((params === null || params === void 0 ? void 0 : params.all) ? {} : { limit: 20 })), { start: skRange === null || skRange === void 0 ? void 0 : skRange.start, end: skRange === null || skRange === void 0 ? void 0 : skRange.end }), opts);
39
+ const newEntityMap = convertToMap(result.data, 'entityId');
40
+ const mergedMap = new Map([
41
+ ...newEntityMap,
42
+ ...store.entity[entityType].dataMap,
43
+ ]);
44
+ monoriseStore.setState(produce((state) => {
45
+ state.entity[entityType] = {
46
+ dataMap: params.skRange ? newEntityMap : mergedMap,
47
+ isFirstFetched: true,
48
+ lastKey: result.lastKey,
49
+ };
50
+ }), undefined, `mr/entity/list/${entityType}`);
51
+ }
52
+ catch (err) {
53
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
54
+ return { error };
55
+ }
37
56
  };
38
57
  const listMoreEntities = async (entityType, opts = {}) => {
58
+ var _a;
59
+ const requestKey = getEntityRequestKey('list', entityType);
39
60
  const store = monoriseStore.getState();
40
61
  const entityState = store.entity[entityType];
41
62
  const { dataMap, lastKey } = entityState;
42
63
  const entityService = makeEntityService(entityType);
64
+ const error = getError(requestKey);
65
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
43
66
  if (!lastKey) {
44
67
  return;
45
68
  }
46
- const { data: result } = await entityService.listEntities({
47
- limit: 20,
48
- lastKey,
49
- }, opts);
50
- monoriseStore.setState(produce((state) => {
51
- state.entity[entityType].isFirstFetched = true;
52
- state.entity[entityType].lastKey = result.lastKey;
53
- for (const i in result.data) {
54
- state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
55
- }
56
- }), undefined, `mr/entity/list-more/${entityType}`);
69
+ if (error) {
70
+ onError(error);
71
+ return;
72
+ }
73
+ try {
74
+ const { data: result } = await entityService.listEntities({
75
+ limit: 20,
76
+ lastKey,
77
+ }, opts);
78
+ monoriseStore.setState(produce((state) => {
79
+ state.entity[entityType].isFirstFetched = true;
80
+ state.entity[entityType].lastKey = result.lastKey;
81
+ for (const i in result.data) {
82
+ state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
83
+ }
84
+ }), undefined, `mr/entity/list-more/${entityType}`);
85
+ }
86
+ catch (err) {
87
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
88
+ return { error };
89
+ }
57
90
  };
58
91
  const searchEntities = async (entityType, query, opts = {}) => {
92
+ var _a;
93
+ const requestKey = getEntityRequestKey('search', entityType);
59
94
  const entityService = makeEntityService(entityType);
60
- const { data: result } = await entityService.searchEntities(query, opts);
61
- monoriseStore.setState(produce((state) => {
62
- state.entity[entityType].searchResults = result.data;
63
- for (const i in result.data) {
64
- state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
65
- }
66
- }));
95
+ const error = getError(requestKey);
96
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
97
+ if (error) {
98
+ onError(error);
99
+ return;
100
+ }
101
+ try {
102
+ const { data: result } = await entityService.searchEntities(query, opts);
103
+ monoriseStore.setState(produce((state) => {
104
+ state.entity[entityType].searchResults = result.data;
105
+ for (const i in result.data) {
106
+ state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
107
+ }
108
+ }));
109
+ }
110
+ catch (err) {
111
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
112
+ return { error };
113
+ }
67
114
  };
68
115
  const listEntitiesByTag = async (entityType, tagName, opts = {}) => {
69
116
  var _a;
70
- const tagKey = `${entityType}/${tagName}/${((_a = opts.params) === null || _a === void 0 ? void 0 : _a.group) || ''}`;
71
- const store = monoriseStore.getState();
72
- const tagState = store.tag[tagKey] || {};
117
+ const tagKey = getTagStateKey(entityType, tagName, opts.params);
118
+ const state = monoriseStore.getState();
119
+ const tagState = state.tag[tagKey] || {};
73
120
  const { isFirstFetched, dataMap } = tagState;
74
121
  const entityService = makeEntityService(entityType);
75
- if (isFirstFetched) {
76
- return;
77
- }
78
- const { data } = await entityService.listEntitiesByTag(tagName, opts);
79
- const { entities, lastKey } = data;
80
- monoriseStore.setState(produce((state) => {
81
- for (const entity of entities) {
82
- state.entity[entityType].dataMap.set(entity.entityId, entity);
122
+ const { forceFetch } = opts;
123
+ const requestKey = getTagRequestKey('list', entityType, tagName, opts.params);
124
+ const isLoading = checkIsLoading(requestKey);
125
+ const error = getError(requestKey);
126
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
127
+ if (!forceFetch) {
128
+ if (error) {
129
+ onError(error);
130
+ return { error };
83
131
  }
84
- }));
85
- monoriseStore.setState(produce((state) => {
86
- state.tag[tagKey] = {
87
- dataMap: convertToMap(entities, 'entityId'),
88
- isFirstFetched: true,
89
- lastKey,
90
- };
91
- }));
132
+ if (isFirstFetched || isLoading) {
133
+ return {
134
+ data: {
135
+ entities: Array.from((dataMap === null || dataMap === void 0 ? void 0 : dataMap.values()) || []),
136
+ lastKey: null,
137
+ },
138
+ };
139
+ }
140
+ }
141
+ try {
142
+ const { data } = await entityService.listEntitiesByTag(tagName, Object.assign(Object.assign({}, opts), { requestKey }));
143
+ const { entities, lastKey } = data;
144
+ monoriseStore.setState(produce((state) => {
145
+ for (const entity of entities) {
146
+ state.entity[entityType].dataMap.set(entity.entityId, entity);
147
+ }
148
+ }));
149
+ monoriseStore.setState(produce((state) => {
150
+ state.tag[tagKey] = {
151
+ dataMap: convertToMap(entities, 'entityId'),
152
+ isFirstFetched: true,
153
+ lastKey,
154
+ };
155
+ }));
156
+ return { data };
157
+ }
158
+ catch (err) {
159
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
160
+ return { error };
161
+ }
92
162
  };
93
163
  const getEntity = async (entityType, id, opts = {}) => {
164
+ var _a;
94
165
  const store = monoriseStore.getState();
95
166
  const entityState = store.entity[entityType];
96
167
  const { dataMap } = entityState;
97
168
  const entityService = makeEntityService(entityType);
98
169
  let entity = dataMap.get(id);
99
- const requestKey = `entity/${entityType}/get/${id}`;
170
+ const requestKey = getEntityRequestKey('get', entityType, id);
100
171
  const isLoading = checkIsLoading(requestKey);
101
172
  const error = getError(requestKey);
102
- if (entity || isLoading || error) {
103
- return;
173
+ const { forceFetch } = opts;
174
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
175
+ if (!forceFetch) {
176
+ if (error) {
177
+ // If there's an existing error, handle it and return the error.
178
+ onError === null || onError === void 0 ? void 0 : onError(error);
179
+ return { error };
180
+ }
181
+ if (entity) {
182
+ // If entity already exists and no force fetch, return it.
183
+ return { data: entity };
184
+ }
185
+ if (isLoading) {
186
+ // If already loading, return undefined data and error to indicate ongoing operation.
187
+ return {};
188
+ }
189
+ }
190
+ try {
191
+ ({ data: entity } = await entityService.getEntity(id, opts));
192
+ monoriseStore.setState(produce((state) => {
193
+ state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
194
+ }), undefined, `mr/entity/get/${entityType}/${id}`);
195
+ return { data: entity };
196
+ }
197
+ catch (err) {
198
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
199
+ return { error };
200
+ }
201
+ };
202
+ const getEntityByUniqueField = async (entityType, fieldName, value, opts = {}) => {
203
+ var _a;
204
+ const store = monoriseStore.getState();
205
+ const entityState = store.entity[entityType];
206
+ const { dataMap } = entityState;
207
+ const entityService = makeEntityService(entityType);
208
+ const stateKey = getUniqueFieldStateKey(fieldName, value);
209
+ let entity = dataMap.get(stateKey);
210
+ const requestKey = getUniqueFieldRequestKey(entityType, fieldName, value);
211
+ const isLoading = checkIsLoading(requestKey);
212
+ const error = getError(requestKey);
213
+ const { forceFetch } = opts;
214
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
215
+ if (!forceFetch) {
216
+ if (error) {
217
+ onError === null || onError === void 0 ? void 0 : onError(error);
218
+ return { error };
219
+ }
220
+ if (entity) {
221
+ return { data: entity };
222
+ }
223
+ if (isLoading) {
224
+ return {};
225
+ }
226
+ }
227
+ try {
228
+ ({ data: entity } = await entityService.getEntityByUniqueField(fieldName, value, opts));
229
+ monoriseStore.setState(produce((state) => {
230
+ state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
231
+ state.entity[entityType].dataMap.set(`${stateKey}`, entity);
232
+ }), undefined, `mr/entity/unique/${entityType}/${stateKey}`);
233
+ return { data: entity };
234
+ }
235
+ catch (err) {
236
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
237
+ return { error };
104
238
  }
105
- ({ data: entity } = await entityService.getEntity(id, opts));
106
- monoriseStore.setState(produce((state) => {
107
- state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
108
- }), undefined, `mr/entity/get/${entityType}/${id}`);
109
239
  };
110
240
  const createEntity = async (entityType, entity, opts = {}) => {
241
+ var _a;
111
242
  const entityService = makeEntityService(entityType);
112
- const { data } = await entityService.createEntity(entity, opts);
113
- monoriseStore.setState(produce((state) => {
114
- state.entity[entityType].dataMap.set(data.entityId, data);
115
- }), undefined, `mr/entity/create/${entityType}`);
116
- return data;
243
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
244
+ try {
245
+ const { data } = await entityService.createEntity(entity, opts);
246
+ monoriseStore.setState(produce((state) => {
247
+ state.entity[entityType].dataMap.set(data.entityId, data);
248
+ }), undefined, `mr/entity/create/${entityType}`);
249
+ return { data };
250
+ }
251
+ catch (err) {
252
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
253
+ onError === null || onError === void 0 ? void 0 : onError(error);
254
+ return { error };
255
+ }
117
256
  };
118
257
  const upsertEntity = async (entityType, id, entity, opts = {}) => {
258
+ var _a;
119
259
  const entityService = makeEntityService(entityType);
120
- const { data } = await entityService.upsertEntity(id, entity, opts);
121
- monoriseStore.setState(produce((state) => {
122
- state.entity[entityType].dataMap.set(data.entityId, data);
123
- }), undefined, `mr/entity/upser/${entityType}/${id}`);
260
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
261
+ try {
262
+ const { data } = await entityService.upsertEntity(id, entity, opts);
263
+ monoriseStore.setState(produce((state) => {
264
+ state.entity[entityType].dataMap.set(data.entityId, data);
265
+ }), undefined, `mr/entity/upsert/${entityType}/${id}`);
266
+ return { data };
267
+ }
268
+ catch (err) {
269
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
270
+ onError(error);
271
+ return { error };
272
+ }
124
273
  };
125
274
  const editEntity = async (entityType, id, entity, opts = {}) => {
275
+ var _a;
126
276
  const entityService = makeEntityService(entityType);
127
- const { data } = await entityService.editEntity(id, entity, opts);
128
- monoriseStore.setState(produce((state) => {
129
- state.entity[entityType].dataMap.set(data.entityId, data);
130
- }), undefined, `mr/entity/edit/${entityType}/${id}`);
277
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
278
+ try {
279
+ const { data } = await entityService.editEntity(id, entity, opts);
280
+ monoriseStore.setState(produce((state) => {
281
+ state.entity[entityType].dataMap.set(data.entityId, data);
282
+ // update mutual's entity data
283
+ for (const key of Object.keys(state.mutual)) {
284
+ const [_byEntity, _byId, _entityType] = key.split('/');
285
+ if (_entityType === entityType) {
286
+ const mutual = state.mutual[key].dataMap.get(id);
287
+ state.mutual[key].dataMap = new Map(state.mutual[key].dataMap).set(id, Object.assign(Object.assign({}, mutual), { data: data.data }));
288
+ }
289
+ }
290
+ }), undefined, `mr/entity/edit/${entityType}/${id}`);
291
+ return { data };
292
+ }
293
+ catch (err) {
294
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
295
+ onError(error);
296
+ return { error };
297
+ }
131
298
  };
132
299
  const deleteEntity = async (entityType, id, opts = {}) => {
300
+ var _a;
133
301
  const entityService = makeEntityService(entityType);
134
- await entityService.deleteEntity(id, opts);
135
- deleteLocalMutualsByEntity(entityType, id);
136
- monoriseStore.setState(produce((state) => {
137
- state.entity[entityType].dataMap.delete(id);
138
- }), undefined, `mr/entity/delete/${entityType}/${id}`);
302
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
303
+ try {
304
+ await entityService.deleteEntity(id, opts);
305
+ deleteLocalMutualsByEntity(entityType, id);
306
+ monoriseStore.setState(produce((state) => {
307
+ state.entity[entityType].dataMap.delete(id);
308
+ // delete mutual's entity data
309
+ for (const key of Object.keys(state.mutual)) {
310
+ const [_byEntity, _byId, _entityType] = key.split('/');
311
+ if (_entityType === entityType) {
312
+ state.mutual[key].dataMap.delete(id);
313
+ }
314
+ }
315
+ }), undefined, `mr/entity/delete/${entityType}/${id}`);
316
+ return { data: { entityId: id } }; // Indicate success with the deleted ID
317
+ }
318
+ catch (err) {
319
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
320
+ onError(error);
321
+ return { error };
322
+ }
139
323
  };
140
324
  const listEntitiesByEntity = async (byEntityType, entityType, id, opts = {}, chainEntityQuery) => {
141
- const selfKey = `${byEntityType}/${id}/${entityType}`;
325
+ var _a, _b, _c;
326
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
327
+ const selfKey = (_b = opts.stateKey) !== null && _b !== void 0 ? _b : getMutualStateKey(byEntityType, id, entityType, undefined, chainEntityQuery);
142
328
  const mutualService = makeMutualService(byEntityType, entityType);
143
329
  const store = monoriseStore.getState();
144
330
  const mutualState = store.mutual[selfKey] || {};
145
331
  const { isFirstFetched } = mutualState;
146
- const requestKey = `mutual/${selfKey}/list`;
332
+ const requestKey = getMutualRequestKey('list', byEntityType, entityType, id, undefined, chainEntityQuery);
147
333
  const isLoading = checkIsLoading(requestKey);
148
334
  const error = getError(requestKey);
149
- if (isFirstFetched || isLoading || error) {
150
- return;
151
- }
152
- const { data } = await mutualService.listEntitiesByEntity(id, opts, chainEntityQuery);
153
- const { entities, lastKey } = data;
154
- const newEntityDataMap = new Map();
155
- for (const i in entities) {
156
- newEntityDataMap.set(entities[i].entityId, entities[i]);
335
+ const { forceFetch } = opts;
336
+ if (!forceFetch) {
337
+ if (error) {
338
+ onError(error);
339
+ return { error };
340
+ }
341
+ if (isFirstFetched || isLoading) {
342
+ return {
343
+ data: {
344
+ entities: Array.from(((_c = mutualState.dataMap) === null || _c === void 0 ? void 0 : _c.values()) || []),
345
+ lastKey: mutualState.lastKey,
346
+ },
347
+ };
348
+ }
157
349
  }
158
- monoriseStore.setState(produce((state) => {
159
- for (const [key, value] of newEntityDataMap) {
160
- state.entity[entityType].dataMap.set(key, value);
350
+ try {
351
+ const { data } = await mutualService.listEntitiesByEntity(id, opts, chainEntityQuery);
352
+ const { entities, lastKey } = data;
353
+ const newEntityDataMap = new Map();
354
+ for (const i in entities) {
355
+ newEntityDataMap.set(entities[i].entityId, entities[i]);
161
356
  }
162
- state.mutual[selfKey] = {
163
- dataMap: convertToMap(entities, 'entityId'),
164
- isFirstFetched: true,
165
- lastKey,
166
- };
167
- }), undefined, `mr/mutual/list/${byEntityType}/${id}/${entityType}`);
357
+ monoriseStore.setState(produce((state) => {
358
+ var _a;
359
+ for (const [key, value] of newEntityDataMap) {
360
+ (_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap.set(key, value);
361
+ }
362
+ // prepare or extend the mutual entry
363
+ const existing = state.mutual[selfKey];
364
+ if (existing) {
365
+ // extend the existing map
366
+ const incoming = convertToMap(entities, 'entityId');
367
+ for (const [k, v] of incoming) {
368
+ existing.dataMap.set(k, v);
369
+ }
370
+ existing.isFirstFetched = true;
371
+ existing.lastKey = lastKey;
372
+ }
373
+ else {
374
+ // first time: create it
375
+ state.mutual[selfKey] = {
376
+ dataMap: convertToMap(entities, 'entityId'),
377
+ isFirstFetched: true,
378
+ lastKey,
379
+ };
380
+ }
381
+ }), undefined, `mr/mutual/list/${selfKey}`);
382
+ return { data };
383
+ }
384
+ catch (err) {
385
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
386
+ return { error };
387
+ }
168
388
  };
169
389
  // todo: list more mutuals by entity
170
390
  // const listMoremutualsByEntity = async (
@@ -173,19 +393,28 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
173
393
  // id: string,
174
394
  // ) =>
175
395
  const getMutual = async (byEntityType, entityType, byEntityId, entityId, opts = {}) => {
176
- var _a, _b;
177
- const selfKey = `${byEntityType}/${byEntityId}/${entityType}`;
396
+ var _a, _b, _c, _d;
397
+ const selfKey = getMutualStateKey(byEntityType, byEntityId, entityType);
178
398
  const mutualService = makeMutualService(byEntityType, entityType);
179
399
  const store = monoriseStore.getState();
180
400
  const mutualState = store.mutual[selfKey] || {};
181
- const requestKey = `mutual/${byEntityType}/${byEntityId}/${entityType}/${entityId}/get`;
401
+ const requestKey = getMutualRequestKey('get', byEntityType, entityType, byEntityId, entityId);
182
402
  const isLoading = checkIsLoading(requestKey);
183
403
  const error = getError(requestKey);
184
- if (!byEntityId || isLoading || error) {
185
- return;
404
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
405
+ if (error) {
406
+ onError === null || onError === void 0 ? void 0 : onError(error);
407
+ return { error };
408
+ }
409
+ if (!byEntityId || isLoading) {
410
+ return {
411
+ data: (_b = mutualState.dataMap) === null || _b === void 0 ? void 0 : _b.get(entityId),
412
+ };
186
413
  }
187
- if ((_a = mutualState.dataMap) === null || _a === void 0 ? void 0 : _a.get(entityId)) {
188
- return mutualState.dataMap.get(entityId);
414
+ if ((_c = mutualState.dataMap) === null || _c === void 0 ? void 0 : _c.get(entityId)) {
415
+ return {
416
+ data: mutualState.dataMap.get(entityId),
417
+ };
189
418
  }
190
419
  let mutual;
191
420
  let hasRequestFailed = false;
@@ -194,7 +423,8 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
194
423
  }
195
424
  catch (err) {
196
425
  if (!opts.defaultMutualData) {
197
- throw err;
426
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
427
+ return { error };
198
428
  }
199
429
  hasRequestFailed = true;
200
430
  mutual = {
@@ -209,7 +439,7 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
209
439
  mutualData: opts.defaultMutualData,
210
440
  };
211
441
  }
212
- const newMutualDataMap = new Map((_b = store.mutual[selfKey]) === null || _b === void 0 ? void 0 : _b.dataMap);
442
+ const newMutualDataMap = new Map((_d = store.mutual[selfKey]) === null || _d === void 0 ? void 0 : _d.dataMap);
213
443
  newMutualDataMap.set(mutual.entityId, mutual);
214
444
  if (!hasRequestFailed) {
215
445
  const entityState = store.entity[entityType];
@@ -219,16 +449,47 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
219
449
  monoriseStore.setState(produce((state) => {
220
450
  state.mutual[selfKey] = Object.assign(Object.assign({}, (state.mutual[selfKey] || {})), { dataMap: newMutualDataMap });
221
451
  state.entity[entityType].dataMap = newEntityDataMap;
222
- }), undefined, `mr/mutual/get/${byEntityType}/${byEntityId}/${entityType}/${entityId}`);
452
+ }), undefined, `mr/mutual/get/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
223
453
  }
454
+ return { data: mutual };
224
455
  };
225
456
  const createMutual = async (byEntityType, entityType, byEntityId, entityId, payload = {}, opts = {}) => {
457
+ var _a;
226
458
  const mutualService = makeMutualService(byEntityType, entityType);
227
- const { data: mutual } = await mutualService.createMutual(byEntityId, entityId, payload, opts);
459
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
460
+ try {
461
+ const { data: mutual } = await mutualService.createMutual(byEntityId, entityId, payload, opts);
462
+ monoriseStore.setState(produce((state) => {
463
+ var _a, _b;
464
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
465
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
466
+ if (!state.mutual[bySide]) {
467
+ state.mutual[bySide] = {
468
+ dataMap: new Map(),
469
+ };
470
+ }
471
+ state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
472
+ if (!state.mutual[side]) {
473
+ state.mutual[side] = {
474
+ dataMap: new Map(),
475
+ };
476
+ }
477
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
478
+ }), undefined, `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
479
+ return { data: mutual };
480
+ }
481
+ catch (err) {
482
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
483
+ onError(error);
484
+ return { error };
485
+ }
486
+ };
487
+ const createLocalMutual = async (byEntityType, entityType, byEntityId, entityId, mutualData, data) => {
488
+ const mutual = constructMutual(byEntityType, byEntityId, entityType, entityId, mutualData, data);
228
489
  monoriseStore.setState(produce((state) => {
229
490
  var _a, _b;
230
- const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
231
- const side = `${entityType}/${entityId}/${byEntityType}`;
491
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
492
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
232
493
  if (!state.mutual[bySide]) {
233
494
  state.mutual[bySide] = {
234
495
  dataMap: new Map(),
@@ -241,7 +502,7 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
241
502
  };
242
503
  }
243
504
  state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
244
- }), undefined, `mr/mutual/create/${byEntityType}/${byEntityId}/${entityType}/${entityId}`);
505
+ }), undefined, `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
245
506
  };
246
507
  const updateLocalEntity = async (entityType, entityId, data = {}) => {
247
508
  const createdEntity = constructLocal(entityType, entityId, data);
@@ -253,8 +514,8 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
253
514
  const mutual = constructMutual(byEntityType, byEntityId, entityType, entityId, mutualData, data);
254
515
  monoriseStore.setState(produce((state) => {
255
516
  var _a, _b;
256
- const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
257
- const side = `${entityType}/${entityId}/${byEntityType}`;
517
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
518
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
258
519
  if (!state.mutual[bySide]) {
259
520
  state.mutual[bySide] = {
260
521
  dataMap: new Map(),
@@ -267,57 +528,81 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
267
528
  };
268
529
  }
269
530
  state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(byEntityId, flipMutual(mutual));
270
- }), undefined, `mr/mutual/local-update/${byEntityType}/${byEntityId}/${entityType}/${entityId}`);
531
+ }), undefined, `mr/mutual/local-update/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
271
532
  };
272
533
  const editMutual = async (byEntityType, entityType, byEntityId, entityId, payload = {}, opts = {}) => {
534
+ var _a;
273
535
  const mutualService = makeMutualService(byEntityType, entityType);
274
- const { data: mutual } = await mutualService.editMutual(byEntityId, entityId, payload, opts);
275
- monoriseStore.setState(produce((state) => {
276
- var _a, _b;
277
- const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
278
- const side = `${entityType}/${entityId}/${byEntityType}`;
279
- if (!state.mutual[bySide]) {
280
- state.mutual[bySide] = {
281
- dataMap: new Map(),
282
- };
283
- }
284
- state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
285
- if (!state.mutual[side]) {
286
- state.mutual[side] = {
287
- dataMap: new Map(),
288
- };
289
- }
290
- state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
291
- }), undefined, `mr/mutual/edit/${byEntityType}/${byEntityId}/${entityType}/${entityId}`);
536
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
537
+ try {
538
+ const { data: mutual } = await mutualService.editMutual(byEntityId, entityId, payload, opts);
539
+ monoriseStore.setState(produce((state) => {
540
+ var _a, _b;
541
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
542
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
543
+ if (!state.mutual[bySide]) {
544
+ state.mutual[bySide] = {
545
+ dataMap: new Map(),
546
+ };
547
+ }
548
+ state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
549
+ if (!state.mutual[side]) {
550
+ state.mutual[side] = {
551
+ dataMap: new Map(),
552
+ };
553
+ }
554
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
555
+ }), undefined, `mr/mutual/edit/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
556
+ return { data: mutual };
557
+ }
558
+ catch (err) {
559
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
560
+ onError(error);
561
+ return { error };
562
+ }
292
563
  };
293
564
  const deleteMutual = async (byEntityType, entityType, byEntityId, entityId = '', opts = {}) => {
565
+ var _a;
294
566
  const mutualService = makeMutualService(byEntityType, entityType);
295
- const { data } = await mutualService.deleteMutual(byEntityId, entityId, opts);
296
- monoriseStore.setState(produce((state) => {
297
- const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
298
- const side = `${entityType}/${entityId}/${byEntityType}`;
299
- state.mutual[bySide].dataMap.delete(data.entityId);
300
- if (state.mutual[side]) {
301
- state.mutual[side].dataMap.delete(data.byEntityId);
302
- }
303
- }), undefined, `mr/mutual/delete/${byEntityType}/${byEntityId}/${entityType}/${entityId}`);
567
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
568
+ try {
569
+ const { data } = await mutualService.deleteMutual(byEntityId, entityId, opts);
570
+ monoriseStore.setState(produce((state) => {
571
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
572
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
573
+ state.mutual[bySide].dataMap.delete(data.entityId);
574
+ if (state.mutual[side]) {
575
+ state.mutual[side].dataMap.delete(data.byEntityId);
576
+ }
577
+ }), undefined, `mr/mutual/delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
578
+ return { data };
579
+ }
580
+ catch (err) {
581
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
582
+ onError(error);
583
+ return { error };
584
+ }
304
585
  };
305
586
  const deleteLocalMutual = (byEntityType, entityType, byEntityId, entityId) => {
306
587
  monoriseStore.setState(produce((state) => {
307
588
  var _a, _b;
308
- const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
309
- const side = `${entityType}/${entityId}/${byEntityType}`;
310
- const bySideDataMap = new Map((_a = state[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap);
311
- const sideDataMap = new Map((_b = state[side]) === null || _b === void 0 ? void 0 : _b.dataMap);
589
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
590
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
591
+ const bySideDataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap);
592
+ const sideDataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap);
312
593
  bySideDataMap.delete(entityId);
313
594
  sideDataMap.delete(byEntityId);
314
- state.mutual[bySide].dataMap.delete(entityId);
315
- state.mutual[side].dataMap.delete(byEntityId);
316
- }), undefined, `mr/mutual/local-delete/${byEntityType}/${byEntityId}/${entityType}/${entityId}`);
595
+ if (state.mutual[bySide]) {
596
+ state.mutual[bySide].dataMap.delete(entityId);
597
+ }
598
+ if (state.mutual[side]) {
599
+ state.mutual[side].dataMap.delete(byEntityId);
600
+ }
601
+ }), undefined, `mr/mutual/local-delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
317
602
  };
318
603
  const deleteLocalMutualsByEntity = (entityType, id) => {
319
604
  const store = monoriseStore.getState();
320
- const entityConfig = store.config;
605
+ const entityConfig = store.config; // Assuming store.config holds mutual related configurations
321
606
  let mutuals = [];
322
607
  for (const i of Object.keys(entityConfig)) {
323
608
  const mutualState = store.mutual[`${entityType}/${id}/${i}`];
@@ -327,19 +612,26 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
327
612
  ];
328
613
  }
329
614
  const updatedState = mutuals.reduce((acc, mutual) => {
330
- const side = `${mutual.entityType}/${mutual.entityId}/${mutual.byEntityType}`;
331
- const dataMap = new Map(store.mutual[side].dataMap);
615
+ var _a;
616
+ const side = getMutualStateKey(mutual.entityType, mutual.entityId, mutual.byEntityType);
617
+ const dataMap = new Map((_a = store.mutual[side]) === null || _a === void 0 ? void 0 : _a.dataMap); // Use optional chaining
332
618
  dataMap.delete(id);
333
619
  acc[side] = Object.assign(Object.assign({}, store.mutual[side]), { dataMap });
334
620
  return acc;
335
621
  }, {});
336
622
  monoriseStore.setState(produce((state) => {
337
- state.mutual = updatedState;
623
+ // This is a full replacement, might not be desired for partial updates.
624
+ // If state.mutual can contain other keys, this might clear them.
625
+ // Consider a more granular update if needed.
626
+ for (const key in updatedState) {
627
+ state.mutual[key] = updatedState[key];
628
+ }
338
629
  }), undefined, `mr/mutual/local-delete-by-entity/${entityType}/${id}`);
339
630
  };
340
631
  const useEntity = (entityType, id, opts = {}) => {
341
632
  const dataMap = monoriseStore((state) => { var _a; return ((_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap) || new Map(); });
342
- const requestKey = `entity/${entityType}/get/${id}`;
633
+ const isFirstFetched = monoriseStore((state) => { var _a; return (_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.isFirstFetched; });
634
+ const requestKey = getEntityRequestKey('get', entityType, id);
343
635
  const isLoading = useLoadStore(requestKey);
344
636
  const error = useErrorStore(requestKey);
345
637
  useEffect(() => {
@@ -352,14 +644,53 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
352
644
  isLoading,
353
645
  error,
354
646
  requestKey,
647
+ isFirstFetched,
648
+ refetch: async () => {
649
+ if (id) {
650
+ const { data, error: fetchError } = await getEntity(entityType, id, Object.assign(Object.assign({}, opts), { forceFetch: true }));
651
+ if (fetchError) {
652
+ // Handle refetch error if necessary, e.g., throw or set local state
653
+ }
654
+ return data;
655
+ }
656
+ },
657
+ };
658
+ };
659
+ const useEntityByUniqueField = (entityType, fieldName, value, opts = {}) => {
660
+ const dataMap = monoriseStore((state) => { var _a; return ((_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap) || new Map(); });
661
+ const isFirstFetched = monoriseStore((state) => { var _a; return (_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.isFirstFetched; });
662
+ const stateKey = getUniqueFieldStateKey(fieldName, value || '');
663
+ const requestKey = getUniqueFieldRequestKey(entityType, fieldName, value || '');
664
+ const isLoading = useLoadStore(requestKey);
665
+ const error = useErrorStore(requestKey);
666
+ useEffect(() => {
667
+ if (value) {
668
+ getEntityByUniqueField(entityType, fieldName, value, opts);
669
+ }
670
+ }, [fieldName, value, entityType, opts]);
671
+ return {
672
+ entity: value ? dataMap.get(`${stateKey}`) : undefined,
673
+ isLoading,
674
+ error,
675
+ requestKey,
676
+ isFirstFetched,
677
+ refetch: async () => {
678
+ if (value) {
679
+ const { data, error: fetchError } = await getEntityByUniqueField(entityType, fieldName, value, Object.assign(Object.assign({}, opts), { forceFetch: true }));
680
+ if (fetchError) {
681
+ // Handle refetch error if necessary
682
+ }
683
+ return data;
684
+ }
685
+ },
355
686
  };
356
687
  };
357
688
  const useEntities = (entityType, params = {}, opts = {}) => {
358
- const requestKey = `entity/${entityType}/list`;
689
+ const requestKey = getEntityRequestKey('list', entityType);
359
690
  const isListing = useLoadStore(requestKey);
360
691
  const error = useErrorStore(requestKey);
361
692
  const state = monoriseStore((state) => state.entity[entityType]);
362
- const { dataMap, searchResults, isFirstFetched } = state !== null && state !== void 0 ? state : {
693
+ const { dataMap, searchResults, isFirstFetched, lastKey } = state !== null && state !== void 0 ? state : {
363
694
  dataMap: new Map(),
364
695
  };
365
696
  const [entities, setEntities] = useState();
@@ -386,13 +717,14 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
386
717
  }
387
718
  }, [all, entityType, skRange, opts, isFirstFetched]);
388
719
  useEffect(() => {
720
+ var _a;
389
721
  let queryTimeout;
390
722
  if (query === null || query === void 0 ? void 0 : query.length) {
391
723
  setIsSearching(true);
392
724
  queryTimeout = setTimeout(async () => {
393
725
  await searchEntities(entityType, query);
394
726
  setIsSearching(false);
395
- }, 700);
727
+ }, (_a = opts.searchInterval) !== null && _a !== void 0 ? _a : 700);
396
728
  }
397
729
  return () => queryTimeout && clearTimeout(queryTimeout);
398
730
  }, [entityType, query]);
@@ -426,11 +758,26 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
426
758
  isLoading,
427
759
  error,
428
760
  requestKey,
761
+ isFirstFetched,
762
+ lastKey,
763
+ refetch: async () => {
764
+ if (entityType && query) {
765
+ await searchEntities(entityType, query);
766
+ return;
767
+ }
768
+ await listEntities(entityType, { skRange, all }, Object.assign(Object.assign({}, opts), { forceFetch: true }));
769
+ },
770
+ listMore: async () => {
771
+ if (!lastKey)
772
+ return;
773
+ await listMoreEntities(entityType, Object.assign(Object.assign({}, opts), { forceFetch: true }));
774
+ },
429
775
  };
430
776
  };
431
777
  const useMutual = (byEntityType, entityType, byId, id, opts = {}) => {
432
- const state = monoriseStore((state) => state.mutual[`${byEntityType}/${byId}/${entityType}`]);
433
- const requestKey = `mutual/${byEntityType}/${byId}/${entityType}/${id}/get`;
778
+ const stateKey = getMutualStateKey(byEntityType, byId, entityType);
779
+ const state = monoriseStore((state) => state.mutual[stateKey]);
780
+ const requestKey = getMutualRequestKey('get', byEntityType, entityType, byId, id);
434
781
  const isLoading = useLoadStore(requestKey);
435
782
  const error = useErrorStore(requestKey);
436
783
  const { dataMap } = state || {
@@ -449,17 +796,18 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
449
796
  };
450
797
  };
451
798
  const useMutuals = (byEntityType, entityType, byId, opts = {}, chainEntityQuery) => {
452
- const state = monoriseStore((state) => state.mutual[`${byEntityType}/${byId}/${entityType}`]);
453
- const { dataMap, isFirstFetched } = state || {
799
+ const stateKey = getMutualStateKey(byEntityType, byId || '', entityType, undefined, chainEntityQuery);
800
+ const state = monoriseStore((state) => state.mutual[stateKey]);
801
+ const { dataMap, isFirstFetched, lastKey } = state || {
454
802
  dataMap: new Map(),
455
803
  };
456
804
  const [mutuals, setMutuals] = useState([]);
457
- const requestKey = `mutual/${byEntityType}/${entityType}/${byId}/list${chainEntityQuery ? `?${chainEntityQuery}` : ''}`;
805
+ const requestKey = getMutualRequestKey('list', byEntityType, entityType, byId || '', undefined, chainEntityQuery);
458
806
  const isLoading = useLoadStore(requestKey);
459
807
  const error = useErrorStore(requestKey);
460
808
  useEffect(() => {
461
809
  if (!isFirstFetched && byEntityType && entityType && byId) {
462
- listEntitiesByEntity(byEntityType, entityType, byId, opts, chainEntityQuery);
810
+ listEntitiesByEntity(byEntityType, entityType, byId, Object.assign(Object.assign({}, opts), { stateKey }), chainEntityQuery);
463
811
  }
464
812
  }, [
465
813
  isFirstFetched,
@@ -468,42 +816,118 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
468
816
  entityType,
469
817
  opts,
470
818
  chainEntityQuery,
819
+ stateKey,
820
+ opts === null || opts === void 0 ? void 0 : opts.forceFetch,
821
+ opts === null || opts === void 0 ? void 0 : opts.noData,
471
822
  ]);
472
823
  useEffect(() => {
473
- if (dataMap.size !== (mutuals === null || mutuals === void 0 ? void 0 : mutuals.length)) {
474
- setMutuals(Array.from(dataMap.values()));
824
+ const dataMapArray = Array.from(dataMap.values());
825
+ if (dataMap.size !== (mutuals === null || mutuals === void 0 ? void 0 : mutuals.length) ||
826
+ dataMapArray.some((item, index) => JSON.stringify(item) !== JSON.stringify(mutuals[index]))) {
827
+ setMutuals(dataMapArray);
475
828
  }
476
829
  }, [dataMap, dataMap.size, mutuals === null || mutuals === void 0 ? void 0 : mutuals.length]);
830
+ return Object.assign({ mutuals, mutualsMap: dataMap, isLoading,
831
+ requestKey,
832
+ error,
833
+ isFirstFetched,
834
+ lastKey }, (lastKey && {
835
+ listMore: () => {
836
+ if (byEntityType && entityType && byId) {
837
+ listEntitiesByEntity(byEntityType, entityType, byId, Object.assign(Object.assign({}, opts), { forceFetch: true, params: Object.assign(Object.assign({}, opts.params), { lastKey }), stateKey }), chainEntityQuery);
838
+ }
839
+ },
840
+ }));
841
+ };
842
+ const useTaggedEntities = (entityType, tagName, opts = {}) => {
843
+ const { params } = opts || {};
844
+ const stateKey = getTagStateKey(entityType, tagName, params);
845
+ const state = monoriseStore((state) => state.tag[stateKey]);
846
+ const { dataMap, isFirstFetched, lastKey } = state || {
847
+ dataMap: new Map(),
848
+ };
849
+ const [entities, setEntities] = useState([]);
850
+ const requestKey = getTagRequestKey('list', entityType, tagName, params);
851
+ const isLoading = useLoadStore(requestKey);
852
+ const error = useErrorStore(requestKey);
853
+ useEffect(() => {
854
+ if (entityType && tagName && Object.keys(params).length > 0) {
855
+ listEntitiesByTag(entityType, tagName, opts);
856
+ }
857
+ }, [entityType, opts, tagName, params, opts === null || opts === void 0 ? void 0 : opts.forceFetch]);
858
+ useEffect(() => {
859
+ const dataMapArray = Array.from(dataMap.values());
860
+ if (dataMap.size !== (entities === null || entities === void 0 ? void 0 : entities.length) ||
861
+ dataMapArray.some((item, index) => JSON.stringify(item) !== JSON.stringify(entities[index]))) {
862
+ setEntities(dataMapArray);
863
+ }
864
+ }, [dataMap, dataMap.size, entities === null || entities === void 0 ? void 0 : entities.length]);
477
865
  return {
478
- mutuals,
479
- mutualsMap: dataMap,
866
+ entities,
867
+ entitiesMap: dataMap,
480
868
  isLoading,
481
869
  requestKey,
482
870
  error,
871
+ isFirstFetched,
872
+ lastKey,
873
+ refetch: async () => {
874
+ if (entityType && tagName && (params === null || params === void 0 ? void 0 : params.group)) {
875
+ return await listEntitiesByTag(entityType, tagName, Object.assign(Object.assign({}, opts), { forceFetch: true }));
876
+ }
877
+ },
878
+ listMore: async () => {
879
+ if (entityType && tagName && (params === null || params === void 0 ? void 0 : params.group)) {
880
+ return await listEntitiesByTag(entityType, tagName, Object.assign(Object.assign({}, opts), { forceFetch: true, params: Object.assign(Object.assign({}, params), { lastKey }) }));
881
+ }
882
+ },
483
883
  };
484
884
  };
485
885
  const useEntityState = (entityType) => {
486
886
  return monoriseStore((state) => state.entity[entityType]);
487
887
  };
888
+ const updateLocalTaggedEntity = (entityType, entityId, tagName, data = {}, params) => {
889
+ const tagKey = getTagStateKey(entityType, tagName, params);
890
+ monoriseStore.setState(produce((state) => {
891
+ var _a, _b;
892
+ const entity = (_b = (_a = state.tag[tagKey]) === null || _a === void 0 ? void 0 : _a.dataMap) === null || _b === void 0 ? void 0 : _b.get(entityId);
893
+ if (entity) {
894
+ state.tag[tagKey].dataMap.set(entityId, Object.assign(Object.assign({}, entity), { data: Object.assign(Object.assign({}, entity.data), data) }));
895
+ }
896
+ }), undefined, `mr/tag/local-update/${entityType}/${entityId}`);
897
+ };
898
+ const deleteLocalTaggedEntity = (entityType, entityId, tagName, params) => {
899
+ const tagKey = getTagStateKey(entityType, tagName, params);
900
+ monoriseStore.setState(produce((state) => {
901
+ var _a, _b;
902
+ if ((_b = (_a = state.tag[tagKey]) === null || _a === void 0 ? void 0 : _a.dataMap) === null || _b === void 0 ? void 0 : _b.has(entityId)) {
903
+ state.tag[tagKey].dataMap.delete(entityId);
904
+ }
905
+ }), undefined, `mr/tag/local-delete/${entityType}/${entityId}`);
906
+ };
488
907
  return {
489
908
  listMoreEntities,
490
909
  createEntity,
491
910
  upsertEntity,
911
+ getEntity,
492
912
  editEntity,
493
913
  deleteEntity,
494
914
  getMutual,
495
915
  updateLocalEntity,
496
916
  createMutual,
917
+ createLocalMutual,
497
918
  upsertLocalMutual,
498
919
  editMutual,
499
920
  deleteMutual,
500
921
  deleteLocalMutual,
501
922
  useEntity,
923
+ useEntityByUniqueField,
502
924
  useEntities,
503
925
  useMutual,
504
926
  useMutuals,
505
- listEntitiesByTag,
927
+ useTaggedEntities,
506
928
  useEntityState,
929
+ updateLocalTaggedEntity,
930
+ deleteLocalTaggedEntity,
507
931
  };
508
932
  };
509
933
  export { initCoreActions };