@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
@@ -1,149 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDocumentsQueryKey = getDocumentsQueryKey;
4
- exports.getManyDocumentsQueryKey = getManyDocumentsQueryKey;
5
- exports.getDocumentByDocIdQueryKey = getDocumentByDocIdQueryKey;
6
- exports.getDocumentByVersionIdQueryKey = getDocumentByVersionIdQueryKey;
7
- exports.documentsQueryOptions = documentsQueryOptions;
8
- exports.documentByDocumentIdQueryOptions = documentByDocumentIdQueryOptions;
9
- exports.documentByVersionIdQueryOptions = documentByVersionIdQueryOptions;
10
- exports.createDocumentMutationOptions = createDocumentMutationOptions;
11
- exports.updateDocumentMutationOptions = updateDocumentMutationOptions;
12
- exports.deleteDocumentMutationOptions = deleteDocumentMutationOptions;
13
- const react_query_1 = require("@tanstack/react-query");
14
- const shared_js_1 = require("./shared.js");
15
- function getDocumentsQueryKey({ projectId, docType, }) {
16
- return [shared_js_1.ROOT_QUERY_KEY, 'projects', projectId, docType];
17
- }
18
- function getManyDocumentsQueryKey({ projectId, docType, includeDeleted, lang, }) {
19
- return [
20
- shared_js_1.ROOT_QUERY_KEY,
21
- 'projects',
22
- projectId,
23
- docType,
24
- { includeDeleted, lang },
25
- ];
26
- }
27
- function getDocumentByDocIdQueryKey({ projectId, docType, docId, lang, }) {
28
- return [
29
- shared_js_1.ROOT_QUERY_KEY,
30
- 'projects',
31
- projectId,
32
- docType,
33
- docId,
34
- { lang },
35
- ];
36
- }
37
- function getDocumentByVersionIdQueryKey({ projectId, docType, versionId, lang, }) {
38
- return [
39
- shared_js_1.ROOT_QUERY_KEY,
40
- 'projects',
41
- projectId,
42
- docType,
43
- versionId,
44
- { lang },
45
- ];
46
- }
47
- function documentsQueryOptions({ projectApi, projectId, docType, includeDeleted, lang, }) {
48
- return (0, react_query_1.queryOptions)({
49
- ...(0, shared_js_1.baseQueryOptions)(),
50
- queryKey: getManyDocumentsQueryKey({
51
- projectId,
52
- docType,
53
- includeDeleted,
54
- lang,
55
- }),
56
- queryFn: async () => {
57
- return projectApi[docType].getMany({
58
- includeDeleted,
59
- lang,
60
- });
61
- },
62
- });
63
- }
64
- function documentByDocumentIdQueryOptions({ projectApi, projectId, docType, docId, lang, }) {
65
- return (0, react_query_1.queryOptions)({
66
- ...(0, shared_js_1.baseQueryOptions)(),
67
- queryKey: getDocumentByDocIdQueryKey({
68
- projectId,
69
- docType,
70
- docId,
71
- lang,
72
- }),
73
- queryFn: async () => {
74
- return projectApi[docType].getByDocId(docId, {
75
- lang,
76
- // We want to make sure that this throws in the case that no match is found
77
- mustBeFound: true,
78
- });
79
- },
80
- });
81
- }
82
- function documentByVersionIdQueryOptions({ projectApi, projectId, docType, versionId, lang, }) {
83
- return (0, react_query_1.queryOptions)({
84
- ...(0, shared_js_1.baseQueryOptions)(),
85
- queryKey: getDocumentByVersionIdQueryKey({
86
- projectId,
87
- docType,
88
- versionId,
89
- lang,
90
- }),
91
- queryFn: async () => {
92
- return projectApi[docType].getByVersionId(versionId, { lang });
93
- },
94
- });
95
- }
96
- function createDocumentMutationOptions({ docType, projectApi, projectId, queryClient, }) {
97
- return {
98
- ...(0, shared_js_1.baseMutationOptions)(),
99
- mutationFn: async ({ value }) => {
100
- // @ts-expect-error TS not handling this well
101
- return projectApi[docType].create({
102
- ...value,
103
- schemaName: docType,
104
- });
105
- },
106
- onSuccess: () => {
107
- queryClient.invalidateQueries({
108
- queryKey: getDocumentsQueryKey({
109
- projectId,
110
- docType,
111
- }),
112
- });
113
- },
114
- };
115
- }
116
- function updateDocumentMutationOptions({ docType, projectApi, projectId, queryClient, }) {
117
- return {
118
- ...(0, shared_js_1.baseMutationOptions)(),
119
- mutationFn: async ({ versionId, value }) => {
120
- // @ts-expect-error TS not handling this well
121
- return projectApi[docType].update(versionId, value);
122
- },
123
- onSuccess: () => {
124
- queryClient.invalidateQueries({
125
- queryKey: getDocumentsQueryKey({
126
- projectId,
127
- docType,
128
- }),
129
- });
130
- },
131
- };
132
- }
133
- function deleteDocumentMutationOptions({ docType, projectApi, projectId, queryClient, }) {
134
- return {
135
- ...(0, shared_js_1.baseMutationOptions)(),
136
- mutationFn: async ({ docId }) => {
137
- // @ts-expect-error TS not handling this well
138
- return projectApi[docType].delete(docId);
139
- },
140
- onSuccess: () => {
141
- queryClient.invalidateQueries({
142
- queryKey: getDocumentsQueryKey({
143
- projectId,
144
- docType,
145
- }),
146
- });
147
- },
148
- };
149
- }
@@ -1,88 +0,0 @@
1
- import type { MemberApi } from '@comapeo/core' with {
2
- 'resolution-mode': 'import'
3
- };
4
- import type { MapeoClientApi, MapeoProjectApi } from '@comapeo/ipc' with {
5
- 'resolution-mode': 'import'
6
- };
7
- import { type QueryClient } from '@tanstack/react-query';
8
- export declare function getInvitesQueryKey(): readonly ["@comapeo/core-react", "invites"];
9
- export declare function getInvitesByIdQueryKey({ inviteId }: {
10
- inviteId: string;
11
- }): readonly ["@comapeo/core-react", "invites", {
12
- readonly inviteId: string;
13
- }];
14
- export declare function getInvitesQueryOptions({ clientApi, }: {
15
- clientApi: MapeoClientApi;
16
- }): import("@tanstack/react-query").OmitKeyof<import("@tanstack/react-query").UseQueryOptions<import("@comapeo/core/dist/invite/invite-api.js", { with: { "resolution-mode": "import" } }).Invite[], Error, import("@comapeo/core/dist/invite/invite-api.js", { with: { "resolution-mode": "import" } }).Invite[], readonly ["@comapeo/core-react", "invites"]>, "queryFn"> & {
17
- queryFn?: import("@tanstack/react-query").QueryFunction<import("@comapeo/core/dist/invite/invite-api.js", { with: { "resolution-mode": "import" } }).Invite[], readonly ["@comapeo/core-react", "invites"], never> | undefined;
18
- } & {
19
- queryKey: readonly ["@comapeo/core-react", "invites"] & {
20
- [dataTagSymbol]: import("@comapeo/core/dist/invite/invite-api.js", { with: { "resolution-mode": "import" } }).Invite[];
21
- [dataTagErrorSymbol]: Error;
22
- };
23
- };
24
- export declare function getInviteByIdQueryOptions({ clientApi, inviteId, }: {
25
- clientApi: MapeoClientApi;
26
- inviteId: string;
27
- }): import("@tanstack/react-query").OmitKeyof<import("@tanstack/react-query").UseQueryOptions<import("@comapeo/core/dist/invite/invite-api.js", { with: { "resolution-mode": "import" } }).Invite, Error, import("@comapeo/core/dist/invite/invite-api.js", { with: { "resolution-mode": "import" } }).Invite, readonly ["@comapeo/core-react", "invites", {
28
- readonly inviteId: string;
29
- }]>, "queryFn"> & {
30
- queryFn?: import("@tanstack/react-query").QueryFunction<import("@comapeo/core/dist/invite/invite-api.js", { with: { "resolution-mode": "import" } }).Invite, readonly ["@comapeo/core-react", "invites", {
31
- readonly inviteId: string;
32
- }], never> | undefined;
33
- } & {
34
- queryKey: readonly ["@comapeo/core-react", "invites", {
35
- readonly inviteId: string;
36
- }] & {
37
- [dataTagSymbol]: import("@comapeo/core/dist/invite/invite-api.js", { with: { "resolution-mode": "import" } }).Invite;
38
- [dataTagErrorSymbol]: Error;
39
- };
40
- };
41
- export declare function acceptInviteMutationOptions({ clientApi, queryClient, }: {
42
- clientApi: MapeoClientApi;
43
- queryClient: QueryClient;
44
- }): {
45
- mutationFn: ({ inviteId }: {
46
- inviteId: string;
47
- }) => Promise<string>;
48
- onSuccess: () => void;
49
- networkMode: "always";
50
- retry: false;
51
- };
52
- export declare function rejectInviteMutationOptions({ clientApi, queryClient, }: {
53
- clientApi: MapeoClientApi;
54
- queryClient: QueryClient;
55
- }): {
56
- mutationFn: ({ inviteId }: {
57
- inviteId: string;
58
- }) => Promise<void>;
59
- onSuccess: () => void;
60
- networkMode: "always";
61
- retry: false;
62
- };
63
- export declare function sendInviteMutationOptions({ projectApi, projectId, queryClient, }: {
64
- projectApi: MapeoProjectApi;
65
- projectId: string;
66
- queryClient: QueryClient;
67
- }): {
68
- mutationFn: ({ deviceId, ...role }: {
69
- deviceId: string;
70
- roleDescription?: string;
71
- roleId: MemberApi.RoleIdForNewInvite;
72
- roleName?: string;
73
- }) => Promise<"ACCEPT" | "REJECT" | "ALREADY">;
74
- onSuccess: () => void;
75
- networkMode: "always";
76
- retry: false;
77
- };
78
- export declare function requestCancelInviteMutationOptions({ projectApi, queryClient, }: {
79
- projectApi: MapeoProjectApi;
80
- queryClient: QueryClient;
81
- }): {
82
- mutationFn: ({ deviceId }: {
83
- deviceId: string;
84
- }) => Promise<void>;
85
- onSuccess: () => void;
86
- networkMode: "always";
87
- retry: false;
88
- };
@@ -1,95 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getInvitesQueryKey = getInvitesQueryKey;
4
- exports.getInvitesByIdQueryKey = getInvitesByIdQueryKey;
5
- exports.getInvitesQueryOptions = getInvitesQueryOptions;
6
- exports.getInviteByIdQueryOptions = getInviteByIdQueryOptions;
7
- exports.acceptInviteMutationOptions = acceptInviteMutationOptions;
8
- exports.rejectInviteMutationOptions = rejectInviteMutationOptions;
9
- exports.sendInviteMutationOptions = sendInviteMutationOptions;
10
- exports.requestCancelInviteMutationOptions = requestCancelInviteMutationOptions;
11
- const react_query_1 = require("@tanstack/react-query");
12
- const projects_js_1 = require("./projects.js");
13
- const shared_js_1 = require("./shared.js");
14
- function getInvitesQueryKey() {
15
- return [shared_js_1.ROOT_QUERY_KEY, 'invites'];
16
- }
17
- function getInvitesByIdQueryKey({ inviteId }) {
18
- return [shared_js_1.ROOT_QUERY_KEY, 'invites', { inviteId }];
19
- }
20
- function getInvitesQueryOptions({ clientApi, }) {
21
- return (0, react_query_1.queryOptions)({
22
- ...(0, shared_js_1.baseQueryOptions)(),
23
- queryKey: getInvitesQueryKey(),
24
- queryFn: async () => {
25
- return clientApi.invite.getMany();
26
- },
27
- });
28
- }
29
- function getInviteByIdQueryOptions({ clientApi, inviteId, }) {
30
- return (0, react_query_1.queryOptions)({
31
- ...(0, shared_js_1.baseQueryOptions)(),
32
- queryKey: getInvitesByIdQueryKey({ inviteId }),
33
- queryFn: async () => {
34
- return clientApi.invite.getById(inviteId);
35
- },
36
- });
37
- }
38
- function acceptInviteMutationOptions({ clientApi, queryClient, }) {
39
- return {
40
- ...(0, shared_js_1.baseMutationOptions)(),
41
- mutationFn: async ({ inviteId }) => {
42
- return clientApi.invite.accept({ inviteId });
43
- },
44
- onSuccess: () => {
45
- queryClient.invalidateQueries({
46
- queryKey: getInvitesQueryKey(),
47
- });
48
- queryClient.invalidateQueries({
49
- queryKey: (0, projects_js_1.getProjectsQueryKey)(),
50
- });
51
- },
52
- };
53
- }
54
- function rejectInviteMutationOptions({ clientApi, queryClient, }) {
55
- return {
56
- ...(0, shared_js_1.baseMutationOptions)(),
57
- mutationFn: async ({ inviteId }) => {
58
- return clientApi.invite.reject({ inviteId });
59
- },
60
- onSuccess: () => {
61
- queryClient.invalidateQueries({
62
- queryKey: getInvitesQueryKey(),
63
- });
64
- },
65
- };
66
- }
67
- function sendInviteMutationOptions({ projectApi, projectId, queryClient, }) {
68
- return {
69
- ...(0, shared_js_1.baseMutationOptions)(),
70
- mutationFn: async ({ deviceId, ...role }) => {
71
- return projectApi.$member.invite(deviceId, role);
72
- },
73
- onSuccess: () => {
74
- queryClient.invalidateQueries({
75
- queryKey: getInvitesQueryKey(),
76
- });
77
- queryClient.invalidateQueries({
78
- queryKey: (0, projects_js_1.getMembersQueryKey)({ projectId }),
79
- });
80
- },
81
- };
82
- }
83
- function requestCancelInviteMutationOptions({ projectApi, queryClient, }) {
84
- return {
85
- ...(0, shared_js_1.baseMutationOptions)(),
86
- mutationFn: async ({ deviceId }) => {
87
- return projectApi.$member.requestCancelInvite(deviceId);
88
- },
89
- onSuccess: () => {
90
- queryClient.invalidateQueries({
91
- queryKey: getInvitesQueryKey(),
92
- });
93
- },
94
- };
95
- }
@@ -1,72 +0,0 @@
1
- import { type QueryClient, type UseMutationOptions } from '@tanstack/react-query';
2
- import type { MapServerApi } from '../../contexts/MapServer.js';
3
- import type { ReceivedMapSharesStore, SentMapSharesStore } from '../map-shares-stores.js';
4
- type CompatFile = Omit<File, 'lastModified' | 'webkitRelativePath'>;
5
- type ExpoFileDuckType = CompatFile & {
6
- exists: boolean;
7
- };
8
- export declare function getMapQueryKey({ mapId }: {
9
- mapId: string;
10
- }): readonly ["@comapeo/core-react", "maps", string];
11
- export declare function getStyleJsonUrlQueryKey({ mapId }: {
12
- mapId: string;
13
- }): readonly ["@comapeo/core-react", "maps", string, "stylejson_url"];
14
- /**
15
- * Invalidate queries for this map and the default map (which internally
16
- * redirects to custom) so that they will be refetched with the new map data.
17
- */
18
- export declare function invalidateMapQueries(queryClient: QueryClient, { mapId }: {
19
- mapId: string;
20
- }): Promise<void>;
21
- export declare function mapStyleJsonUrlQueryOptions({ mapServerApi, mapId, }: {
22
- mapServerApi: MapServerApi;
23
- mapId?: string;
24
- }): import("@tanstack/react-query").OmitKeyof<import("@tanstack/react-query").UseQueryOptions<string, Error, string, readonly ["@comapeo/core-react", "maps", string, "stylejson_url"]>, "queryFn"> & {
25
- queryFn?: import("@tanstack/react-query").QueryFunction<string, readonly ["@comapeo/core-react", "maps", string, "stylejson_url"], never> | undefined;
26
- } & {
27
- queryKey: readonly ["@comapeo/core-react", "maps", string, "stylejson_url"] & {
28
- [dataTagSymbol]: string;
29
- [dataTagErrorSymbol]: Error;
30
- };
31
- };
32
- export declare function mapInfoQueryOptions({ mapServerApi, mapId, }: {
33
- mapServerApi: MapServerApi;
34
- mapId?: string;
35
- }): import("@tanstack/react-query").OmitKeyof<import("@tanstack/react-query").UseQueryOptions<unknown, Error, unknown, readonly ["@comapeo/core-react", "maps", string, "info"]>, "queryFn"> & {
36
- queryFn?: import("@tanstack/react-query").QueryFunction<unknown, readonly ["@comapeo/core-react", "maps", string, "info"], never> | undefined;
37
- } & {
38
- queryKey: readonly ["@comapeo/core-react", "maps", string, "info"] & {
39
- [dataTagSymbol]: unknown;
40
- [dataTagErrorSymbol]: Error;
41
- };
42
- };
43
- export declare function mapImportMutationOptions({ mapServerApi, queryClient, }: {
44
- mapServerApi: MapServerApi;
45
- queryClient: QueryClient;
46
- }): {
47
- mutationFn: ({ file }: {
48
- file: File | ExpoFileDuckType;
49
- }) => Promise<Response>;
50
- onSuccess: () => Promise<void>;
51
- networkMode: "always";
52
- retry: false;
53
- };
54
- export declare function mapRemoveMutationOptions({ mapServerApi, queryClient, }: {
55
- mapServerApi: MapServerApi;
56
- queryClient: QueryClient;
57
- }): {
58
- mutationFn: () => Promise<Response>;
59
- onSuccess: () => Promise<void>;
60
- networkMode: "always";
61
- retry: false;
62
- };
63
- /**
64
- * Mutation options for actions on sent map shares
65
- */
66
- export declare function mapSharesMutationOptions<TAction extends SentMapSharesStore['actions'][keyof SentMapSharesStore['actions']] | keyof ReceivedMapSharesStore['actions'][keyof ReceivedMapSharesStore['actions']]>(options: {
67
- action: Exclude<TAction, SentMapSharesStore['actions']['createAndSend']>;
68
- } | {
69
- action: SentMapSharesStore['actions']['createAndSend'];
70
- projectId: string;
71
- }): UseMutationOptions<ReturnType<TAction>, Error, TAction extends SentMapSharesStore['actions']['createAndSend'] ? Parameters<TAction>[0] : Omit<Parameters<TAction>[0], 'projectId'>>;
72
- export {};
@@ -1,129 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMapQueryKey = getMapQueryKey;
4
- exports.getStyleJsonUrlQueryKey = getStyleJsonUrlQueryKey;
5
- exports.invalidateMapQueries = invalidateMapQueries;
6
- exports.mapStyleJsonUrlQueryOptions = mapStyleJsonUrlQueryOptions;
7
- exports.mapInfoQueryOptions = mapInfoQueryOptions;
8
- exports.mapImportMutationOptions = mapImportMutationOptions;
9
- exports.mapRemoveMutationOptions = mapRemoveMutationOptions;
10
- exports.mapSharesMutationOptions = mapSharesMutationOptions;
11
- const constants_js_1 = require("@comapeo/map-server/constants.js");
12
- const react_query_1 = require("@tanstack/react-query");
13
- const shared_js_1 = require("./shared.js");
14
- // ============================================
15
- // QUERY KEYS
16
- // ============================================
17
- const MAPS_ROOT_QUERY_KEY = [shared_js_1.ROOT_QUERY_KEY, 'maps'];
18
- function getMapQueryKey({ mapId }) {
19
- return [...MAPS_ROOT_QUERY_KEY, mapId];
20
- }
21
- function getStyleJsonUrlQueryKey({ mapId }) {
22
- return [...getMapQueryKey({ mapId }), 'stylejson_url'];
23
- }
24
- /**
25
- * Invalidate queries for this map and the default map (which internally
26
- * redirects to custom) so that they will be refetched with the new map data.
27
- */
28
- async function invalidateMapQueries(queryClient, { mapId }) {
29
- await Promise.all([
30
- queryClient.invalidateQueries({
31
- queryKey: getMapQueryKey({ mapId }),
32
- }),
33
- queryClient.invalidateQueries({
34
- queryKey: getMapQueryKey({ mapId: constants_js_1.DEFAULT_MAP_ID }),
35
- }),
36
- ]);
37
- }
38
- // ============================================
39
- // QUERY OPTIONS
40
- // ============================================
41
- function mapStyleJsonUrlQueryOptions({ mapServerApi, mapId = constants_js_1.DEFAULT_MAP_ID, }) {
42
- if (mapId !== constants_js_1.DEFAULT_MAP_ID) {
43
- throw new Error('Custom map IDs are not supported yet');
44
- }
45
- return (0, react_query_1.queryOptions)({
46
- ...(0, shared_js_1.baseQueryOptions)(),
47
- queryKey: getStyleJsonUrlQueryKey({ mapId }),
48
- queryFn: async () => {
49
- const result = await mapServerApi.getMapStyleJsonUrl(mapId);
50
- const u = new URL(result);
51
- // This ensures that every time this query is refetched, it will have a different search param, forcing the map to reload.
52
- u.searchParams.set('refresh_token', Date.now().toString());
53
- return u.href;
54
- },
55
- // Keep this cached until the cache is manually invalidated by a map upload
56
- staleTime: Infinity,
57
- gcTime: Infinity,
58
- });
59
- }
60
- function mapInfoQueryOptions({ mapServerApi, mapId = constants_js_1.DEFAULT_MAP_ID, }) {
61
- if (mapId !== constants_js_1.CUSTOM_MAP_ID) {
62
- throw new Error('Only custom map ID is currently supported');
63
- }
64
- return (0, react_query_1.queryOptions)({
65
- ...(0, shared_js_1.baseQueryOptions)(),
66
- queryKey: [...getMapQueryKey({ mapId }), 'info'],
67
- queryFn: async () => {
68
- return mapServerApi.get(`maps/${mapId}/info`).json();
69
- },
70
- // Keep this cached until the cache is manually invalidated by a map upload
71
- staleTime: Infinity,
72
- gcTime: Infinity,
73
- });
74
- }
75
- // ============================================
76
- // MUTATION OPTIONS
77
- // ============================================
78
- function mapImportMutationOptions({ mapServerApi, queryClient, }) {
79
- // TODO: Support importing to custom map IDs, to support multiple maps.
80
- const mapId = constants_js_1.CUSTOM_MAP_ID;
81
- return {
82
- ...(0, shared_js_1.baseMutationOptions)(),
83
- mutationFn: async ({ file }) => {
84
- if ('exists' in file && !file.exists) {
85
- throw new Error('File does not exist or is not accessible');
86
- }
87
- return mapServerApi.put(`maps/${mapId}`, {
88
- body: file,
89
- headers: {
90
- 'Content-Type': 'application/octet-stream',
91
- },
92
- });
93
- },
94
- onSuccess: async () => {
95
- await invalidateMapQueries(queryClient, { mapId });
96
- },
97
- };
98
- }
99
- function mapRemoveMutationOptions({ mapServerApi, queryClient, }) {
100
- // TODO: Support removing from custom map IDs, to support multiple maps.
101
- const mapId = constants_js_1.CUSTOM_MAP_ID;
102
- return {
103
- ...(0, shared_js_1.baseMutationOptions)(),
104
- mutationFn: async () => {
105
- return mapServerApi.delete(`maps/${mapId}`);
106
- },
107
- onSuccess: async () => {
108
- await invalidateMapQueries(queryClient, { mapId });
109
- },
110
- };
111
- }
112
- /**
113
- * Mutation options for actions on sent map shares
114
- */
115
- function mapSharesMutationOptions(options) {
116
- return {
117
- ...(0, shared_js_1.baseMutationOptions)(),
118
- mutationFn: async (variables) => {
119
- // For consistency with other hooks, we use `projectId` as a parameter of
120
- // the hook, rather than a parameter of the mutate function.
121
- const actionOptions = 'projectId' in options
122
- ? { ...variables, projectId: options.projectId }
123
- : variables;
124
- return options.action(
125
- // @ts-expect-error - TS can't help us here
126
- actionOptions);
127
- },
128
- };
129
- }
@@ -1,8 +0,0 @@
1
- import type { UseMutationResult } from '@tanstack/react-query';
2
- import type { DistributedPick } from 'type-fest';
3
- /**
4
- * Filters a `UseMutationResult` to only include a subset of its keys, and uses
5
- * `DistributedPick` to preserve the discriminated union types of the mutation
6
- * result based on the `status` property.
7
- */
8
- export declare function filterMutationResult<TResult extends UseMutationResult<any, any, any, any>>(mutationResult: TResult): DistributedPick<TResult, "error" | "status" | "mutate" | "reset" | "mutateAsync">;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.filterMutationResult = filterMutationResult;
4
- const PICKED_MUTATION_RESULT_KEYS = [
5
- 'error',
6
- 'mutate',
7
- 'mutateAsync',
8
- 'reset',
9
- 'status',
10
- ];
11
- /**
12
- * Filters a `UseMutationResult` to only include a subset of its keys, and uses
13
- * `DistributedPick` to preserve the discriminated union types of the mutation
14
- * result based on the `status` property.
15
- */
16
- function filterMutationResult(mutationResult) {
17
- const filteredResult = {};
18
- for (const key of PICKED_MUTATION_RESULT_KEYS) {
19
- filteredResult[key] = mutationResult[key];
20
- }
21
- return filteredResult;
22
- }