Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1153,9 +1153,39 @@ function MapImportExample() {

### useGetCustomMapInfo

Get the map info for the custom map.

Note that this is _not_ a suspenseful hook. The different read states should be explicitly handled at usage.

| Function | Type |
| ---------- | ---------- |
| `useGetCustomMapInfo` | `() => { data: any; error: Error or null; isRefetching: boolean; }` |
| `useGetCustomMapInfo` | `() => Pick<QueryObserverRefetchErrorResult<MapInfoResponse, Error>, "data" or "error" or "status" or "isRefetching"> or Pick<QueryObserverSuccessResult<MapInfoResponse, Error>, "data" or ... 2 more ... or "isRefetching"> or Pick<...> or Pick<...> or Pick<...> or Pick<...>` |

Examples:

```tsx
import { getErrorCode } from '@comapeo/core-react'

function Example() {
const mapInfo = useGetCustomMapInfo()

if (mapInfo.status === 'pending') {
// handle pending...
}

if (mapInfo.status === 'error') {
if (getErrorCode(mapInfo.error) === 'MAP_NOT_FOUND') {
// handle not found error...
} else {
// handle all other errors...
}
}

// Handle success state...
console.log(mapInfo.data)
}
```


### useManyReceivedMapShares

Expand Down
35 changes: 33 additions & 2 deletions src/hooks/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
baseMutationOptions,
baseQueryOptions,
filterMutationResult,
filterQueryResult,
getMapInfoQueryKey,
getStyleJsonUrlQueryKey,
invalidateMapQueries,
Expand Down Expand Up @@ -138,13 +139,43 @@ export function useRemoveCustomMapFile() {
)
}

/**
* Get the map info for the custom map.
*
* Note that this is _not_ a suspenseful hook. The different read states should be explicitly handled at usage.
*
* @example
* ```tsx
* import { getErrorCode } from '@comapeo/core-react'
*
* function Example() {
* const mapInfo = useGetCustomMapInfo()
*
* if (mapInfo.status === 'pending') {
* // handle pending...
* }
*
* if (mapInfo.status === 'error') {
* if (getErrorCode(mapInfo.error) === 'MAP_NOT_FOUND') {
* // handle not found error...
* } else {
* // handle all other errors...
* }
* }
*
* // Handle success state...
* console.log(mapInfo.data)
* }
* ```
*
*/
export function useGetCustomMapInfo() {
const mapServerApi = useMapServerApi()

// TODO: Support custom maps
const mapId = CUSTOM_MAP_ID

const { data, error, isRefetching } = useQuery({
const result = useQuery({
...baseQueryOptions(),
queryKey: getMapInfoQueryKey({ mapId }),
queryFn: async () => {
Expand All @@ -155,7 +186,7 @@ export function useGetCustomMapInfo() {
gcTime: Infinity,
})

return { data, error, isRefetching }
return filterQueryResult(result)
}

// ============================================
Expand Down
24 changes: 24 additions & 0 deletions src/lib/react-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
QueryOptions,
UseMutationOptions,
UseMutationResult,
UseQueryResult,
} from '@tanstack/react-query'
import { DistributedPick } from 'type-fest'

Expand Down Expand Up @@ -61,6 +62,29 @@ export function filterMutationResult<
return filteredResult
}

const PICKED_QUERY_RESULT_KEYS = [
'data',
'error',
'isRefetching',
'status',
] as const satisfies ReadonlyArray<keyof UseQueryResult>

export type FilteredQueryResult<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
TResult extends UseQueryResult<any, any>,
> = DistributedPick<TResult, (typeof PICKED_QUERY_RESULT_KEYS)[number]>

export function filterQueryResult<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
TResult extends UseQueryResult<any, any>,
>(queryResult: TResult) {
const filteredResult = {} as FilteredQueryResult<TResult>
for (const key of PICKED_QUERY_RESULT_KEYS) {
filteredResult[key] = queryResult[key]
}
return filteredResult
}

// #endregion

// #region Client
Expand Down
Loading