@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.
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 +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 +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 +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
@@ -0,0 +1,103 @@
1
+ import type { QueryClient, UseMutationResult } from '@tanstack/react-query';
2
+ import { DistributedPick } from 'type-fest';
3
+ import type { WriteableDocumentType } from './types.js';
4
+ export declare function baseQueryOptions(): {
5
+ networkMode: "always";
6
+ retry: false;
7
+ };
8
+ export declare function baseMutationOptions(): {
9
+ networkMode: "always";
10
+ retry: false;
11
+ };
12
+ declare const PICKED_MUTATION_RESULT_KEYS: readonly ["error", "mutate", "mutateAsync", "reset", "status"];
13
+ export type FilteredMutationResult<TResult extends UseMutationResult<any, any, any, any>> = DistributedPick<TResult, (typeof PICKED_MUTATION_RESULT_KEYS)[number]>;
14
+ /**
15
+ * Filters a `UseMutationResult` to only include a subset of its keys, and uses
16
+ * `DistributedPick` to preserve the discriminated union types of the mutation
17
+ * result based on the `status` property.
18
+ */
19
+ export declare function filterMutationResult<TResult extends UseMutationResult<any, any, any, any>>(mutationResult: TResult): DistributedPick<TResult, "error" | "status" | "mutate" | "reset" | "mutateAsync">;
20
+ export declare function getDeviceInfoQueryKey(): readonly ["@comapeo/core-react", "client", "device_info"];
21
+ export declare function getIsArchiveDeviceQueryKey(): readonly ["@comapeo/core-react", "client", "is_archive_device"];
22
+ export declare function getInvitesQueryKey(): readonly ["@comapeo/core-react", "invites"];
23
+ export declare function getInvitesByIdQueryKey({ inviteId }: {
24
+ inviteId: string;
25
+ }): readonly ["@comapeo/core-react", "invites", {
26
+ readonly inviteId: string;
27
+ }];
28
+ export declare function getMapQueryKey({ mapId }: {
29
+ mapId: string;
30
+ }): readonly ["@comapeo/core-react", "maps", string];
31
+ export declare function getMapInfoQueryKey({ mapId }: {
32
+ mapId: string;
33
+ }): readonly ["@comapeo/core-react", "maps", string, "info"];
34
+ export declare function getStyleJsonUrlQueryKey({ mapId }: {
35
+ mapId: string;
36
+ }): readonly ["@comapeo/core-react", "maps", string, "stylejson_url"];
37
+ /**
38
+ * Invalidate queries for this map and the default map (which internally
39
+ * redirects to custom) so that they will be refetched with the new map data.
40
+ */
41
+ export declare function invalidateMapQueries(queryClient: QueryClient, { mapId }: {
42
+ mapId: string;
43
+ }): Promise<void>;
44
+ export declare function getProjectsQueryKey(): readonly ["@comapeo/core-react", "projects"];
45
+ export declare function getProjectByIdQueryKey({ projectId }: {
46
+ projectId: string;
47
+ }): readonly ["@comapeo/core-react", "projects", string];
48
+ export declare function getProjectSettingsQueryKey({ projectId, }: {
49
+ projectId: string;
50
+ }): readonly ["@comapeo/core-react", "projects", string, "project_settings"];
51
+ export declare function getProjectRoleQueryKey({ projectId }: {
52
+ projectId: string;
53
+ }): readonly ["@comapeo/core-react", "projects", string, "role"];
54
+ export declare function getMembersQueryKey({ projectId, includeLeft, }: {
55
+ projectId: string;
56
+ includeLeft?: boolean;
57
+ }): readonly ["@comapeo/core-react", "projects", string, "members", {
58
+ readonly includeLeft: boolean | undefined;
59
+ }];
60
+ export declare function getMemberByIdQueryKey({ projectId, deviceId, }: {
61
+ projectId: string;
62
+ deviceId: string;
63
+ }): readonly ["@comapeo/core-react", "projects", string, "members", string];
64
+ export declare function getDocumentCreatedByQueryKey({ projectId, originalVersionId, }: {
65
+ projectId: string;
66
+ originalVersionId: string;
67
+ }): readonly ["@comapeo/core-react", "projects", string, "document_created_by", string];
68
+ /**
69
+ * We call this within a project hook, because that's the only place the API is
70
+ * exposed right now, but it is the same for all projects, so no need for
71
+ * scoping the query key to the project
72
+ */
73
+ export declare function getMediaServerOriginQueryKey(): readonly ["@comapeo/core-react", "media_server_origin"];
74
+ export declare function getDocumentsQueryKey<D extends WriteableDocumentType>({ projectId, docType, }: {
75
+ projectId: string;
76
+ docType: D;
77
+ }): readonly ["@comapeo/core-react", "projects", string, D];
78
+ export declare function getManyDocumentsQueryKey<D extends WriteableDocumentType>({ projectId, docType, includeDeleted, lang, }: {
79
+ projectId: string;
80
+ docType: D;
81
+ includeDeleted?: boolean;
82
+ lang?: string;
83
+ }): readonly ["@comapeo/core-react", "projects", string, D, {
84
+ readonly includeDeleted: boolean | undefined;
85
+ readonly lang: string | undefined;
86
+ }];
87
+ export declare function getDocumentByDocIdQueryKey<D extends WriteableDocumentType>({ projectId, docType, docId, lang, }: {
88
+ projectId: string;
89
+ docType: D;
90
+ docId: string;
91
+ lang?: string;
92
+ }): readonly ["@comapeo/core-react", "projects", string, D, string, {
93
+ readonly lang: string | undefined;
94
+ }];
95
+ export declare function getDocumentByVersionIdQueryKey<D extends WriteableDocumentType>({ projectId, docType, versionId, lang, }: {
96
+ projectId: string;
97
+ docType: D;
98
+ versionId: string;
99
+ lang?: string;
100
+ }): readonly ["@comapeo/core-react", "projects", string, D, string, {
101
+ readonly lang: string | undefined;
102
+ }];
103
+ export {};
@@ -0,0 +1,162 @@
1
+ import { DEFAULT_MAP_ID } from '@comapeo/map-server';
2
+ // #region Shared
3
+ const ROOT_QUERY_KEY = '@comapeo/core-react';
4
+ // Since the API is running locally, queries should run regardless of network
5
+ // status, and should not be retried. In React Native the API consumer would
6
+ // have to manually set the network mode, but we still should keep these options
7
+ // to avoid surprises. Not using the queryClient `defaultOptions` because the API
8
+ // consumer might also use the same queryClient for network queries
9
+ export function baseQueryOptions() {
10
+ return {
11
+ networkMode: 'always',
12
+ retry: false,
13
+ };
14
+ }
15
+ export function baseMutationOptions() {
16
+ return {
17
+ networkMode: 'always',
18
+ retry: false,
19
+ };
20
+ }
21
+ const PICKED_MUTATION_RESULT_KEYS = [
22
+ 'error',
23
+ 'mutate',
24
+ 'mutateAsync',
25
+ 'reset',
26
+ 'status',
27
+ ];
28
+ /**
29
+ * Filters a `UseMutationResult` to only include a subset of its keys, and uses
30
+ * `DistributedPick` to preserve the discriminated union types of the mutation
31
+ * result based on the `status` property.
32
+ */
33
+ export function filterMutationResult(mutationResult) {
34
+ const filteredResult = {};
35
+ for (const key of PICKED_MUTATION_RESULT_KEYS) {
36
+ filteredResult[key] = mutationResult[key];
37
+ }
38
+ return filteredResult;
39
+ }
40
+ // #endregion
41
+ // #region Client
42
+ function getClientQueryKey() {
43
+ return [ROOT_QUERY_KEY, 'client'];
44
+ }
45
+ export function getDeviceInfoQueryKey() {
46
+ return [...getClientQueryKey(), 'device_info'];
47
+ }
48
+ export function getIsArchiveDeviceQueryKey() {
49
+ return [...getClientQueryKey(), 'is_archive_device'];
50
+ }
51
+ // #endregion
52
+ // #region Invites
53
+ export function getInvitesQueryKey() {
54
+ return [ROOT_QUERY_KEY, 'invites'];
55
+ }
56
+ export function getInvitesByIdQueryKey({ inviteId }) {
57
+ return [ROOT_QUERY_KEY, 'invites', { inviteId }];
58
+ }
59
+ // #endregion
60
+ // #region Maps
61
+ const MAPS_ROOT_QUERY_KEY = [ROOT_QUERY_KEY, 'maps'];
62
+ export function getMapQueryKey({ mapId }) {
63
+ return [...MAPS_ROOT_QUERY_KEY, mapId];
64
+ }
65
+ export function getMapInfoQueryKey({ mapId }) {
66
+ return [...getMapQueryKey({ mapId }), 'info'];
67
+ }
68
+ export function getStyleJsonUrlQueryKey({ mapId }) {
69
+ return [...getMapQueryKey({ mapId }), 'stylejson_url'];
70
+ }
71
+ /**
72
+ * Invalidate queries for this map and the default map (which internally
73
+ * redirects to custom) so that they will be refetched with the new map data.
74
+ */
75
+ export async function invalidateMapQueries(queryClient, { mapId }) {
76
+ await Promise.all([
77
+ queryClient.invalidateQueries({
78
+ queryKey: getMapQueryKey({ mapId }),
79
+ }),
80
+ queryClient.invalidateQueries({
81
+ queryKey: getMapQueryKey({ mapId: DEFAULT_MAP_ID }),
82
+ }),
83
+ ]);
84
+ }
85
+ // #endregion
86
+ // #region Projects
87
+ export function getProjectsQueryKey() {
88
+ return [ROOT_QUERY_KEY, 'projects'];
89
+ }
90
+ export function getProjectByIdQueryKey({ projectId }) {
91
+ return [ROOT_QUERY_KEY, 'projects', projectId];
92
+ }
93
+ export function getProjectSettingsQueryKey({ projectId, }) {
94
+ return [ROOT_QUERY_KEY, 'projects', projectId, 'project_settings'];
95
+ }
96
+ export function getProjectRoleQueryKey({ projectId }) {
97
+ return [ROOT_QUERY_KEY, 'projects', projectId, 'role'];
98
+ }
99
+ export function getMembersQueryKey({ projectId, includeLeft, }) {
100
+ return [
101
+ ROOT_QUERY_KEY,
102
+ 'projects',
103
+ projectId,
104
+ 'members',
105
+ { includeLeft },
106
+ ];
107
+ }
108
+ export function getMemberByIdQueryKey({ projectId, deviceId, }) {
109
+ return [ROOT_QUERY_KEY, 'projects', projectId, 'members', deviceId];
110
+ }
111
+ export function getDocumentCreatedByQueryKey({ projectId, originalVersionId, }) {
112
+ return [
113
+ ROOT_QUERY_KEY,
114
+ 'projects',
115
+ projectId,
116
+ 'document_created_by',
117
+ originalVersionId,
118
+ ];
119
+ }
120
+ /**
121
+ * We call this within a project hook, because that's the only place the API is
122
+ * exposed right now, but it is the same for all projects, so no need for
123
+ * scoping the query key to the project
124
+ */
125
+ export function getMediaServerOriginQueryKey() {
126
+ return [ROOT_QUERY_KEY, 'media_server_origin'];
127
+ }
128
+ // #endregion
129
+ // #region Documents
130
+ export function getDocumentsQueryKey({ projectId, docType, }) {
131
+ return [ROOT_QUERY_KEY, 'projects', projectId, docType];
132
+ }
133
+ export function getManyDocumentsQueryKey({ projectId, docType, includeDeleted, lang, }) {
134
+ return [
135
+ ROOT_QUERY_KEY,
136
+ 'projects',
137
+ projectId,
138
+ docType,
139
+ { includeDeleted, lang },
140
+ ];
141
+ }
142
+ export function getDocumentByDocIdQueryKey({ projectId, docType, docId, lang, }) {
143
+ return [
144
+ ROOT_QUERY_KEY,
145
+ 'projects',
146
+ projectId,
147
+ docType,
148
+ docId,
149
+ { lang },
150
+ ];
151
+ }
152
+ export function getDocumentByVersionIdQueryKey({ projectId, docType, versionId, lang, }) {
153
+ return [
154
+ ROOT_QUERY_KEY,
155
+ 'projects',
156
+ projectId,
157
+ docType,
158
+ versionId,
159
+ { lang },
160
+ ];
161
+ }
162
+ // #endregion
@@ -1,12 +1,9 @@
1
- import type { MapeoProjectApi } from '@comapeo/ipc' with {
2
- 'resolution-mode': 'import'
3
- };
1
+ import type { MapeoProjectApi } from '@comapeo/ipc';
4
2
  export type SyncState = Awaited<ReturnType<MapeoProjectApi['$sync']['getState']>>;
5
- export declare function getDataSyncCountForDevice(syncStateForDevice: SyncState['remoteDeviceSyncState'][string]): number;
6
3
  export declare class SyncStore {
7
4
  #private;
8
5
  constructor(project: MapeoProjectApi);
9
6
  subscribe: (listener: () => void) => () => void;
10
- getStateSnapshot: () => import("@comapeo/core/dist/sync/sync-api.js").State | null;
7
+ getStateSnapshot: () => SyncState | null;
11
8
  getDataProgressSnapshot: () => number | null;
12
9
  }
@@ -1,4 +1,4 @@
1
- export function getDataSyncCountForDevice(syncStateForDevice) {
1
+ function getDataSyncCountForDevice(syncStateForDevice) {
2
2
  const { data } = syncStateForDevice;
3
3
  return data.want + data.wanted;
4
4
  }
@@ -1,11 +1,9 @@
1
- import type { MapeoDoc, MapeoValue } from '@comapeo/schema' with {
2
- 'resolution-mode': 'import'
3
- };
4
- export type WriteableDocumentType = Extract<MapeoDoc['schemaName'], 'field' | 'observation' | 'preset' | 'track' | 'remoteDetectionAlert'>;
5
- export type WriteableValue<D extends WriteableDocumentType> = Extract<MapeoValue, {
1
+ import type { ComapeoDoc, ComapeoValue } from '@comapeo/core/schema.js';
2
+ export type WriteableDocumentType = Extract<ComapeoDoc['schemaName'], 'field' | 'observation' | 'preset' | 'track' | 'remoteDetectionAlert'>;
3
+ export type WriteableValue<D extends WriteableDocumentType> = Extract<ComapeoValue, {
6
4
  schemaName: D;
7
5
  }>;
8
- export type WriteableDocument<D extends WriteableDocumentType> = Extract<MapeoDoc, {
6
+ export type WriteableDocument<D extends WriteableDocumentType> = Extract<ComapeoDoc, {
9
7
  schemaName: D;
10
8
  }>;
11
9
  export {};