-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
165 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { useQueryClient } from '@tanstack/react-query' | ||
|
||
import { | ||
getClientQueryKey, | ||
getDeviceInfoQueryKey, | ||
getIsArchiveDeviceQueryKey, | ||
} from '../lib/react-query/client.js' | ||
import { | ||
getDocumentByDocIdQueryKey, | ||
getDocumentByVersionIdQueryKey, | ||
getDocumentsQueryKey, | ||
getManyDocumentsQueryKey, | ||
} from '../lib/react-query/documents.js' | ||
import { | ||
getInvitesQueryKey, | ||
getPendingInvitesQueryKey, | ||
} from '../lib/react-query/invites.js' | ||
import { | ||
getMapsQueryKey, | ||
getStyleJsonUrlQueryKey, | ||
} from '../lib/react-query/maps.js' | ||
import { | ||
getAttachmentUrlQueryKey, | ||
getDocumentCreatedByQueryKey, | ||
getIconUrlQueryKey, | ||
getMemberByIdQueryKey, | ||
getMembersQueryKey, | ||
getProjectByIdQueryKey, | ||
getProjectRoleQueryKey, | ||
getProjectSettingsQueryKey, | ||
getProjectsQueryKey, | ||
} from '../lib/react-query/projects.js' | ||
import { ROOT_QUERY_KEY } from '../lib/react-query/shared.js' | ||
|
||
const PATH_TO_QUERY_KEY_FACTORY = { | ||
'/': () => ROOT_QUERY_KEY, | ||
'/maps': getMapsQueryKey, | ||
'/maps/stylejson_url': getStyleJsonUrlQueryKey, | ||
'/invites': getInvitesQueryKey, | ||
'/invites/pending': getPendingInvitesQueryKey, | ||
'/client': getClientQueryKey, | ||
'/client/device_info': getDeviceInfoQueryKey, | ||
'/client/is_archive_device': getIsArchiveDeviceQueryKey, | ||
'/projects': getProjectsQueryKey, | ||
'/projects/:projectId': getProjectByIdQueryKey, | ||
'/projects/:projectId/settings': getProjectSettingsQueryKey, | ||
'/projects/:projectId/role': getProjectRoleQueryKey, | ||
'/projects/:projectId/members': getMembersQueryKey, | ||
'/projects/:projectId/members/:deviceId': getMemberByIdQueryKey, | ||
'/projects/:projectId/icons/:iconId': getIconUrlQueryKey, | ||
'/projects/:projectId/document_created_by/:originalVersionId': | ||
getDocumentCreatedByQueryKey, | ||
'/projects/:project_id/attachments/:blobId': getAttachmentUrlQueryKey, | ||
'/projects/:project_id/:doc_type': ( | ||
opts: Parameters< | ||
typeof getDocumentsQueryKey | typeof getManyDocumentsQueryKey | ||
>[0], | ||
) => { | ||
if ('lang' in opts || 'includeDeleted' in opts) { | ||
return getManyDocumentsQueryKey(opts) | ||
} | ||
return getDocumentsQueryKey(opts) | ||
}, | ||
'/projects/:project_id/:doc_type/:docId': getDocumentByDocIdQueryKey, | ||
'/projects/:project_id/:doc_type/:versionId': getDocumentByVersionIdQueryKey, | ||
} as const | ||
|
||
/** | ||
* Provides a function that invalidates a specified read when called. | ||
* | ||
* @example | ||
* ```tsx | ||
* function App() { | ||
* const invalidateRead = useInvalidateRead() | ||
* | ||
* return ( | ||
* <button | ||
* onClick={() => { | ||
* // Invalidate all reads! | ||
* invalidateRead('/', undefined) | ||
* }} | ||
* > | ||
* Invalidate everything! | ||
* </button> | ||
* ) | ||
* } | ||
* ``` | ||
*/ | ||
export function useInvalidateRead(): < | ||
Path extends keyof typeof PATH_TO_QUERY_KEY_FACTORY, | ||
>( | ||
path: Path, | ||
params: Parameters<(typeof PATH_TO_QUERY_KEY_FACTORY)[Path]>[0], | ||
invalidationOpts?: { | ||
exact: boolean | ||
}, | ||
) => Promise<void> { | ||
const queryClient = useQueryClient() | ||
|
||
return (path, pathParams, invalidationOpts) => { | ||
return queryClient.invalidateQueries({ | ||
exact: invalidationOpts?.exact, | ||
queryKey: PATH_TO_QUERY_KEY_FACTORY[path]( | ||
// @ts-expect-error TS not capable enough to get this right | ||
pathParams, | ||
), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { | ||
MapeoDoc, | ||
MapeoValue, | ||
} from '@comapeo/schema' with { 'resolution-mode': 'import' } | ||
|
||
export type WriteableDocumentType = Extract< | ||
MapeoDoc['schemaName'], | ||
'field' | 'observation' | 'preset' | 'track' | 'remoteDetectionAlert' | ||
> | ||
export type WriteableValue<D extends WriteableDocumentType> = Extract< | ||
MapeoValue, | ||
{ schemaName: D } | ||
> | ||
export type WriteableDocument<D extends WriteableDocumentType> = Extract< | ||
MapeoDoc, | ||
{ schemaName: D } | ||
> | ||
|
||
export {} |