@comapeo/core-react 9.0.1 → 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.
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 +41 -429
  7. package/dist/commonjs/hooks/documents.js +101 -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 +98 -219
  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 +41 -429
  28. package/dist/esm/hooks/documents.js +101 -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 +98 -219
  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 -72
  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 -72
  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
@@ -18,8 +18,7 @@ const react_query_1 = require("@tanstack/react-query");
18
18
  const react_1 = require("react");
19
19
  const MapServer_js_1 = require("../contexts/MapServer.js");
20
20
  const MapShares_js_1 = require("../contexts/MapShares.js");
21
- const maps_js_1 = require("../lib/react-query/maps.js");
22
- const mutation_result_js_1 = require("../lib/react-query/mutation-result.js");
21
+ const react_query_js_1 = require("../lib/react-query.js");
23
22
  /**
24
23
  * Get a URL that points to a StyleJSON resource served by the embedded HTTP server.
25
24
  *
@@ -48,7 +47,22 @@ const mutation_result_js_1 = require("../lib/react-query/mutation-result.js");
48
47
  */
49
48
  function useMapStyleUrl() {
50
49
  const mapServerApi = (0, MapServer_js_1.useMapServerApi)();
51
- const { data, error, isRefetching } = (0, react_query_1.useSuspenseQuery)((0, maps_js_1.mapStyleJsonUrlQueryOptions)({ mapServerApi }));
50
+ // TODO: Support custom maps
51
+ const mapId = constants_js_1.DEFAULT_MAP_ID;
52
+ const { data, error, isRefetching } = (0, react_query_1.useSuspenseQuery)({
53
+ ...(0, react_query_js_1.baseQueryOptions)(),
54
+ queryKey: (0, react_query_js_1.getStyleJsonUrlQueryKey)({ mapId }),
55
+ queryFn: async () => {
56
+ const result = await mapServerApi.getMapStyleJsonUrl(mapId);
57
+ const u = new URL(result);
58
+ // This ensures that every time this query is refetched, it will have a different search param, forcing the map to reload.
59
+ u.searchParams.set('refresh_token', Date.now().toString());
60
+ return u.href;
61
+ },
62
+ // Keep this cached until the cache is manually invalidated by a map upload
63
+ staleTime: Infinity,
64
+ gcTime: Infinity,
65
+ });
52
66
  return { data, error, isRefetching };
53
67
  }
54
68
  /**
@@ -66,20 +80,54 @@ function useMapStyleUrl() {
66
80
  function useImportCustomMapFile() {
67
81
  const mapServerApi = (0, MapServer_js_1.useMapServerApi)();
68
82
  const queryClient = (0, react_query_1.useQueryClient)();
69
- const options = (0, maps_js_1.mapImportMutationOptions)({ mapServerApi, queryClient });
70
- const result = (0, react_query_1.useMutation)(options);
71
- return (0, mutation_result_js_1.filterMutationResult)(result);
83
+ // TODO: Support importing to other custom map IDs, to support multiple maps.
84
+ const mapId = constants_js_1.CUSTOM_MAP_ID;
85
+ return (0, react_query_js_1.filterMutationResult)((0, react_query_1.useMutation)({
86
+ ...(0, react_query_js_1.baseMutationOptions)(),
87
+ mutationFn: async ({ file }) => {
88
+ if ('exists' in file && !file.exists) {
89
+ throw new Error('File does not exist or is not accessible');
90
+ }
91
+ return mapServerApi.put(`maps/${mapId}`, {
92
+ body: file,
93
+ headers: {
94
+ 'Content-Type': 'application/octet-stream',
95
+ },
96
+ });
97
+ },
98
+ onSuccess: async () => {
99
+ await (0, react_query_js_1.invalidateMapQueries)(queryClient, { mapId });
100
+ },
101
+ }));
72
102
  }
73
103
  function useRemoveCustomMapFile() {
74
104
  const mapServerApi = (0, MapServer_js_1.useMapServerApi)();
75
105
  const queryClient = (0, react_query_1.useQueryClient)();
76
- const options = (0, maps_js_1.mapRemoveMutationOptions)({ mapServerApi, queryClient });
77
- const result = (0, react_query_1.useMutation)(options);
78
- return (0, mutation_result_js_1.filterMutationResult)(result);
106
+ const mapId = constants_js_1.CUSTOM_MAP_ID;
107
+ return (0, react_query_js_1.filterMutationResult)((0, react_query_1.useMutation)({
108
+ ...(0, react_query_js_1.baseMutationOptions)(),
109
+ mutationFn: async () => {
110
+ return mapServerApi.delete(`maps/${mapId}`);
111
+ },
112
+ onSuccess: async () => {
113
+ await (0, react_query_js_1.invalidateMapQueries)(queryClient, { mapId });
114
+ },
115
+ }));
79
116
  }
80
117
  function useGetCustomMapInfo() {
81
118
  const mapServerApi = (0, MapServer_js_1.useMapServerApi)();
82
- const { data, error, isRefetching } = (0, react_query_1.useQuery)((0, maps_js_1.mapInfoQueryOptions)({ mapServerApi, mapId: constants_js_1.CUSTOM_MAP_ID }));
119
+ // TODO: Support custom maps
120
+ const mapId = constants_js_1.CUSTOM_MAP_ID;
121
+ const { data, error, isRefetching } = (0, react_query_1.useQuery)({
122
+ ...(0, react_query_js_1.baseQueryOptions)(),
123
+ queryKey: (0, react_query_js_1.getMapInfoQueryKey)({ mapId }),
124
+ queryFn: async () => {
125
+ return mapServerApi.get(`maps/${mapId}/info`).json();
126
+ },
127
+ // Keep this cached until the cache is manually invalidated by a map upload
128
+ staleTime: Infinity,
129
+ gcTime: Infinity,
130
+ });
83
131
  return { data, error, isRefetching };
84
132
  }
85
133
  // ============================================
@@ -150,9 +198,12 @@ function useSingleReceivedMapShare({ shareId }) {
150
198
  */
151
199
  function useDownloadReceivedMapShare() {
152
200
  const { download } = (0, MapShares_js_1.useReceivedMapSharesActions)();
153
- const options = (0, maps_js_1.mapSharesMutationOptions)({ action: download });
154
- const result = (0, react_query_1.useMutation)(options);
155
- return (0, mutation_result_js_1.filterMutationResult)(result);
201
+ return (0, react_query_js_1.filterMutationResult)((0, react_query_1.useMutation)({
202
+ ...(0, react_query_js_1.baseMutationOptions)(),
203
+ mutationFn: async (options) => {
204
+ return download(options);
205
+ },
206
+ }));
156
207
  }
157
208
  /**
158
209
  * Decline a map share that has been received. Notifies the sender that the
@@ -178,9 +229,12 @@ function useDownloadReceivedMapShare() {
178
229
  */
179
230
  function useDeclineReceivedMapShare() {
180
231
  const { decline } = (0, MapShares_js_1.useReceivedMapSharesActions)();
181
- const options = (0, maps_js_1.mapSharesMutationOptions)({ action: decline });
182
- const result = (0, react_query_1.useMutation)(options);
183
- return (0, mutation_result_js_1.filterMutationResult)(result);
232
+ return (0, react_query_js_1.filterMutationResult)((0, react_query_1.useMutation)({
233
+ ...(0, react_query_js_1.baseMutationOptions)(),
234
+ mutationFn: async (options) => {
235
+ return decline(options);
236
+ },
237
+ }));
184
238
  }
185
239
  /**
186
240
  * Abort an in-progress map share download.
@@ -199,9 +253,12 @@ function useDeclineReceivedMapShare() {
199
253
  */
200
254
  function useAbortReceivedMapShareDownload() {
201
255
  const { abort } = (0, MapShares_js_1.useReceivedMapSharesActions)();
202
- const options = (0, maps_js_1.mapSharesMutationOptions)({ action: abort });
203
- const result = (0, react_query_1.useMutation)(options);
204
- return (0, mutation_result_js_1.filterMutationResult)(result);
256
+ return (0, react_query_js_1.filterMutationResult)((0, react_query_1.useMutation)({
257
+ ...(0, react_query_js_1.baseMutationOptions)(),
258
+ mutationFn: async (options) => {
259
+ return abort(options);
260
+ },
261
+ }));
205
262
  }
206
263
  // ============================================
207
264
  // SENDER HOOKS
@@ -217,25 +274,32 @@ function useAbortReceivedMapShareDownload() {
217
274
  * @example
218
275
  * ```tsx
219
276
  * function SendMapButton({ projectId, deviceId }: { projectId: string; deviceId: string }) {
220
- * const { mutate: send } = useSendMapShare({ projectId }, {
221
- * onSuccess: (mapShare) => {
222
- * console.log('Share sent with id', mapShare.shareId)
223
- * }
224
- * })
225
- *
226
- * return (
227
- * <button onClick={() => send({ receiverDeviceId: deviceId, mapId: 'custom' })}>
228
- * Send Map
229
- * </button>
230
- * )
277
+ * const { mutate: send } = useSendMapShare()
278
+ *
279
+ * return (
280
+ * <button
281
+ * onClick={() =>
282
+ * send({ projectId, receiverDeviceId: deviceId, mapId: 'custom' }, {
283
+ * onSuccess: (mapShare) => {
284
+ * console.log('Share sent with id', mapShare.shareId)
285
+ * }
286
+ * )
287
+ * }
288
+ * >
289
+ * Send Map
290
+ * </button>
291
+ * )
231
292
  * }
232
293
  * ```
233
294
  */
234
- function useSendMapShare({ projectId }) {
295
+ function useSendMapShare() {
235
296
  const { createAndSend } = (0, MapShares_js_1.useSentMapSharesActions)();
236
- const options = (0, maps_js_1.mapSharesMutationOptions)({ action: createAndSend, projectId });
237
- const result = (0, react_query_1.useMutation)(options);
238
- return (0, mutation_result_js_1.filterMutationResult)(result);
297
+ return (0, react_query_js_1.filterMutationResult)((0, react_query_1.useMutation)({
298
+ ...(0, react_query_js_1.baseMutationOptions)(),
299
+ mutationFn: async (options) => {
300
+ return createAndSend(options);
301
+ },
302
+ }));
239
303
  }
240
304
  /**
241
305
  * Cancel a map share that was previously sent. If the recipient has not yet
@@ -257,9 +321,12 @@ function useSendMapShare({ projectId }) {
257
321
  */
258
322
  function useCancelSentMapShare() {
259
323
  const { cancel } = (0, MapShares_js_1.useSentMapSharesActions)();
260
- const options = (0, maps_js_1.mapSharesMutationOptions)({ action: cancel });
261
- const result = (0, react_query_1.useMutation)(options);
262
- return (0, mutation_result_js_1.filterMutationResult)(result);
324
+ return (0, react_query_js_1.filterMutationResult)((0, react_query_1.useMutation)({
325
+ ...(0, react_query_js_1.baseMutationOptions)(),
326
+ mutationFn: async (options) => {
327
+ return cancel(options);
328
+ },
329
+ }));
263
330
  }
264
331
  /**
265
332
  * Track the status and progress of a sent map share. Returns the current state