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