@comapeo/core-react 9.0.2 → 10.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 (73) hide show
  1. package/README.md +1 -1
  2. package/dist/commonjs/contexts/ClientApi.d.ts +1 -3
  3. package/dist/commonjs/contexts/ClientApi.js +2 -2
  4. package/dist/commonjs/hooks/client.d.ts +74 -33
  5. package/dist/commonjs/hooks/client.js +40 -11
  6. package/dist/commonjs/hooks/documents.d.ts +42 -429
  7. package/dist/commonjs/hooks/documents.js +100 -51
  8. package/dist/commonjs/hooks/invites.d.ts +153 -55
  9. package/dist/commonjs/hooks/invites.js +69 -20
  10. package/dist/commonjs/hooks/maps.d.ts +96 -225
  11. package/dist/commonjs/hooks/maps.js +104 -37
  12. package/dist/commonjs/hooks/projects.d.ts +666 -223
  13. package/dist/commonjs/hooks/projects.js +264 -131
  14. package/dist/commonjs/index.d.ts +2 -2
  15. package/dist/commonjs/lib/map-shares-stores.d.ts +1 -1
  16. package/dist/commonjs/lib/map-shares-stores.js +2 -2
  17. package/dist/commonjs/lib/presets.d.ts +1 -3
  18. package/dist/commonjs/lib/react-query.d.ts +103 -0
  19. package/dist/commonjs/lib/react-query.js +187 -0
  20. package/dist/commonjs/lib/sync.d.ts +2 -5
  21. package/dist/commonjs/lib/sync.js +0 -1
  22. package/dist/commonjs/lib/types.d.ts +4 -6
  23. package/dist/esm/contexts/ClientApi.d.ts +1 -3
  24. package/dist/esm/contexts/ClientApi.js +1 -1
  25. package/dist/esm/hooks/client.d.ts +74 -33
  26. package/dist/esm/hooks/client.js +40 -11
  27. package/dist/esm/hooks/documents.d.ts +42 -429
  28. package/dist/esm/hooks/documents.js +100 -51
  29. package/dist/esm/hooks/invites.d.ts +153 -55
  30. package/dist/esm/hooks/invites.js +69 -20
  31. package/dist/esm/hooks/maps.d.ts +96 -225
  32. package/dist/esm/hooks/maps.js +105 -38
  33. package/dist/esm/hooks/projects.d.ts +666 -223
  34. package/dist/esm/hooks/projects.js +262 -129
  35. package/dist/esm/index.d.ts +2 -2
  36. package/dist/esm/lib/map-shares-stores.d.ts +1 -1
  37. package/dist/esm/lib/map-shares-stores.js +1 -1
  38. package/dist/esm/lib/presets.d.ts +1 -3
  39. package/dist/esm/lib/react-query.d.ts +103 -0
  40. package/dist/esm/lib/react-query.js +162 -0
  41. package/dist/esm/lib/sync.d.ts +2 -5
  42. package/dist/esm/lib/sync.js +1 -1
  43. package/dist/esm/lib/types.d.ts +4 -6
  44. package/docs/API.md +137 -81
  45. package/package.json +39 -35
  46. package/dist/commonjs/lib/react-query/client.d.ts +0 -65
  47. package/dist/commonjs/lib/react-query/client.js +0 -68
  48. package/dist/commonjs/lib/react-query/documents.d.ts +0 -1484
  49. package/dist/commonjs/lib/react-query/documents.js +0 -149
  50. package/dist/commonjs/lib/react-query/invites.d.ts +0 -88
  51. package/dist/commonjs/lib/react-query/invites.js +0 -95
  52. package/dist/commonjs/lib/react-query/maps.d.ts +0 -104
  53. package/dist/commonjs/lib/react-query/maps.js +0 -129
  54. package/dist/commonjs/lib/react-query/mutation-result.d.ts +0 -8
  55. package/dist/commonjs/lib/react-query/mutation-result.js +0 -22
  56. package/dist/commonjs/lib/react-query/projects.d.ts +0 -316
  57. package/dist/commonjs/lib/react-query/projects.js +0 -359
  58. package/dist/commonjs/lib/react-query/shared.d.ts +0 -9
  59. package/dist/commonjs/lib/react-query/shared.js +0 -23
  60. package/dist/esm/lib/react-query/client.d.ts +0 -65
  61. package/dist/esm/lib/react-query/client.js +0 -59
  62. package/dist/esm/lib/react-query/documents.d.ts +0 -1484
  63. package/dist/esm/lib/react-query/documents.js +0 -137
  64. package/dist/esm/lib/react-query/invites.d.ts +0 -88
  65. package/dist/esm/lib/react-query/invites.js +0 -85
  66. package/dist/esm/lib/react-query/maps.d.ts +0 -104
  67. package/dist/esm/lib/react-query/maps.js +0 -119
  68. package/dist/esm/lib/react-query/mutation-result.d.ts +0 -8
  69. package/dist/esm/lib/react-query/mutation-result.js +0 -19
  70. package/dist/esm/lib/react-query/projects.d.ts +0 -316
  71. package/dist/esm/lib/react-query/projects.js +0 -324
  72. package/dist/esm/lib/react-query/shared.d.ts +0 -9
  73. package/dist/esm/lib/react-query/shared.js +0 -18
@@ -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 { mapImportMutationOptions, mapInfoQueryOptions, mapRemoveMutationOptions, mapSharesMutationOptions, mapStyleJsonUrlQueryOptions, } from '../lib/react-query/maps.js';
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
- const { data, error, isRefetching } = useSuspenseQuery(mapStyleJsonUrlQueryOptions({ mapServerApi }));
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
- const options = mapImportMutationOptions({ mapServerApi, queryClient });
56
- const result = useMutation(options);
57
- return filterMutationResult(result);
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 options = mapRemoveMutationOptions({ mapServerApi, queryClient });
63
- const result = useMutation(options);
64
- return filterMutationResult(result);
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
- const { data, error, isRefetching } = useQuery(mapInfoQueryOptions({ mapServerApi, mapId: CUSTOM_MAP_ID }));
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
- const options = mapSharesMutationOptions({ action: download });
140
- const result = useMutation(options);
141
- return filterMutationResult(result);
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
- const options = mapSharesMutationOptions({ action: decline });
168
- const result = useMutation(options);
169
- return filterMutationResult(result);
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
- const options = mapSharesMutationOptions({ action: abort });
189
- const result = useMutation(options);
190
- return filterMutationResult(result);
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
- * const { mutate: send } = useSendMapShare({ projectId }, {
207
- * onSuccess: (mapShare) => {
208
- * console.log('Share sent with id', mapShare.shareId)
209
- * }
210
- * })
211
- *
212
- * return (
213
- * <button onClick={() => send({ receiverDeviceId: deviceId, mapId: 'custom' })}>
214
- * Send Map
215
- * </button>
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({ projectId }) {
281
+ export function useSendMapShare() {
221
282
  const { createAndSend } = useSentMapSharesActions();
222
- const options = mapSharesMutationOptions({ action: createAndSend, projectId });
223
- const result = useMutation(options);
224
- return filterMutationResult(result);
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
- const options = mapSharesMutationOptions({ action: cancel });
247
- const result = useMutation(options);
248
- return filterMutationResult(result);
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