-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add stubbed map sharing API #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0825b6b
26b4e28
7ee3fbd
69a1cea
a024869
1af841d
371e486
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,111 @@ | ||
| import type { MapeoClientApi } from '@comapeo/ipc' with { 'resolution-mode': 'import' } | ||
| import { queryOptions } from '@tanstack/react-query' | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
| import type { | ||
| MapeoClientApi, | ||
| MapeoProjectApi, | ||
| } from '@comapeo/ipc' with { 'resolution-mode': 'import' } | ||
| import { | ||
| queryOptions, | ||
| type QueryClient, | ||
| type UseMutationOptions, | ||
| } from '@tanstack/react-query' | ||
|
|
||
| import { baseQueryOptions, ROOT_QUERY_KEY } from './shared.js' | ||
| import { | ||
| baseMutationOptions, | ||
| baseQueryOptions, | ||
| ROOT_QUERY_KEY, | ||
| } from './shared.js' | ||
|
|
||
| type MapShareState = | ||
| /** Map share has been received and is awaiting a response */ | ||
| | 'pending' | ||
| /** Map share has been rejected */ | ||
| | 'rejected' | ||
| /** Map share is currently being downloaded */ | ||
| | 'downloading' | ||
| /** Map share has been cancelled by the sharer */ | ||
| | 'cancelled' | ||
| /** Map has been downloaded */ | ||
| | 'completed' | ||
| /** An error occurred while receiving the map share */ | ||
| | 'error' | ||
|
|
||
| type MapShareBase = { | ||
| /** The ID of the device that sent the map share */ | ||
| senderDeviceId: string | ||
| /** The name of the device that sent the map share */ | ||
| senderDeviceName: string | ||
| /** The ID of the map share */ | ||
| shareId: string | ||
| /** The name of the map being shared */ | ||
| mapName: string | ||
| /** The ID of the map being shared */ | ||
| mapId: string | ||
| /** The timestamp when the map share invite was received */ | ||
| receivedAt: number | ||
| /** The bounding box of the map data being shared */ | ||
| bounds: [number, number, number, number] | ||
| /** The minimum zoom level of the map data being shared */ | ||
| minzoom: number | ||
| /** The maximum zoom level of the map data being shared */ | ||
| maxzoom: number | ||
| /** Estimated size of the map data being shared in bytes */ | ||
| estimatedSizeBytes: number | ||
| } | ||
|
|
||
| type MapShareResponse = | ||
| | { | ||
| decision: 'ACCEPT' | 'UNRECOGNIZED' | ||
| shareId: string | ||
| } | ||
| | { | ||
| decision: 'REJECT' | ||
| shareId: string | ||
| reason: 'DISK_SPACE' | 'USER_REJECTED' | 'ALREADY' | 'UNRECOGNIZED' | ||
| } | ||
|
|
||
| type MapShare = MapShareBase & | ||
| ( | ||
| | { | ||
| state: Exclude<MapShareState, 'downloading' | 'error'> | ||
| } | ||
| | { | ||
| state: 'downloading' | ||
| /** Total bytes downloaded so far (compare with estimatedSizeBytes for progress) */ | ||
| bytesDownloaded: number | ||
| } | ||
| | { | ||
| state: 'error' | ||
| /** Error that occurred while receiving the map share */ | ||
| error: Error | ||
| } | ||
| ) | ||
|
|
||
| const MOCK_MAP_SHARE = { | ||
| senderDeviceId: 'device-123', | ||
| senderDeviceName: 'Device 123', | ||
| shareId: 'share-456', | ||
| mapName: 'Sample Map', | ||
| mapId: 'map-789', | ||
| receivedAt: Date.now(), | ||
| bounds: [0, 0, 10, 10], | ||
| minzoom: 0, | ||
| maxzoom: 14, | ||
| estimatedSizeBytes: 1024 * 1024, | ||
| state: 'pending' as const, | ||
| } satisfies MapShare | ||
|
|
||
| export function getMapsQueryKey() { | ||
| return [ROOT_QUERY_KEY, 'maps'] as const | ||
| } | ||
|
|
||
| export function getMapSharesQueryKey() { | ||
| return [ROOT_QUERY_KEY, 'maps', 'shares'] as const | ||
| } | ||
|
|
||
| export function getMapSharesByIdQueryKey({ shareId }: { shareId: string }) { | ||
| return [ROOT_QUERY_KEY, 'maps', 'shares', shareId] as const | ||
| } | ||
|
|
||
| export function getStyleJsonUrlQueryKey({ | ||
| refreshToken, | ||
| }: { | ||
|
|
@@ -36,3 +135,125 @@ export function mapStyleJsonUrlQueryOptions({ | |
| }, | ||
| }) | ||
| } | ||
|
|
||
| export function getMapSharesQueryOptions({ | ||
| clientApi, | ||
| }: { | ||
| clientApi: MapeoClientApi | ||
| }) { | ||
| return queryOptions({ | ||
| ...baseQueryOptions(), | ||
| queryKey: getMapSharesQueryKey(), | ||
| queryFn: async (): Promise<Array<MapShare>> => { | ||
| return [MOCK_MAP_SHARE] | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| export function getMapShareByIdQueryOptions({ | ||
| clientApi, | ||
| shareId, | ||
| }: { | ||
| clientApi: MapeoClientApi | ||
| shareId: string | ||
| }) { | ||
| return queryOptions({ | ||
| ...baseQueryOptions(), | ||
| queryKey: getMapSharesByIdQueryKey({ shareId }), | ||
| queryFn: async (): Promise<MapShare> => { | ||
| return MOCK_MAP_SHARE | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| export function acceptMapShareMutationOptions({ | ||
| clientApi, | ||
| queryClient, | ||
| }: { | ||
| clientApi: MapeoClientApi | ||
| queryClient: QueryClient | ||
| }) { | ||
| return { | ||
| ...baseMutationOptions(), | ||
| mutationFn: async ({ shareId }) => { | ||
| await new Promise((res) => setTimeout(res, 1000)) | ||
| console.log('Accepted map share', shareId) | ||
| return shareId | ||
| }, | ||
| } satisfies UseMutationOptions<string, Error, { shareId: string }> | ||
| } | ||
|
|
||
| export function rejectMapShareMutationOptions({ | ||
| clientApi, | ||
| queryClient, | ||
| }: { | ||
| clientApi: MapeoClientApi | ||
| queryClient: QueryClient | ||
| }) { | ||
| return { | ||
| ...baseMutationOptions(), | ||
| mutationFn: async ({ shareId }) => { | ||
| await new Promise((res) => setTimeout(res, 1000)) | ||
| console.log('Rejected map share', shareId) | ||
| }, | ||
| } satisfies UseMutationOptions<void, Error, { shareId: string }> | ||
| } | ||
|
|
||
| export function sendMapShareMutationOptions({ | ||
| projectApi, | ||
| projectId, | ||
| queryClient, | ||
| }: { | ||
| projectApi: MapeoProjectApi | ||
| projectId: string | ||
| queryClient: QueryClient | ||
| }) { | ||
| return { | ||
| ...baseMutationOptions(), | ||
| mutationFn: async ({ deviceId, mapId }) => { | ||
| await new Promise((res) => setTimeout(res, 5000)) | ||
| console.log( | ||
| `Sent map share for map ${mapId} to device ${deviceId} on project ${projectId}`, | ||
| ) | ||
| const outcomes: Array<MapShareResponse> = [ | ||
| { decision: 'ACCEPT', shareId: 'share-1' }, | ||
| { decision: 'REJECT', shareId: 'share-2', reason: 'DISK_SPACE' }, | ||
| { decision: 'REJECT', shareId: 'share-3', reason: 'USER_REJECTED' }, | ||
| ] | ||
| return ( | ||
| outcomes[Math.floor(Math.random() * outcomes.length)] || { | ||
| decision: 'ACCEPT', | ||
| shareId: 'share-1', | ||
| } | ||
| ) | ||
| }, | ||
| } satisfies UseMutationOptions< | ||
| MapShareResponse, | ||
| Error, | ||
| { | ||
| deviceId: string | ||
| mapId: string | ||
| } | ||
| > | ||
| } | ||
|
|
||
| export function requestCancelMapShareMutationOptions({ | ||
| projectApi, | ||
| queryClient, | ||
| }: { | ||
| projectApi: MapeoProjectApi | ||
| queryClient: QueryClient | ||
| }) { | ||
| return { | ||
| ...baseMutationOptions(), | ||
| mutationFn: async ({ shareId }) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there could be a parameter mismatch here? projectId? shareId? or like in invites, deviceId? Which should it be?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes good question, I'll think about this more tomorrow.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I think it's possible to implement either
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I'm further into the implementation I have an answer for this. The code is only going to allow one download per map share (although retries will be allowed in the case of failure). The cancel on the sender side will be with the |
||
| await new Promise((res) => setTimeout(res, 1000)) | ||
| console.log('Requested cancellation of map share', shareId) | ||
| }, | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ | ||
| queryKey: getMapSharesQueryKey(), | ||
| }) | ||
| }, | ||
| } satisfies UseMutationOptions<void, Error, { shareId: string }> | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.