mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(servers): link remote OpenCode servers with QR pairing
This commit is contained in:
1 parent
7f5c242fe0
commit
c591c15743
115 files changed
+7630
-566
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type {
|
||||
CreateOpenCodeServerRequest,
|
||||
OpenCodeServerStatus,
|
||||
TestOpenCodeServerRequest,
|
||||
UpdateOpenCodeServerRequest,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
import {
|
||||
createOpenCodeServer,
|
||||
deleteOpenCodeServer,
|
||||
listOpenCodeServers,
|
||||
listOpenCodeServerStatuses,
|
||||
testOpenCodeServer,
|
||||
testOpenCodeServerById,
|
||||
updateOpenCodeServer,
|
||||
} from '@/api/opencodeServers'
|
||||
|
||||
const OPENCODE_SERVERS_QUERY_KEY = ['opencode-servers']
|
||||
const OPENCODE_SERVER_STATUSES_QUERY_KEY = ['opencode-server-statuses']
|
||||
const SERVER_STATUS_POLL_INTERVAL_MS = 15000
|
||||
|
||||
export function useOpenCodeServers(options: { enabled?: boolean } = {}) {
|
||||
const queryClient = useQueryClient()
|
||||
const serversQuery = useQuery({
|
||||
queryKey: OPENCODE_SERVERS_QUERY_KEY,
|
||||
queryFn: listOpenCodeServers,
|
||||
enabled: options.enabled ?? true,
|
||||
})
|
||||
|
||||
const invalidateServers = () => {
|
||||
queryClient.invalidateQueries({ queryKey: OPENCODE_SERVERS_QUERY_KEY })
|
||||
}
|
||||
|
||||
const createMutation = useMutation({
|
||||
mutationFn: (input: CreateOpenCodeServerRequest) => createOpenCodeServer(input),
|
||||
onSuccess: invalidateServers,
|
||||
})
|
||||
|
||||
const updateMutation = useMutation({
|
||||
mutationFn: ({ id, input }: { id: string; input: UpdateOpenCodeServerRequest }) =>
|
||||
updateOpenCodeServer(id, input),
|
||||
onSuccess: invalidateServers,
|
||||
})
|
||||
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: (id: string) => deleteOpenCodeServer(id),
|
||||
onSuccess: invalidateServers,
|
||||
})
|
||||
|
||||
const testMutation = useMutation({
|
||||
mutationFn: (input: TestOpenCodeServerRequest) => testOpenCodeServer(input),
|
||||
})
|
||||
|
||||
const testByIdMutation = useMutation({
|
||||
mutationFn: (id: string) => testOpenCodeServerById(id),
|
||||
})
|
||||
|
||||
return {
|
||||
servers: serversQuery.data ?? [],
|
||||
isLoading: serversQuery.isLoading,
|
||||
error: serversQuery.error,
|
||||
refetch: serversQuery.refetch,
|
||||
createAsync: createMutation.mutateAsync,
|
||||
isCreating: createMutation.isPending,
|
||||
updateAsync: updateMutation.mutateAsync,
|
||||
isUpdating: updateMutation.isPending,
|
||||
deleteAsync: deleteMutation.mutateAsync,
|
||||
isDeleting: deleteMutation.isPending,
|
||||
testAsync: testMutation.mutateAsync,
|
||||
testByIdAsync: testByIdMutation.mutateAsync,
|
||||
}
|
||||
}
|
||||
|
||||
export function useOpenCodeServerStatuses() {
|
||||
const statusesQuery = useQuery({
|
||||
queryKey: OPENCODE_SERVER_STATUSES_QUERY_KEY,
|
||||
queryFn: listOpenCodeServerStatuses,
|
||||
refetchInterval: SERVER_STATUS_POLL_INTERVAL_MS,
|
||||
retry: false,
|
||||
})
|
||||
|
||||
const statusById = (statusesQuery.data ?? []).reduce<Record<string, OpenCodeServerStatus>>(
|
||||
(byId, status) => {
|
||||
byId[status.serverId] = status
|
||||
return byId
|
||||
},
|
||||
{},
|
||||
)
|
||||
|
||||
return {
|
||||
statuses: statusesQuery.data ?? [],
|
||||
statusById,
|
||||
isLoading: statusesQuery.isLoading,
|
||||
error: statusesQuery.error,
|
||||
refetch: statusesQuery.refetch,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user