@comapeo/core-react 9.0.2 → 10.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.
- package/README.md +1 -1
- package/dist/commonjs/contexts/ClientApi.d.ts +1 -3
- package/dist/commonjs/contexts/ClientApi.js +2 -2
- package/dist/commonjs/hooks/client.d.ts +74 -33
- package/dist/commonjs/hooks/client.js +40 -11
- package/dist/commonjs/hooks/documents.d.ts +41 -429
- package/dist/commonjs/hooks/documents.js +101 -51
- package/dist/commonjs/hooks/invites.d.ts +153 -55
- package/dist/commonjs/hooks/invites.js +69 -20
- package/dist/commonjs/hooks/maps.d.ts +96 -225
- package/dist/commonjs/hooks/maps.js +104 -37
- package/dist/commonjs/hooks/projects.d.ts +666 -223
- package/dist/commonjs/hooks/projects.js +264 -131
- package/dist/commonjs/index.d.ts +2 -2
- package/dist/commonjs/lib/map-shares-stores.d.ts +1 -1
- package/dist/commonjs/lib/map-shares-stores.js +2 -2
- package/dist/commonjs/lib/presets.d.ts +1 -3
- package/dist/commonjs/lib/react-query.d.ts +103 -0
- package/dist/commonjs/lib/react-query.js +187 -0
- package/dist/commonjs/lib/sync.d.ts +2 -5
- package/dist/commonjs/lib/sync.js +0 -1
- package/dist/commonjs/lib/types.d.ts +4 -6
- package/dist/esm/contexts/ClientApi.d.ts +1 -3
- package/dist/esm/contexts/ClientApi.js +1 -1
- package/dist/esm/hooks/client.d.ts +74 -33
- package/dist/esm/hooks/client.js +40 -11
- package/dist/esm/hooks/documents.d.ts +41 -429
- package/dist/esm/hooks/documents.js +101 -51
- package/dist/esm/hooks/invites.d.ts +153 -55
- package/dist/esm/hooks/invites.js +69 -20
- package/dist/esm/hooks/maps.d.ts +96 -225
- package/dist/esm/hooks/maps.js +105 -38
- package/dist/esm/hooks/projects.d.ts +666 -223
- package/dist/esm/hooks/projects.js +262 -129
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/lib/map-shares-stores.d.ts +1 -1
- package/dist/esm/lib/map-shares-stores.js +1 -1
- package/dist/esm/lib/presets.d.ts +1 -3
- package/dist/esm/lib/react-query.d.ts +103 -0
- package/dist/esm/lib/react-query.js +162 -0
- package/dist/esm/lib/sync.d.ts +2 -5
- package/dist/esm/lib/sync.js +1 -1
- package/dist/esm/lib/types.d.ts +4 -6
- package/docs/API.md +137 -81
- package/package.json +39 -35
- package/dist/commonjs/lib/react-query/client.d.ts +0 -65
- package/dist/commonjs/lib/react-query/client.js +0 -68
- package/dist/commonjs/lib/react-query/documents.d.ts +0 -1484
- package/dist/commonjs/lib/react-query/documents.js +0 -149
- package/dist/commonjs/lib/react-query/invites.d.ts +0 -88
- package/dist/commonjs/lib/react-query/invites.js +0 -95
- package/dist/commonjs/lib/react-query/maps.d.ts +0 -104
- package/dist/commonjs/lib/react-query/maps.js +0 -129
- package/dist/commonjs/lib/react-query/mutation-result.d.ts +0 -8
- package/dist/commonjs/lib/react-query/mutation-result.js +0 -22
- package/dist/commonjs/lib/react-query/projects.d.ts +0 -316
- package/dist/commonjs/lib/react-query/projects.js +0 -359
- package/dist/commonjs/lib/react-query/shared.d.ts +0 -9
- package/dist/commonjs/lib/react-query/shared.js +0 -23
- package/dist/esm/lib/react-query/client.d.ts +0 -65
- package/dist/esm/lib/react-query/client.js +0 -59
- package/dist/esm/lib/react-query/documents.d.ts +0 -1484
- package/dist/esm/lib/react-query/documents.js +0 -137
- package/dist/esm/lib/react-query/invites.d.ts +0 -88
- package/dist/esm/lib/react-query/invites.js +0 -85
- package/dist/esm/lib/react-query/maps.d.ts +0 -104
- package/dist/esm/lib/react-query/maps.js +0 -119
- package/dist/esm/lib/react-query/mutation-result.d.ts +0 -8
- package/dist/esm/lib/react-query/mutation-result.js +0 -19
- package/dist/esm/lib/react-query/projects.d.ts +0 -316
- package/dist/esm/lib/react-query/projects.js +0 -324
- package/dist/esm/lib/react-query/shared.d.ts +0 -9
- package/dist/esm/lib/react-query/shared.js +0 -18
package/dist/esm/hooks/maps.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { CUSTOM_MAP_ID } from '@comapeo/map-server/constants.js';
|
|
1
|
+
import { CUSTOM_MAP_ID, DEFAULT_MAP_ID } from '@comapeo/map-server/constants.js';
|
|
2
2
|
import { errors } from '@comapeo/map-server/errors.js';
|
|
3
3
|
import { useMutation, useQuery, useQueryClient, useSuspenseQuery, } from '@tanstack/react-query';
|
|
4
4
|
import { useCallback } from 'react';
|
|
5
5
|
import { useMapServerApi } from '../contexts/MapServer.js';
|
|
6
6
|
import { useReceivedMapSharesActions, useReceivedMapSharesState, useSentMapSharesActions, useSentMapSharesState, } from '../contexts/MapShares.js';
|
|
7
|
-
import {
|
|
8
|
-
import { filterMutationResult } from '../lib/react-query/mutation-result.js';
|
|
7
|
+
import { baseMutationOptions, baseQueryOptions, filterMutationResult, getMapInfoQueryKey, getStyleJsonUrlQueryKey, invalidateMapQueries, } from '../lib/react-query.js';
|
|
9
8
|
/**
|
|
10
9
|
* Get a URL that points to a StyleJSON resource served by the embedded HTTP server.
|
|
11
10
|
*
|
|
@@ -34,7 +33,22 @@ import { filterMutationResult } from '../lib/react-query/mutation-result.js';
|
|
|
34
33
|
*/
|
|
35
34
|
export function useMapStyleUrl() {
|
|
36
35
|
const mapServerApi = useMapServerApi();
|
|
37
|
-
|
|
36
|
+
// TODO: Support custom maps
|
|
37
|
+
const mapId = DEFAULT_MAP_ID;
|
|
38
|
+
const { data, error, isRefetching } = useSuspenseQuery({
|
|
39
|
+
...baseQueryOptions(),
|
|
40
|
+
queryKey: getStyleJsonUrlQueryKey({ mapId }),
|
|
41
|
+
queryFn: async () => {
|
|
42
|
+
const result = await mapServerApi.getMapStyleJsonUrl(mapId);
|
|
43
|
+
const u = new URL(result);
|
|
44
|
+
// This ensures that every time this query is refetched, it will have a different search param, forcing the map to reload.
|
|
45
|
+
u.searchParams.set('refresh_token', Date.now().toString());
|
|
46
|
+
return u.href;
|
|
47
|
+
},
|
|
48
|
+
// Keep this cached until the cache is manually invalidated by a map upload
|
|
49
|
+
staleTime: Infinity,
|
|
50
|
+
gcTime: Infinity,
|
|
51
|
+
});
|
|
38
52
|
return { data, error, isRefetching };
|
|
39
53
|
}
|
|
40
54
|
/**
|
|
@@ -52,20 +66,54 @@ export function useMapStyleUrl() {
|
|
|
52
66
|
export function useImportCustomMapFile() {
|
|
53
67
|
const mapServerApi = useMapServerApi();
|
|
54
68
|
const queryClient = useQueryClient();
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
return filterMutationResult(
|
|
69
|
+
// TODO: Support importing to other custom map IDs, to support multiple maps.
|
|
70
|
+
const mapId = CUSTOM_MAP_ID;
|
|
71
|
+
return filterMutationResult(useMutation({
|
|
72
|
+
...baseMutationOptions(),
|
|
73
|
+
mutationFn: async ({ file }) => {
|
|
74
|
+
if ('exists' in file && !file.exists) {
|
|
75
|
+
throw new Error('File does not exist or is not accessible');
|
|
76
|
+
}
|
|
77
|
+
return mapServerApi.put(`maps/${mapId}`, {
|
|
78
|
+
body: file,
|
|
79
|
+
headers: {
|
|
80
|
+
'Content-Type': 'application/octet-stream',
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
onSuccess: async () => {
|
|
85
|
+
await invalidateMapQueries(queryClient, { mapId });
|
|
86
|
+
},
|
|
87
|
+
}));
|
|
58
88
|
}
|
|
59
89
|
export function useRemoveCustomMapFile() {
|
|
60
90
|
const mapServerApi = useMapServerApi();
|
|
61
91
|
const queryClient = useQueryClient();
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
92
|
+
const mapId = CUSTOM_MAP_ID;
|
|
93
|
+
return filterMutationResult(useMutation({
|
|
94
|
+
...baseMutationOptions(),
|
|
95
|
+
mutationFn: async () => {
|
|
96
|
+
return mapServerApi.delete(`maps/${mapId}`);
|
|
97
|
+
},
|
|
98
|
+
onSuccess: async () => {
|
|
99
|
+
await invalidateMapQueries(queryClient, { mapId });
|
|
100
|
+
},
|
|
101
|
+
}));
|
|
65
102
|
}
|
|
66
103
|
export function useGetCustomMapInfo() {
|
|
67
104
|
const mapServerApi = useMapServerApi();
|
|
68
|
-
|
|
105
|
+
// TODO: Support custom maps
|
|
106
|
+
const mapId = CUSTOM_MAP_ID;
|
|
107
|
+
const { data, error, isRefetching } = useQuery({
|
|
108
|
+
...baseQueryOptions(),
|
|
109
|
+
queryKey: getMapInfoQueryKey({ mapId }),
|
|
110
|
+
queryFn: async () => {
|
|
111
|
+
return mapServerApi.get(`maps/${mapId}/info`).json();
|
|
112
|
+
},
|
|
113
|
+
// Keep this cached until the cache is manually invalidated by a map upload
|
|
114
|
+
staleTime: Infinity,
|
|
115
|
+
gcTime: Infinity,
|
|
116
|
+
});
|
|
69
117
|
return { data, error, isRefetching };
|
|
70
118
|
}
|
|
71
119
|
// ============================================
|
|
@@ -136,9 +184,12 @@ export function useSingleReceivedMapShare({ shareId }) {
|
|
|
136
184
|
*/
|
|
137
185
|
export function useDownloadReceivedMapShare() {
|
|
138
186
|
const { download } = useReceivedMapSharesActions();
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
187
|
+
return filterMutationResult(useMutation({
|
|
188
|
+
...baseMutationOptions(),
|
|
189
|
+
mutationFn: async (options) => {
|
|
190
|
+
return download(options);
|
|
191
|
+
},
|
|
192
|
+
}));
|
|
142
193
|
}
|
|
143
194
|
/**
|
|
144
195
|
* Decline a map share that has been received. Notifies the sender that the
|
|
@@ -164,9 +215,12 @@ export function useDownloadReceivedMapShare() {
|
|
|
164
215
|
*/
|
|
165
216
|
export function useDeclineReceivedMapShare() {
|
|
166
217
|
const { decline } = useReceivedMapSharesActions();
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
218
|
+
return filterMutationResult(useMutation({
|
|
219
|
+
...baseMutationOptions(),
|
|
220
|
+
mutationFn: async (options) => {
|
|
221
|
+
return decline(options);
|
|
222
|
+
},
|
|
223
|
+
}));
|
|
170
224
|
}
|
|
171
225
|
/**
|
|
172
226
|
* Abort an in-progress map share download.
|
|
@@ -185,9 +239,12 @@ export function useDeclineReceivedMapShare() {
|
|
|
185
239
|
*/
|
|
186
240
|
export function useAbortReceivedMapShareDownload() {
|
|
187
241
|
const { abort } = useReceivedMapSharesActions();
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
242
|
+
return filterMutationResult(useMutation({
|
|
243
|
+
...baseMutationOptions(),
|
|
244
|
+
mutationFn: async (options) => {
|
|
245
|
+
return abort(options);
|
|
246
|
+
},
|
|
247
|
+
}));
|
|
191
248
|
}
|
|
192
249
|
// ============================================
|
|
193
250
|
// SENDER HOOKS
|
|
@@ -203,25 +260,32 @@ export function useAbortReceivedMapShareDownload() {
|
|
|
203
260
|
* @example
|
|
204
261
|
* ```tsx
|
|
205
262
|
* function SendMapButton({ projectId, deviceId }: { projectId: string; deviceId: string }) {
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
263
|
+
* const { mutate: send } = useSendMapShare()
|
|
264
|
+
*
|
|
265
|
+
* return (
|
|
266
|
+
* <button
|
|
267
|
+
* onClick={() =>
|
|
268
|
+
* send({ projectId, receiverDeviceId: deviceId, mapId: 'custom' }, {
|
|
269
|
+
* onSuccess: (mapShare) => {
|
|
270
|
+
* console.log('Share sent with id', mapShare.shareId)
|
|
271
|
+
* }
|
|
272
|
+
* )
|
|
273
|
+
* }
|
|
274
|
+
* >
|
|
275
|
+
* Send Map
|
|
276
|
+
* </button>
|
|
277
|
+
* )
|
|
217
278
|
* }
|
|
218
279
|
* ```
|
|
219
280
|
*/
|
|
220
|
-
export function useSendMapShare(
|
|
281
|
+
export function useSendMapShare() {
|
|
221
282
|
const { createAndSend } = useSentMapSharesActions();
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
283
|
+
return filterMutationResult(useMutation({
|
|
284
|
+
...baseMutationOptions(),
|
|
285
|
+
mutationFn: async (options) => {
|
|
286
|
+
return createAndSend(options);
|
|
287
|
+
},
|
|
288
|
+
}));
|
|
225
289
|
}
|
|
226
290
|
/**
|
|
227
291
|
* Cancel a map share that was previously sent. If the recipient has not yet
|
|
@@ -243,9 +307,12 @@ export function useSendMapShare({ projectId }) {
|
|
|
243
307
|
*/
|
|
244
308
|
export function useCancelSentMapShare() {
|
|
245
309
|
const { cancel } = useSentMapSharesActions();
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
310
|
+
return filterMutationResult(useMutation({
|
|
311
|
+
...baseMutationOptions(),
|
|
312
|
+
mutationFn: async (options) => {
|
|
313
|
+
return cancel(options);
|
|
314
|
+
},
|
|
315
|
+
}));
|
|
249
316
|
}
|
|
250
317
|
/**
|
|
251
318
|
* Track the status and progress of a sent map share. Returns the current state
|