Skip to content

Commit

Permalink
add useIsArchiveDevice() hook
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Dec 12, 2024
1 parent 1052291 commit a79a239
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
32 changes: 32 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
- [baseQueryOptions](#basequeryoptions)
- [getClientQueryKey](#getclientquerykey)
- [getDeviceInfoQueryKey](#getdeviceinfoquerykey)
- [getIsArchiveDeviceQueryKey](#getisarchivedevicequerykey)
- [deviceInfoQueryOptions](#deviceinfoqueryoptions)
- [isArchiveDeviceQueryOptions](#isarchivedevicequeryoptions)
- [useClientApi](#useclientapi)
- [useOwnDeviceInfo](#useowndeviceinfo)
- [useIsArchiveDevice](#useisarchivedevice)
- [getDocumentsQueryKey](#getdocumentsquerykey)
- [getManyDocumentsQueryKey](#getmanydocumentsquerykey)
- [getDocumentByDocIdQueryKey](#getdocumentbydocidquerykey)
Expand Down Expand Up @@ -83,12 +86,24 @@ Parameters:
| ---------- | ---------- |
| `getDeviceInfoQueryKey` | `() => readonly ["@comapeo/core-react", "client", "device_info"]` |

### getIsArchiveDeviceQueryKey

| Function | Type |
| ---------- | ---------- |
| `getIsArchiveDeviceQueryKey` | `() => readonly ["@comapeo/core-react", "client", "is_remote_archive"]` |

### deviceInfoQueryOptions

| Function | Type |
| ---------- | ---------- |
| `deviceInfoQueryOptions` | `({ clientApi, }: { clientApi: MapeoClientApi; }) => OmitKeyof<UseQueryOptions<{ deviceId: string; deviceType: "device_type_unspecified" or "mobile" or "tablet" or "desktop" or "selfHostedServer" or "UNRECOGNIZED"; } and Partial<...>, Error, { ...; } and Partial<...>, QueryKey>, "queryFn"> and { ...; } and { ...; }` |

### isArchiveDeviceQueryOptions

| Function | Type |
| ---------- | ---------- |
| `isArchiveDeviceQueryOptions` | `({ clientApi, }: { clientApi: MapeoClientApi; }) => OmitKeyof<UseQueryOptions<boolean, Error, boolean, QueryKey>, "queryFn"> and { ...; } and { ...; }` |

### useClientApi

Access a client API instance. If a ClientApiContext provider is not
Expand Down Expand Up @@ -135,6 +150,23 @@ function DeviceInfoExample() {
```


### useIsArchiveDevice

Retrieve whether the current device is an archive device or not.

| Function | Type |
| ---------- | ---------- |
| `useIsArchiveDevice` | `() => { data: boolean; error: Error or null; isRefetching: boolean; }` |

Examples:

```tsx
function IsArchiveDeviceExample() {
const { data } = useIsArchiveDevice()
}
```


### getDocumentsQueryKey

| Function | Type |
Expand Down
26 changes: 24 additions & 2 deletions src/hooks/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { useSuspenseQuery } from '@tanstack/react-query'
import { useContext } from 'react'

import { ClientApiContext } from '../contexts/ClientApi'
import { deviceInfoQueryOptions } from '../lib/react-query/client'
import {
deviceInfoQueryOptions,
isArchiveDeviceQueryOptions,
} from '../lib/react-query/client'

/**
* Access a client API instance. If a ClientApiContext provider is not
Expand Down Expand Up @@ -50,7 +53,6 @@ export function useClientApi() {
* const { data } = useDeviceInfo()
* }
* ```
*
*/
export function useOwnDeviceInfo() {
const clientApi = useClientApi()
Expand All @@ -61,3 +63,23 @@ export function useOwnDeviceInfo() {

return { data, error, isRefetching }
}

/**
* Retrieve whether the current device is an archive device or not.
*
* @example
* ```tsx
* function IsArchiveDeviceExample() {
* const { data } = useIsArchiveDevice()
* }
* ```
*/
export function useIsArchiveDevice() {
const clientApi = useClientApi()

const { data, error, isRefetching } = useSuspenseQuery(
isArchiveDeviceQueryOptions({ clientApi }),
)

return { data, error, isRefetching }
}
18 changes: 18 additions & 0 deletions src/lib/react-query/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function getDeviceInfoQueryKey() {
return [ROOT_QUERY_KEY, 'client', 'device_info'] as const
}

export function getIsArchiveDeviceQueryKey() {
return [ROOT_QUERY_KEY, 'client', 'is_remote_archive'] as const
}

export function deviceInfoQueryOptions({
clientApi,
}: {
Expand All @@ -24,3 +28,17 @@ export function deviceInfoQueryOptions({
},
})
}

export function isArchiveDeviceQueryOptions({
clientApi,
}: {
clientApi: MapeoClientApi
}) {
return queryOptions({
...baseQueryOptions(),
queryKey: getIsArchiveDeviceQueryKey(),
queryFn: async () => {
return clientApi.getIsArchiveDevice()
},
})
}

0 comments on commit a79a239

Please sign in to comment.