Files
opencode-manager/frontend/src/hooks/useOpenCodeServers.ts
T
Chris Scott b6c21de0bb feat(frontend): persist the query cache and restore the signed-in session offline
Allowlisted query data is persisted to localStorage for 24h, scoped to the origin and user and cleared on sign-out. The last validated user is remembered so a cold start opens with cached lists when the session check fails on the network, while a 401 or signed-out response still signs out. Lists keep cached data when a refetch fails.
2026-10-24 19:24:26 -04:00

105 lines
3.2 KiB
TypeScript

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'
import { cachedListStates } from '@/lib/queryState'
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),
})
const serversListState = cachedListStates(serversQuery.data, serversQuery.isError)
return {
servers: serversQuery.data ?? [],
isLoading: serversQuery.isLoading,
/** True only while no server list is cached and no error settled, so a restore is not read as a missing server. */
showsInitialLoading: serversListState.showsInitialLoading,
isSuccess: serversQuery.isSuccess,
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(options: { enabled?: boolean } = {}) {
const statusesQuery = useQuery({
queryKey: OPENCODE_SERVER_STATUSES_QUERY_KEY,
queryFn: listOpenCodeServerStatuses,
refetchInterval: SERVER_STATUS_POLL_INTERVAL_MS,
retry: false,
enabled: options.enabled ?? true,
})
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,
}
}