@comapeo/core-react 8.0.0 → 9.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.
- package/README.md +38 -0
- package/dist/commonjs/contexts/ClientApi.d.ts +5 -5
- package/dist/commonjs/contexts/ClientApi.js +6 -5
- package/dist/commonjs/contexts/ComapeoCore.d.ts +8 -0
- package/dist/commonjs/contexts/ComapeoCore.js +9 -0
- package/dist/commonjs/contexts/MapServer.d.ts +69 -0
- package/dist/commonjs/contexts/MapServer.js +92 -0
- package/dist/commonjs/contexts/MapShares.d.ts +52 -0
- package/dist/commonjs/contexts/MapShares.js +74 -0
- package/dist/commonjs/hooks/maps.d.ts +460 -3
- package/dist/commonjs/hooks/maps.js +261 -4
- package/dist/commonjs/index.d.ts +5 -2
- package/dist/commonjs/index.js +20 -3
- package/dist/commonjs/lib/http.d.ts +45 -0
- package/dist/commonjs/lib/http.js +103 -0
- package/dist/commonjs/lib/map-shares-stores.d.ts +80 -0
- package/dist/commonjs/lib/map-shares-stores.js +299 -0
- package/dist/commonjs/lib/react-query/maps.d.ts +66 -20
- package/dist/commonjs/lib/react-query/maps.js +113 -11
- package/dist/commonjs/lib/react-query/mutation-result.d.ts +8 -0
- package/dist/commonjs/lib/react-query/mutation-result.js +22 -0
- package/dist/esm/contexts/ClientApi.d.ts +5 -5
- package/dist/esm/contexts/ClientApi.js +6 -5
- package/dist/esm/contexts/ComapeoCore.d.ts +8 -0
- package/dist/esm/contexts/ComapeoCore.js +6 -0
- package/dist/esm/contexts/MapServer.d.ts +69 -0
- package/dist/esm/contexts/MapServer.js +86 -0
- package/dist/esm/contexts/MapShares.d.ts +52 -0
- package/dist/esm/contexts/MapShares.js +65 -0
- package/dist/esm/hooks/maps.d.ts +460 -3
- package/dist/esm/hooks/maps.js +252 -6
- package/dist/esm/index.d.ts +5 -2
- package/dist/esm/index.js +4 -2
- package/dist/esm/lib/http.d.ts +45 -0
- package/dist/esm/lib/http.js +98 -0
- package/dist/esm/lib/map-shares-stores.d.ts +80 -0
- package/dist/esm/lib/map-shares-stores.js +291 -0
- package/dist/esm/lib/react-query/maps.d.ts +66 -20
- package/dist/esm/lib/react-query/maps.js +109 -12
- package/dist/esm/lib/react-query/mutation-result.d.ts +8 -0
- package/dist/esm/lib/react-query/mutation-result.js +19 -0
- package/docs/API.md +567 -60
- package/package.json +15 -4
package/README.md
CHANGED
|
@@ -10,6 +10,44 @@ React wrapper for working with [`@comapeo/core`](https://github.com/digidem/coma
|
|
|
10
10
|
npm install react @tanstack/react-query@5 @comapeo/core-react @comapeo/schema @comapeo/core @comapeo/ipc
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
### Basic Setup
|
|
16
|
+
|
|
17
|
+
Wrap your application with `ComapeoCoreProvider` and a React Query `QueryClientProvider`. You will need to be running an instance of [`@comapeo/map-server`](https://github.com/digidem/comapeo-map-server) and provide a `getMapServerBaseUrl` function that returns a Promise resolving to the base URL of your map server:
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { ComapeoCoreProvider } from '@comapeo/core-react'
|
|
21
|
+
import { createServer } from '@comapeo/map-server'
|
|
22
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
23
|
+
|
|
24
|
+
const queryClient = new QueryClient()
|
|
25
|
+
|
|
26
|
+
const server = createServer()
|
|
27
|
+
const listenPromise = server.listen()
|
|
28
|
+
|
|
29
|
+
const getMapServerBaseUrl = async () => {
|
|
30
|
+
const { localPort } = await listenPromise
|
|
31
|
+
return new URL(`http://localhost:${localPort}/`)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function App() {
|
|
35
|
+
return (
|
|
36
|
+
<QueryClientProvider client={queryClient}>
|
|
37
|
+
<ComapeoCoreProvider
|
|
38
|
+
clientApi={clientApi}
|
|
39
|
+
queryClient={queryClient}
|
|
40
|
+
getMapServerBaseUrl={getMapServerBaseUrl}
|
|
41
|
+
>
|
|
42
|
+
<MyApp />
|
|
43
|
+
</ComapeoCoreProvider>
|
|
44
|
+
</QueryClientProvider>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Hooks that communicate with the map server will wait for `getMapServerBaseUrl()` to resolve before making requests, so the provider can be mounted before the server is ready. You can also provide an optional `fetch` prop to use a custom fetch implementation.
|
|
50
|
+
|
|
13
51
|
## API Documentation
|
|
14
52
|
|
|
15
53
|
Still a work in progress. Currently lives in [`docs/API.md`](./docs/API.md).
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { MapeoClientApi } from '@comapeo/ipc' with {
|
|
2
2
|
'resolution-mode': 'import'
|
|
3
3
|
};
|
|
4
|
-
import { type Context, type JSX, type
|
|
4
|
+
import { type Context, type JSX, type PropsWithChildren } from 'react';
|
|
5
5
|
export declare const ClientApiContext: Context<MapeoClientApi | null>;
|
|
6
|
+
export type ClientApiProviderProps = PropsWithChildren<{
|
|
7
|
+
clientApi: MapeoClientApi;
|
|
8
|
+
}>;
|
|
6
9
|
/**
|
|
7
10
|
* Create a context provider that holds a CoMapeo API client instance.
|
|
8
11
|
*
|
|
@@ -10,7 +13,4 @@ export declare const ClientApiContext: Context<MapeoClientApi | null>;
|
|
|
10
13
|
* @param opts.clientApi Client API instance
|
|
11
14
|
*
|
|
12
15
|
*/
|
|
13
|
-
export declare function ClientApiProvider({ children, clientApi, }:
|
|
14
|
-
children: ReactNode;
|
|
15
|
-
clientApi: MapeoClientApi;
|
|
16
|
-
}): JSX.Element;
|
|
16
|
+
export declare function ClientApiProvider({ children, clientApi, }: ClientApiProviderProps): JSX.Element;
|
|
@@ -16,14 +16,15 @@ exports.ClientApiContext = (0, react_1.createContext)(null);
|
|
|
16
16
|
function ClientApiProvider({ children, clientApi, }) {
|
|
17
17
|
const queryClient = (0, react_query_1.useQueryClient)();
|
|
18
18
|
(0, react_1.useEffect)(() => {
|
|
19
|
-
function
|
|
19
|
+
function invalidateInviteCache() {
|
|
20
20
|
queryClient.invalidateQueries({ queryKey: (0, invites_js_1.getInvitesQueryKey)() });
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
clientApi.invite.addListener('invite-
|
|
22
|
+
// Invite listeners
|
|
23
|
+
clientApi.invite.addListener('invite-received', invalidateInviteCache);
|
|
24
|
+
clientApi.invite.addListener('invite-updated', invalidateInviteCache);
|
|
24
25
|
return () => {
|
|
25
|
-
clientApi.invite.removeListener('invite-received',
|
|
26
|
-
clientApi.invite.removeListener('invite-updated',
|
|
26
|
+
clientApi.invite.removeListener('invite-received', invalidateInviteCache);
|
|
27
|
+
clientApi.invite.removeListener('invite-updated', invalidateInviteCache);
|
|
27
28
|
};
|
|
28
29
|
}, [clientApi, queryClient]);
|
|
29
30
|
return (0, react_1.createElement)(exports.ClientApiContext.Provider, { value: clientApi }, children);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type JSX } from 'react';
|
|
2
|
+
import { type ClientApiProviderProps } from './ClientApi.js';
|
|
3
|
+
import { type MapServerProviderProps } from './MapServer.js';
|
|
4
|
+
type ComapeoCoreProviderProps = ClientApiProviderProps & Omit<MapServerProviderProps, 'getBaseUrl'> & {
|
|
5
|
+
getMapServerBaseUrl(): Promise<URL>;
|
|
6
|
+
};
|
|
7
|
+
export declare function ComapeoCoreProvider({ children, clientApi, getMapServerBaseUrl, fetch, queryClient, }: ComapeoCoreProviderProps): JSX.Element;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ComapeoCoreProvider = ComapeoCoreProvider;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const ClientApi_js_1 = require("./ClientApi.js");
|
|
6
|
+
const MapServer_js_1 = require("./MapServer.js");
|
|
7
|
+
function ComapeoCoreProvider({ children, clientApi, getMapServerBaseUrl, fetch, queryClient, }) {
|
|
8
|
+
return (0, react_1.createElement)(ClientApi_js_1.ClientApiProvider, { clientApi }, (0, react_1.createElement)(MapServer_js_1.MapServerProvider, { getBaseUrl: getMapServerBaseUrl, fetch, queryClient }, children));
|
|
9
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type QueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { type EventSourceClient, type EventSourceOptions } from 'eventsource-client';
|
|
3
|
+
import { type Context, type JSX, type PropsWithChildren } from 'react';
|
|
4
|
+
import { createHttp } from '../lib/http.js';
|
|
5
|
+
export type MapServerApiOptions = {
|
|
6
|
+
getBaseUrl(): Promise<URL>;
|
|
7
|
+
/**
|
|
8
|
+
* We assume the passed fetch implementation will only accept a `string` as
|
|
9
|
+
* input, not a `URL` or `Request`, because right now the expo/fetch
|
|
10
|
+
* implementation will only accept a `string`. Adding this restriction will
|
|
11
|
+
* catch potential issues if we try to pass a URL in our code. Can be relaxed
|
|
12
|
+
* when https://github.com/expo/expo/issues/43193 is fixed upstream.
|
|
13
|
+
*/
|
|
14
|
+
fetch?(input: string, options?: RequestInit): Promise<Response>;
|
|
15
|
+
};
|
|
16
|
+
export type MapServerApi = ReturnType<typeof createHttp> & {
|
|
17
|
+
createEventSource(options: EventSourceOptions): EventSourceClient;
|
|
18
|
+
getMapStyleJsonUrl(mapId: string): Promise<string>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Utility function to create a MapServerApi instance.
|
|
22
|
+
* Only exported for unit testing purposes.
|
|
23
|
+
* @private
|
|
24
|
+
*
|
|
25
|
+
* @param opts.getBaseUrl A function that returns a promise that resolves to the base URL of the map server.
|
|
26
|
+
* @param opts.fetch An optional custom fetch function to use for making requests to the map server. If not provided, the global `fetch` will be used.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createMapServerApi({ getBaseUrl, fetch, }: MapServerApiOptions): MapServerApi;
|
|
29
|
+
export declare const MapServerContext: Context<MapServerApi | null>;
|
|
30
|
+
export type MapServerProviderProps = PropsWithChildren<MapServerApiOptions & {
|
|
31
|
+
queryClient: QueryClient;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Create a context provider that holds a `MapServerFetch` function, which waits
|
|
35
|
+
* for the map server to be ready before making requests.
|
|
36
|
+
*
|
|
37
|
+
* @param opts.children React children node
|
|
38
|
+
* @param opts.getBaseUrl A function that returns a promise that resolves to the base URL of the map server.
|
|
39
|
+
* @param opts.fetch An optional custom fetch function to use for making requests to the map server. If not provided, the global `fetch` will be used.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```tsx
|
|
43
|
+
* import { createServer } from '@comapeo/map-server'
|
|
44
|
+
*
|
|
45
|
+
* const server = createServer()
|
|
46
|
+
* const listenPromise = server.listen()
|
|
47
|
+
*
|
|
48
|
+
* const getBaseUrl = async () => {
|
|
49
|
+
* const { localPort } = await listenPromise
|
|
50
|
+
* return new URL(`http://localhost:${localPort}/`)
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* const mapServerApi = createMapServerApi({ getBaseUrl })
|
|
54
|
+
*
|
|
55
|
+
* function App() {
|
|
56
|
+
* return (
|
|
57
|
+
* <MapServerProvider mapServerApi={mapServerApi}>
|
|
58
|
+
* <MyApp />
|
|
59
|
+
* </MapServerProvider>
|
|
60
|
+
* )
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function MapServerProvider({ children, getBaseUrl, fetch, queryClient, }: MapServerProviderProps): JSX.Element;
|
|
65
|
+
/**
|
|
66
|
+
* Internal hook to get the MapServerApi (instance of ky) from context.
|
|
67
|
+
* Throws if used outside of MapServerProvider.
|
|
68
|
+
*/
|
|
69
|
+
export declare function useMapServerApi(): MapServerApi;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MapServerContext = void 0;
|
|
4
|
+
exports.createMapServerApi = createMapServerApi;
|
|
5
|
+
exports.MapServerProvider = MapServerProvider;
|
|
6
|
+
exports.useMapServerApi = useMapServerApi;
|
|
7
|
+
const eventsource_client_1 = require("eventsource-client");
|
|
8
|
+
const react_1 = require("react");
|
|
9
|
+
const client_js_1 = require("../hooks/client.js");
|
|
10
|
+
const http_js_1 = require("../lib/http.js");
|
|
11
|
+
const MapShares_js_1 = require("./MapShares.js");
|
|
12
|
+
/**
|
|
13
|
+
* Utility function to create a MapServerApi instance.
|
|
14
|
+
* Only exported for unit testing purposes.
|
|
15
|
+
* @private
|
|
16
|
+
*
|
|
17
|
+
* @param opts.getBaseUrl A function that returns a promise that resolves to the base URL of the map server.
|
|
18
|
+
* @param opts.fetch An optional custom fetch function to use for making requests to the map server. If not provided, the global `fetch` will be used.
|
|
19
|
+
*/
|
|
20
|
+
function createMapServerApi({ getBaseUrl, fetch = globalThis.fetch, }) {
|
|
21
|
+
const wrappedFetch = async (input, init) => {
|
|
22
|
+
const baseUrl = await getBaseUrl();
|
|
23
|
+
return fetch(new URL(input, baseUrl).href, init);
|
|
24
|
+
};
|
|
25
|
+
const api = (0, http_js_1.createHttp)(wrappedFetch);
|
|
26
|
+
Object.defineProperty(api, 'createEventSource', {
|
|
27
|
+
value: (options) => {
|
|
28
|
+
return (0, eventsource_client_1.createEventSource)({
|
|
29
|
+
...options,
|
|
30
|
+
fetch: async (input, init) => {
|
|
31
|
+
const baseUrl = await getBaseUrl();
|
|
32
|
+
return fetch(new URL(input, baseUrl).href, init);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(api, 'getMapStyleJsonUrl', {
|
|
38
|
+
value: async (mapId) => {
|
|
39
|
+
const baseUrl = await getBaseUrl();
|
|
40
|
+
return new URL(`/maps/${mapId}/style.json`, baseUrl).href;
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
return api;
|
|
44
|
+
}
|
|
45
|
+
exports.MapServerContext = (0, react_1.createContext)(null);
|
|
46
|
+
/**
|
|
47
|
+
* Create a context provider that holds a `MapServerFetch` function, which waits
|
|
48
|
+
* for the map server to be ready before making requests.
|
|
49
|
+
*
|
|
50
|
+
* @param opts.children React children node
|
|
51
|
+
* @param opts.getBaseUrl A function that returns a promise that resolves to the base URL of the map server.
|
|
52
|
+
* @param opts.fetch An optional custom fetch function to use for making requests to the map server. If not provided, the global `fetch` will be used.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```tsx
|
|
56
|
+
* import { createServer } from '@comapeo/map-server'
|
|
57
|
+
*
|
|
58
|
+
* const server = createServer()
|
|
59
|
+
* const listenPromise = server.listen()
|
|
60
|
+
*
|
|
61
|
+
* const getBaseUrl = async () => {
|
|
62
|
+
* const { localPort } = await listenPromise
|
|
63
|
+
* return new URL(`http://localhost:${localPort}/`)
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* const mapServerApi = createMapServerApi({ getBaseUrl })
|
|
67
|
+
*
|
|
68
|
+
* function App() {
|
|
69
|
+
* return (
|
|
70
|
+
* <MapServerProvider mapServerApi={mapServerApi}>
|
|
71
|
+
* <MyApp />
|
|
72
|
+
* </MapServerProvider>
|
|
73
|
+
* )
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
function MapServerProvider({ children, getBaseUrl, fetch, queryClient, }) {
|
|
78
|
+
const clientApi = (0, client_js_1.useClientApi)();
|
|
79
|
+
const mapServerApi = (0, react_1.useMemo)(() => createMapServerApi({ getBaseUrl, fetch }), [getBaseUrl, fetch]);
|
|
80
|
+
return (0, react_1.createElement)(exports.MapServerContext.Provider, { value: mapServerApi }, (0, react_1.createElement)(MapShares_js_1.SentMapSharesProvider, { clientApi, mapServerApi }, (0, react_1.createElement)(MapShares_js_1.ReceivedMapSharesProvider, { clientApi, mapServerApi, queryClient }, children)));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Internal hook to get the MapServerApi (instance of ky) from context.
|
|
84
|
+
* Throws if used outside of MapServerProvider.
|
|
85
|
+
*/
|
|
86
|
+
function useMapServerApi() {
|
|
87
|
+
const api = (0, react_1.useContext)(exports.MapServerContext);
|
|
88
|
+
if (!api) {
|
|
89
|
+
throw new Error('useMapServerApi must be used within a MapServerProvider');
|
|
90
|
+
}
|
|
91
|
+
return api;
|
|
92
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { MapeoClientApi } from '@comapeo/ipc';
|
|
2
|
+
import type { QueryClient } from '@tanstack/react-query';
|
|
3
|
+
import { type Context, type JSX, type PropsWithChildren } from 'react';
|
|
4
|
+
import { type ReceivedMapSharesStore, type ReceivedMapShareState, type SentMapSharesStore, type SentMapShareState } from '../lib/map-shares-stores.js';
|
|
5
|
+
import type { MapServerApi } from './MapServer.js';
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare const ReceivedMapSharesContext: Context<ReceivedMapSharesStore | null>;
|
|
10
|
+
/**
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export declare const SentMapSharesContext: Context<SentMapSharesStore | null>;
|
|
14
|
+
type MapSharesProviderProps = PropsWithChildren<{
|
|
15
|
+
clientApi: MapeoClientApi;
|
|
16
|
+
mapServerApi: MapServerApi;
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare function ReceivedMapSharesProvider({ children, clientApi, mapServerApi, queryClient, }: MapSharesProviderProps & {
|
|
22
|
+
queryClient: QueryClient;
|
|
23
|
+
}): JSX.Element;
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare function SentMapSharesProvider({ children, clientApi, mapServerApi, }: MapSharesProviderProps): JSX.Element;
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare function useReceivedMapSharesActions(): {
|
|
32
|
+
download({ shareId }: import("../lib/map-shares-stores.js").DownloadMapShareOptions): Promise<void>;
|
|
33
|
+
decline({ shareId, reason }: import("../lib/map-shares-stores.js").DeclineMapShareOptions): Promise<void>;
|
|
34
|
+
abort({ shareId }: import("../lib/map-shares-stores.js").AbortMapShareOptions): Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export declare function useReceivedMapSharesState(): Array<ReceivedMapShareState>;
|
|
40
|
+
export declare function useReceivedMapSharesState<T>(selector: (state: Array<ReceivedMapShareState>) => T): T;
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export declare function useSentMapSharesActions(): {
|
|
45
|
+
createAndSend({ projectId, receiverDeviceId, mapId, }: import("../lib/map-shares-stores.js").CreateAndSendMapShareOptions): Promise<import("@comapeo/map-server", { with: { "resolution-mode": "import" } }).MapShareState>;
|
|
46
|
+
cancel({ shareId }: import("../lib/map-shares-stores.js").CancelMapShareOptions): Promise<void>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
export declare function useSentMapSharesState<T>(selector?: (state: Array<SentMapShareState>) => T): T;
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SentMapSharesContext = exports.ReceivedMapSharesContext = void 0;
|
|
4
|
+
exports.ReceivedMapSharesProvider = ReceivedMapSharesProvider;
|
|
5
|
+
exports.SentMapSharesProvider = SentMapSharesProvider;
|
|
6
|
+
exports.useReceivedMapSharesActions = useReceivedMapSharesActions;
|
|
7
|
+
exports.useReceivedMapSharesState = useReceivedMapSharesState;
|
|
8
|
+
exports.useSentMapSharesActions = useSentMapSharesActions;
|
|
9
|
+
exports.useSentMapSharesState = useSentMapSharesState;
|
|
10
|
+
const react_1 = require("react");
|
|
11
|
+
const map_shares_stores_js_1 = require("../lib/map-shares-stores.js");
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
exports.ReceivedMapSharesContext = (0, react_1.createContext)(null);
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
exports.SentMapSharesContext = (0, react_1.createContext)(null);
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
function ReceivedMapSharesProvider({ children, clientApi, mapServerApi, queryClient, }) {
|
|
24
|
+
const mapSharesStore = (0, react_1.useMemo)(() => (0, map_shares_stores_js_1.createReceivedMapSharesStore)({ clientApi, mapServerApi, queryClient }), [clientApi, mapServerApi, queryClient]);
|
|
25
|
+
return (0, react_1.createElement)(exports.ReceivedMapSharesContext.Provider, { value: mapSharesStore }, children);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
function SentMapSharesProvider({ children, clientApi, mapServerApi, }) {
|
|
31
|
+
const mapSharesStore = (0, react_1.useMemo)(() => (0, map_shares_stores_js_1.createSentMapSharesStore)({ clientApi, mapServerApi }), [clientApi, mapServerApi]);
|
|
32
|
+
return (0, react_1.createElement)(exports.SentMapSharesContext.Provider, { value: mapSharesStore }, children);
|
|
33
|
+
}
|
|
34
|
+
const identity = (arg) => arg;
|
|
35
|
+
/**
|
|
36
|
+
* Internal hook to get the MapSharesStore from context.
|
|
37
|
+
*/
|
|
38
|
+
function useMapSharesActions(context) {
|
|
39
|
+
const store = (0, react_1.useContext)(context);
|
|
40
|
+
if (!store) {
|
|
41
|
+
throw new Error('useMapSharesActions must be used within MapSharesProvider');
|
|
42
|
+
}
|
|
43
|
+
return store.actions;
|
|
44
|
+
}
|
|
45
|
+
function useMapSharesState(context, selector = identity) {
|
|
46
|
+
const store = (0, react_1.useContext)(context);
|
|
47
|
+
if (!store) {
|
|
48
|
+
throw new Error('useMapSharesState must be used within MapSharesProvider');
|
|
49
|
+
}
|
|
50
|
+
return (0, react_1.useSyncExternalStore)(store.subscribe, (0, react_1.useCallback)(
|
|
51
|
+
// Casting here because of TS not resolving generic conditional types correctly
|
|
52
|
+
() => selector(store.getSnapshot()), [selector, store]));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
function useReceivedMapSharesActions() {
|
|
58
|
+
return useMapSharesActions(exports.ReceivedMapSharesContext);
|
|
59
|
+
}
|
|
60
|
+
function useReceivedMapSharesState(selector) {
|
|
61
|
+
return useMapSharesState(exports.ReceivedMapSharesContext, selector);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
function useSentMapSharesActions() {
|
|
67
|
+
return useMapSharesActions(exports.SentMapSharesContext);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
function useSentMapSharesState(selector) {
|
|
73
|
+
return useMapSharesState(exports.SentMapSharesContext, selector);
|
|
74
|
+
}
|