Skip to content

Commit

Permalink
feat: introduce some write hooks for client, invites, and projects (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 authored Jan 8, 2025
1 parent 71647d6 commit 622e278
Show file tree
Hide file tree
Showing 9 changed files with 701 additions and 13 deletions.
137 changes: 134 additions & 3 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- [useClientApi](#useclientapi)
- [useOwnDeviceInfo](#useowndeviceinfo)
- [useIsArchiveDevice](#useisarchivedevice)
- [useSetOwnDeviceInfo](#usesetowndeviceinfo)
- [useSetIsArchiveDevice](#usesetisarchivedevice)
- [useProjectSettings](#useprojectsettings)
- [useSingleProject](#usesingleproject)
- [useManyProjects](#usemanyprojects)
Expand All @@ -12,9 +14,19 @@
- [useIconUrl](#useiconurl)
- [useAttachmentUrl](#useattachmenturl)
- [useDocumentCreatedBy](#usedocumentcreatedby)
- [useAddServerPeer](#useaddserverpeer)
- [useCreateProject](#usecreateproject)
- [useLeaveProject](#useleaveproject)
- [useImportProjectConfig](#useimportprojectconfig)
- [useUpdateProjectSettings](#useupdateprojectsettings)
- [useCreateBlob](#usecreateblob)
- [useSingleDocByDocId](#usesingledocbydocid)
- [useSingleDocByVersionId](#usesingledocbyversionid)
- [useManyDocs](#usemanydocs)
- [useAcceptInvite](#useacceptinvite)
- [useRejectInvite](#userejectinvite)
- [useSendInvite](#usesendinvite)
- [useRequestCancelInvite](#userequestcancelinvite)
- [useMapStyleUrl](#usemapstyleurl)

### ClientApiProvider
Expand Down Expand Up @@ -94,6 +106,22 @@ function IsArchiveDeviceExample() {
```


### useSetOwnDeviceInfo

Update the device info for the current device.

| Function | Type |
| ---------- | ---------- |
| `useSetOwnDeviceInfo` | `() => { mutate: UseMutateFunction<void, Error, { name: string; deviceType: "device_type_unspecified" or "mobile" or "tablet" or "desktop" or "selfHostedServer" or "UNRECOGNIZED"; }, unknown>; reset: () => void; status: "pending" or ... 2 more ... or "idle"; }` |

### useSetIsArchiveDevice

Set or unset the current device as an archive device.

| Function | Type |
| ---------- | ---------- |
| `useSetIsArchiveDevice` | `() => { mutate: UseMutateFunction<void, Error, { isArchiveDevice: boolean; }, unknown>; reset: () => void; status: "pending" or "error" or "success" or "idle"; }` |

### useProjectSettings

Retrieve the project settings for a project.
Expand Down Expand Up @@ -342,6 +370,67 @@ function BasicExample() {
```


### useAddServerPeer

| Function | Type |
| ---------- | ---------- |
| `useAddServerPeer` | `({ projectId }: { projectId: string; }) => { mutate: UseMutateFunction<void, Error, { baseUrl: string; dangerouslyAllowInsecureConnections?: boolean or undefined; }, unknown>; reset: () => void; status: "pending" or ... 2 more ... or "idle"; }` |

### useCreateProject

Create a new project.

| Function | Type |
| ---------- | ---------- |
| `useCreateProject` | `() => { mutate: UseMutateFunction<string, Error, { name?: string or undefined; configPath?: string or undefined; } or undefined, unknown>; reset: () => void; status: "pending" or ... 2 more ... or "idle"; }` |

### useLeaveProject

Leave an existing project.

| Function | Type |
| ---------- | ---------- |
| `useLeaveProject` | `() => { mutate: UseMutateFunction<void, Error, { projectId: string; }, unknown>; reset: () => void; status: "pending" or "error" or "success" or "idle"; }` |

### useImportProjectConfig

Update the configuration of a project using an external file.

| Function | Type |
| ---------- | ---------- |
| `useImportProjectConfig` | `({ projectId }: { projectId: string; }) => { mutate: UseMutateFunction<Error[], Error, { configPath: string; }, unknown>; reset: () => void; status: "pending" or "error" or "success" or "idle"; }` |

Parameters:

* `opts.projectId`: Public ID of the project to apply changes to.


### useUpdateProjectSettings

Update the settings of a project.

| Function | Type |
| ---------- | ---------- |
| `useUpdateProjectSettings` | `({ projectId }: { projectId: string; }) => { mutate: UseMutateFunction<EditableProjectSettings, Error, { name?: string or undefined; configMetadata?: { name: string; buildDate: string; importDate: string; fileVersion: string; } or undefined; defaultPresets?: { ...; } or undefined; }, unknown>; reset: () => void; status...` |

Parameters:

* `opts.projectId`: Public ID of the project to apply changes to.


### useCreateBlob

Create a blob for a project.

| Function | Type |
| ---------- | ---------- |
| `useCreateBlob` | `({ projectId }: { projectId: string; }) => { mutate: UseMutateFunction<{ driveId: string; name: string; type: "audio" or "video" or "photo"; hash: string; }, Error, { original: string; preview?: string or undefined; thumbnail?: string or undefined; metadata: Metadata; }, unknown>; reset: () => void; status: "pending" or ...` |

Parameters:

* `opts.projectId`: Public project ID of project to apply to changes to.


### useSingleDocByDocId

Retrieve a single document from the database based on the document's document ID.
Expand All @@ -350,7 +439,7 @@ Triggers the closest error boundary if the document cannot be found

| Function | Type |
| ---------- | ---------- |
| `useSingleDocByDocId` | `<D extends DocumentType>({ projectId, docType, docId, lang, }: { projectId: string; docType: D; docId: string; lang?: string or undefined; }) => ReadHookResult<Extract<{ schemaName: "translation"; docRef: { docId: string; versionId: string; }; docRefType: "track" or ... 7 more ... or "type_unspecified"; ... 10 more ......` |
| `useSingleDocByDocId` | `<D extends DocumentType>({ projectId, docType, docId, lang, }: { projectId: string; docType: D; docId: string; lang?: string or undefined; }) => ReadHookResult<Extract<{ schemaName: "deviceInfo"; name: string; deviceType: "device_type_unspecified" or ... 4 more ... or "UNRECOGNIZED"; ... 7 more ...; deleted: boolean; }...` |

Parameters:

Expand Down Expand Up @@ -383,7 +472,7 @@ Triggers the closest error boundary if the document cannot be found.

| Function | Type |
| ---------- | ---------- |
| `useSingleDocByVersionId` | `<D extends DocumentType>({ projectId, docType, versionId, lang, }: { projectId: string; docType: D; versionId: string; lang?: string or undefined; }) => ReadHookResult<Extract<{ schemaName: "translation"; docRef: { docId: string; versionId: string; }; docRefType: "track" or ... 7 more ... or "type_unspecified"; ... 10 ...` |
| `useSingleDocByVersionId` | `<D extends DocumentType>({ projectId, docType, versionId, lang, }: { projectId: string; docType: D; versionId: string; lang?: string or undefined; }) => ReadHookResult<Extract<{ schemaName: "deviceInfo"; name: string; deviceType: "device_type_unspecified" or ... 4 more ... or "UNRECOGNIZED"; ... 7 more ...; deleted: bo...` |

Parameters:

Expand Down Expand Up @@ -416,7 +505,7 @@ Retrieve all documents of a specific `docType`.

| Function | Type |
| ---------- | ---------- |
| `useManyDocs` | `<D extends DocumentType>({ projectId, docType, includeDeleted, lang, }: { projectId: string; docType: D; includeDeleted?: boolean or undefined; lang?: string or undefined; }) => ReadHookResult<Extract<{ schemaName: "translation"; docRef: { docId: string; versionId: string; }; ... 11 more ...; deleted: boolean; }, { .....` |
| `useManyDocs` | `<D extends DocumentType>({ projectId, docType, includeDeleted, lang, }: { projectId: string; docType: D; includeDeleted?: boolean or undefined; lang?: string or undefined; }) => ReadHookResult<Extract<{ schemaName: "deviceInfo"; name: string; deviceType: "device_type_unspecified" or ... 4 more ... or "UNRECOGNIZED"; ......` |

Parameters:

Expand Down Expand Up @@ -454,6 +543,48 @@ function useAllPresets(opts) {
```


### useAcceptInvite

Accept an invite that has been received.

| Function | Type |
| ---------- | ---------- |
| `useAcceptInvite` | `() => { mutate: UseMutateFunction<string, Error, { inviteId: string; }, unknown>; reset: () => void; status: "pending" or "error" or "success" or "idle"; }` |

### useRejectInvite

Reject an invite that has been received.

| Function | Type |
| ---------- | ---------- |
| `useRejectInvite` | `() => { mutate: UseMutateFunction<void, Error, { inviteId: string; }, unknown>; reset: () => void; status: "pending" or "error" or "success" or "idle"; }` |

### useSendInvite

Send an invite for a project.

| Function | Type |
| ---------- | ---------- |
| `useSendInvite` | `({ projectId }: { projectId: string; }) => { mutate: UseMutateFunction<"ACCEPT" or "REJECT" or "ALREADY", Error, { deviceId: string; roleDescription?: string or undefined; roleId: "f7c150f5a3a9a855" or "012fd2d431c0bf60" or "9e6d29263cba36c9"; roleName?: string or undefined; }, unknown>; reset: () => void; status: "pendin...` |

Parameters:

* `opts.projectId`: Public ID of project to send the invite on behalf of.


### useRequestCancelInvite

Request a cancellation of an invite sent to another device.

| Function | Type |
| ---------- | ---------- |
| `useRequestCancelInvite` | `({ projectId }: { projectId: string; }) => { mutate: UseMutateFunction<void, Error, { deviceId: string; }, unknown>; reset: () => void; status: "pending" or "error" or "success" or "idle"; }` |

Parameters:

* `opts.projectId`: Public ID of project to request the invite cancellation for.


### useMapStyleUrl

Get a URL that points to a StyleJSON resource served by the embedded HTTP server.
Expand Down
36 changes: 35 additions & 1 deletion src/hooks/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { useSuspenseQuery } from '@tanstack/react-query'
import {
useMutation,
useQueryClient,
useSuspenseQuery,
} from '@tanstack/react-query'
import { useContext } from 'react'

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

/**
Expand Down Expand Up @@ -83,3 +89,31 @@ export function useIsArchiveDevice() {

return { data, error, isRefetching }
}

/**
* Update the device info for the current device.
*/
export function useSetOwnDeviceInfo() {
const queryClient = useQueryClient()
const clientApi = useClientApi()

const { mutate, status, reset } = useMutation(
setOwnDeviceInfoMutationOptions({ clientApi, queryClient }),
)

return { mutate, reset, status }
}

/**
* Set or unset the current device as an archive device.
*/
export function useSetIsArchiveDevice() {
const queryClient = useQueryClient()
const clientApi = useClientApi()

const { mutate, status, reset } = useMutation(
setIsArchiveDeviceMutationOptions({ clientApi, queryClient }),
)

return { mutate, reset, status }
}
70 changes: 70 additions & 0 deletions src/hooks/invites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'

import {
acceptInviteMutationOptions,
rejectInviteMutationOptions,
requestCancelInviteMutationOptions,
sendInviteMutationOptions,
} from '../lib/react-query/invites.js'
import { useClientApi } from './client.js'
import { useSingleProject } from './projects.js'

/**
* Accept an invite that has been received.
*/
export function useAcceptInvite() {
const queryClient = useQueryClient()
const clientApi = useClientApi()

const { mutate, status, reset } = useMutation(
acceptInviteMutationOptions({ clientApi, queryClient }),
)

return { mutate, reset, status }
}

/**
* Reject an invite that has been received.
*/
export function useRejectInvite() {
const queryClient = useQueryClient()
const clientApi = useClientApi()

const { mutate, status, reset } = useMutation(
rejectInviteMutationOptions({ clientApi, queryClient }),
)

return { mutate, reset, status }
}

/**
* Send an invite for a project.
*
* @param opts.projectId Public ID of project to send the invite on behalf of.
*/
export function useSendInvite({ projectId }: { projectId: string }) {
const queryClient = useQueryClient()
const { data: projectApi } = useSingleProject({ projectId })

const { mutate, status, reset } = useMutation(
sendInviteMutationOptions({ projectApi, projectId, queryClient }),
)

return { mutate, reset, status }
}

/**
* Request a cancellation of an invite sent to another device.
*
* @param opts.projectId Public ID of project to request the invite cancellation for.
*/
export function useRequestCancelInvite({ projectId }: { projectId: string }) {
const queryClient = useQueryClient()
const { data: projectApi } = useSingleProject({ projectId })

const { mutate, status, reset } = useMutation(
requestCancelInviteMutationOptions({ projectApi, queryClient }),
)

return { mutate, reset, status }
}
Loading

0 comments on commit 622e278

Please sign in to comment.