@crimson-education/sdk 0.3.5 → 0.3.6
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.
|
@@ -14,6 +14,14 @@ export declare const taskKeys: {
|
|
|
14
14
|
};
|
|
15
15
|
export declare function useTasks(missionId: string | null, enabled: boolean): import("@tanstack/react-query").UseQueryResult<Task[], Error>;
|
|
16
16
|
export declare function useTaskCreators(roadmapId: string | null, enabled?: boolean): import("@tanstack/react-query").UseQueryResult<User[], Error>;
|
|
17
|
+
export declare class ActivityLoader {
|
|
18
|
+
private client;
|
|
19
|
+
private queue;
|
|
20
|
+
private timeout;
|
|
21
|
+
constructor(client: any);
|
|
22
|
+
load(id: string, type: string): Promise<ActionItemActivityBrief[]>;
|
|
23
|
+
private flush;
|
|
24
|
+
}
|
|
17
25
|
export declare function useTaskActivities(actionItemIds: string[], type?: string, enabled?: boolean): {
|
|
18
26
|
data: Record<string, ActionItemActivityBrief[]>;
|
|
19
27
|
isLoading: boolean;
|
|
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
});
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.taskKeys = void 0;
|
|
13
|
+
exports.ActivityLoader = exports.taskKeys = void 0;
|
|
14
14
|
exports.useTasks = useTasks;
|
|
15
15
|
exports.useTaskCreators = useTaskCreators;
|
|
16
16
|
exports.useTaskActivities = useTaskActivities;
|
|
@@ -66,72 +66,70 @@ function useTaskCreators(roadmapId, enabled = true) {
|
|
|
66
66
|
enabled: !!roadmapId && enabled,
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const idsToFetch = actionItemIds.filter((id) => {
|
|
89
|
-
const state = queryClient.getQueryState(exports.taskKeys.activity(id, type));
|
|
90
|
-
// If no data, need fetch
|
|
91
|
-
if (!state || !state.data)
|
|
92
|
-
return true;
|
|
93
|
-
// If data exists but is stale, need fetch
|
|
94
|
-
if (state.dataUpdatedAt && (now - state.dataUpdatedAt > STALE_TIME)) {
|
|
95
|
-
return true;
|
|
69
|
+
class ActivityLoader {
|
|
70
|
+
constructor(client) {
|
|
71
|
+
this.queue = new Map();
|
|
72
|
+
this.timeout = new Map();
|
|
73
|
+
this.client = client;
|
|
74
|
+
}
|
|
75
|
+
load(id, type) {
|
|
76
|
+
const key = type;
|
|
77
|
+
if (!this.queue.has(key)) {
|
|
78
|
+
this.queue.set(key, []);
|
|
79
|
+
}
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
this.queue.get(key).push({ id, type, resolve, reject });
|
|
82
|
+
if (!this.timeout.has(key)) {
|
|
83
|
+
const timeout = setTimeout(() => {
|
|
84
|
+
this.timeout.delete(key);
|
|
85
|
+
this.flush(key);
|
|
86
|
+
}, 50);
|
|
87
|
+
this.timeout.set(key, timeout);
|
|
96
88
|
}
|
|
97
|
-
return false;
|
|
98
89
|
});
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
90
|
+
}
|
|
91
|
+
flush(key) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const items = this.queue.get(key);
|
|
94
|
+
this.queue.delete(key);
|
|
95
|
+
if (!items || items.length === 0)
|
|
96
|
+
return;
|
|
97
|
+
const uniqueIds = Array.from(new Set(items.map(i => i.id)));
|
|
98
|
+
const type = items[0].type; // All items in this queue have same type
|
|
105
99
|
try {
|
|
106
|
-
const result = yield client.tasks.getActivities(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// it implies no activities, so we set empty array to prevent infinite refetching.
|
|
110
|
-
uniqueIdsToFetch.forEach(id => {
|
|
111
|
-
const activities = result[id] || [];
|
|
112
|
-
queryClient.setQueryData(exports.taskKeys.activity(id, type), activities);
|
|
100
|
+
const result = yield this.client.tasks.getActivities(uniqueIds, type);
|
|
101
|
+
items.forEach(({ id, resolve }) => {
|
|
102
|
+
resolve(result[id] || []);
|
|
113
103
|
});
|
|
114
104
|
}
|
|
115
105
|
catch (error) {
|
|
116
|
-
|
|
106
|
+
items.forEach(({ reject }) => reject(error));
|
|
117
107
|
}
|
|
118
108
|
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.ActivityLoader = ActivityLoader;
|
|
112
|
+
function useTaskActivities(actionItemIds, type = "duedate", enabled = true) {
|
|
113
|
+
const client = (0, provider_1.useCrimsonClient)();
|
|
114
|
+
const loaderRef = (0, react_1.useMemo)(() => new ActivityLoader(client), [client]);
|
|
115
|
+
return (0, react_query_1.useQueries)({
|
|
116
|
+
queries: actionItemIds.map((id) => ({
|
|
117
|
+
queryKey: exports.taskKeys.activity(id, type),
|
|
118
|
+
queryFn: () => loaderRef.load(id, type),
|
|
119
|
+
enabled: enabled,
|
|
120
|
+
staleTime: Infinity, // Cache forever until invalidated
|
|
121
|
+
})),
|
|
122
|
+
combine: (results) => {
|
|
123
|
+
const data = {};
|
|
124
|
+
const isLoading = results.some((r) => r.isLoading);
|
|
125
|
+
results.forEach((r, index) => {
|
|
126
|
+
if (r.data) {
|
|
127
|
+
data[actionItemIds[index]] = r.data;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return { data, isLoading };
|
|
132
131
|
}
|
|
133
132
|
});
|
|
134
|
-
return { data, isLoading };
|
|
135
133
|
}
|
|
136
134
|
function useAllUserTasks(missionLinkIds, enabled) {
|
|
137
135
|
const client = (0, provider_1.useCrimsonClient)();
|
|
@@ -425,8 +423,12 @@ function useBulkUpdateTasks(options) {
|
|
|
425
423
|
queryClient.setQueryData(JSON.parse(keyStr), old);
|
|
426
424
|
});
|
|
427
425
|
},
|
|
428
|
-
onSettled: () => {
|
|
426
|
+
onSettled: (data, error, variables) => {
|
|
429
427
|
queryClient.invalidateQueries({ queryKey: exports.taskKeys.all });
|
|
428
|
+
// Invalidate activities for all updated tasks
|
|
429
|
+
variables.ids.forEach(id => {
|
|
430
|
+
queryClient.invalidateQueries({ queryKey: exports.taskKeys.activitiesOfTask(id) });
|
|
431
|
+
});
|
|
430
432
|
},
|
|
431
433
|
});
|
|
432
434
|
}
|