Skip to content
Draft
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
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@
"typescript": "5.8.3",
"typescript-eslint": "8.34.1",
"vitest": "3.2.3"
},
"dependencies": {
"type-fest": "^4.41.0"
}
}
11 changes: 5 additions & 6 deletions src/hooks/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
import { useContext } from 'react'

import { ClientApiContext } from '../contexts/ClientApi.js'
import { EXPOSED_MUTATION_PROPS } from '../lib/constants.js'
import { pick } from '../lib/pick.js'
import {
deviceInfoQueryOptions,
isArchiveDeviceQueryOptions,
Expand Down Expand Up @@ -114,11 +116,8 @@ export function useSetIsArchiveDevice() {
const queryClient = useQueryClient()
const clientApi = useClientApi()

const { error, mutate, mutateAsync, status, reset } = useMutation(
setIsArchiveDeviceMutationOptions({ clientApi, queryClient }),
return pick(
useMutation(setIsArchiveDeviceMutationOptions({ clientApi, queryClient })),
EXPOSED_MUTATION_PROPS,
)

return status === 'error'
? { error, mutate, mutateAsync, reset, status }
: { error: null, mutate, mutateAsync, reset, status }
}
13 changes: 7 additions & 6 deletions src/hooks/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from '@tanstack/react-query'
import { useSyncExternalStore } from 'react'

import { EXPOSED_MUTATION_PROPS } from '../lib/constants.js'
import { pick } from '../lib/pick.js'
import {
addServerPeerMutationOptions,
connectSyncServersMutationOptions,
Expand Down Expand Up @@ -398,13 +400,12 @@ export function useAddServerPeer({ projectId }: { projectId: string }) {
const queryClient = useQueryClient()
const { data: projectApi } = useSingleProject({ projectId })

const { error, mutate, mutateAsync, reset, status } = useMutation(
addServerPeerMutationOptions({ projectApi, projectId, queryClient }),
return pick(
useMutation(
addServerPeerMutationOptions({ projectApi, projectId, queryClient }),
),
EXPOSED_MUTATION_PROPS,
)

return status === 'error'
? { error, mutate, mutateAsync, reset, status }
: { error: null, mutate, mutateAsync, reset, status }
}

export function useRemoveServerPeer({ projectId }: { projectId: string }) {
Expand Down
7 changes: 7 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const EXPOSED_MUTATION_PROPS = [
'error',
'mutate',
'mutateAsync',
'reset',
'status',
] as const
28 changes: 28 additions & 0 deletions src/lib/pick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Vendored from https://github.com/sindresorhus/filter-obj/blob/7bcfe43cb7bfa8fd553110e0a04043e73f5e78f9/index.js
// With distributive pick support from https://github.com/sindresorhus/filter-obj/pull/39

import type { Simplify } from 'type-fest'

type DistributivePick<Value, Key extends keyof Value> = Value extends unknown
? Pick<Value, Key>
: never

export function pick<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ObjectType extends Record<PropertyKey, any>,
IncludedKeys extends keyof ObjectType,
>(
object: ObjectType,
// eslint-disable-next-line @typescript-eslint/array-type
keys: readonly IncludedKeys[],
): Simplify<DistributivePick<ObjectType, IncludedKeys>> {
const result = {} as DistributivePick<ObjectType, IncludedKeys>
for (const key of keys) {
const descriptor = Object.getOwnPropertyDescriptor(object, key)
if (!descriptor?.enumerable) {
continue
}
Object.defineProperty(result, key, descriptor)
}
return result
}