Skip to content

Commit 7aad96c

Browse files
Apply PR #25941: refactor(app): centralize sync query options
2 parents 27e8c3c + 79cddb6 commit 7aad96c

7 files changed

Lines changed: 70 additions & 52 deletions

File tree

packages/app/src/components/dialog-select-mcp.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { Dialog } from "@opencode-ai/ui/dialog"
66
import { List } from "@opencode-ai/ui/list"
77
import { Switch } from "@opencode-ai/ui/switch"
88
import { useLanguage } from "@/context/language"
9-
import { mcpQueryKey } from "@/context/global-sync"
9+
import { useQueryOptions } from "@/context/global-sync"
10+
import { pathKey } from "@/utils/path-key"
1011

1112
const statusLabels = {
1213
connected: "mcp.status.connected",
@@ -20,6 +21,7 @@ export const DialogSelectMcp: Component = () => {
2021
const sdk = useSDK()
2122
const language = useLanguage()
2223
const queryClient = useQueryClient()
24+
const queryOptions = useQueryOptions()
2325

2426
const items = createMemo(() =>
2527
Object.entries(sync.data.mcp ?? {})
@@ -32,7 +34,7 @@ export const DialogSelectMcp: Component = () => {
3234
if (sync.data.mcp[name]?.status === "connected") await sdk.client.mcp.disconnect({ name })
3335
else await sdk.client.mcp.connect({ name })
3436
},
35-
onSuccess: () => queryClient.refetchQueries({ queryKey: mcpQueryKey(sync.directory) }),
37+
onSuccess: () => queryClient.refetchQueries(queryOptions.mcp(pathKey(sync.directory))),
3638
}))
3739

3840
const enabledCount = createMemo(() => items().filter((i) => i.status === "connected").length)

packages/app/src/components/prompt-input.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from "@/context/prompt"
1717
import { useLayout } from "@/context/layout"
1818
import { useSDK } from "@/context/sdk"
19-
import { useGlobalSDK } from "@/context/global-sdk"
2019
import { useSync } from "@/context/sync"
2120
import { useComments } from "@/context/comments"
2221
import { Button } from "@opencode-ai/ui/button"
@@ -56,7 +55,8 @@ import { PromptDragOverlay } from "./prompt-input/drag-overlay"
5655
import { promptPlaceholder } from "./prompt-input/placeholder"
5756
import { ImagePreview } from "@opencode-ai/ui/image-preview"
5857
import { useQueries } from "@tanstack/solid-query"
59-
import { loadAgentsQuery, loadProvidersQuery } from "@/context/global-sync/bootstrap"
58+
import { useQueryOptions } from "@/context/global-sync"
59+
import { pathKey } from "@/utils/path-key"
6060

6161
interface PromptInputProps {
6262
class?: string
@@ -103,7 +103,7 @@ const NON_EMPTY_TEXT = /[^\s\u200B]/
103103

104104
export const PromptInput: Component<PromptInputProps> = (props) => {
105105
const sdk = useSDK()
106-
const globalSDK = useGlobalSDK()
106+
const queryOptions = useQueryOptions()
107107

108108
const sync = useSync()
109109
const local = useLocal()
@@ -1256,9 +1256,9 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
12561256

12571257
const [agentsQuery, globalProvidersQuery, providersQuery] = useQueries(() => ({
12581258
queries: [
1259-
loadAgentsQuery(sdk.directory, sdk.client),
1260-
loadProvidersQuery(null, globalSDK.client),
1261-
loadProvidersQuery(sdk.directory, sdk.client),
1259+
queryOptions.agents(pathKey(sdk.directory)),
1260+
queryOptions.providers(null),
1261+
queryOptions.providers(pathKey(sdk.directory)),
12621262
],
12631263
}))
12641264

packages/app/src/components/status-popover-body.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import { useSDK } from "@/context/sdk"
1515
import { normalizeServerUrl, ServerConnection, useServer } from "@/context/server"
1616
import { useSync } from "@/context/sync"
1717
import { useCheckServerHealth, type ServerHealth } from "@/utils/server-health"
18-
import { mcpQueryKey } from "@/context/global-sync"
18+
import { useQueryOptions } from "@/context/global-sync"
19+
import { pathKey } from "@/utils/path-key"
1920

2021
const pollMs = 10_000
2122

@@ -139,13 +140,14 @@ const useMcpToggleMutation = () => {
139140
const sdk = useSDK()
140141
const language = useLanguage()
141142
const queryClient = useQueryClient()
143+
const queryOptions = useQueryOptions()
142144

143145
return useMutation(() => ({
144146
mutationFn: async (name: string) => {
145147
const status = sync.data.mcp[name]
146148
await (status?.status === "connected" ? sdk.client.mcp.disconnect({ name }) : sdk.client.mcp.connect({ name }))
147149
},
148-
onSuccess: () => queryClient.refetchQueries({ queryKey: mcpQueryKey(sync.directory) }),
150+
onSuccess: () => queryClient.refetchQueries(queryOptions.mcp(pathKey(sync.directory))),
149151
onError: (err) => {
150152
showToast({
151153
variant: "error",

packages/app/src/context/global-sync.tsx

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import {
1818
bootstrapDirectory,
1919
bootstrapGlobal,
2020
clearProviderRev,
21+
loadAgentsQuery,
2122
loadGlobalConfigQuery,
2223
loadPathQuery,
24+
loadProjectsQuery,
2325
loadProvidersQuery,
2426
} from "./global-sync/bootstrap"
2527
import { createChildStoreManager } from "./global-sync/child-store"
@@ -33,6 +35,7 @@ import { formatServerError } from "@/utils/server-errors"
3335
import { queryOptions, useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/solid-query"
3436
import { createRefreshQueue } from "./global-sync/queue"
3537
import { directoryKey } from "./global-sync/utils"
38+
import { PathKey } from "@/utils/path-key"
3639

3740
type GlobalStore = {
3841
ready: boolean
@@ -48,24 +51,33 @@ type GlobalStore = {
4851
reload: undefined | "pending" | "complete"
4952
}
5053

51-
export const loadSessionsQueryKey = (directory: string) => [directory, "loadSessions"] as const
52-
53-
export const mcpQueryKey = (directory: string) => [directory, "mcp"] as const
54-
5554
export const loadMcpQuery = (directory: string, sdk: OpencodeClient) =>
5655
queryOptions({
57-
queryKey: mcpQueryKey(directory),
56+
queryKey: [directory, "mcp"] as const,
5857
queryFn: () => sdk.mcp.status().then((r) => r.data ?? {}),
5958
})
6059

61-
export const lspQueryKey = (directory: string) => [directory, "lsp"] as const
62-
6360
export const loadLspQuery = (directory: string, sdk: OpencodeClient) =>
6461
queryOptions({
65-
queryKey: lspQueryKey(directory),
62+
queryKey: [directory, "lsp"] as const,
6663
queryFn: () => sdk.lsp.status().then((r) => r.data ?? []),
6764
})
6865

66+
function makeQueryOptionsApi(globalSDK: () => OpencodeClient, sdkFor: (dir: PathKey) => OpencodeClient) {
67+
return {
68+
globalConfig: () => loadGlobalConfigQuery(globalSDK()),
69+
projects: () => loadProjectsQuery(globalSDK()),
70+
providers: (directory: PathKey | null) =>
71+
loadProvidersQuery(directory, directory === null ? globalSDK() : sdkFor(directory)),
72+
path: (directory: PathKey | null) => loadPathQuery(directory, directory === null ? globalSDK() : sdkFor(directory)),
73+
agents: (directory: PathKey) => loadAgentsQuery(directory, sdkFor(directory)),
74+
mcp: (directory: PathKey) => loadMcpQuery(directory, sdkFor(directory)),
75+
lsp: (directory: PathKey) => loadLspQuery(directory, sdkFor(directory)),
76+
sessions: (directory: PathKey) => ({ queryKey: [directory, "loadSessions"] as const }),
77+
}
78+
}
79+
export type QueryOptionsApi = ReturnType<typeof makeQueryOptionsApi>
80+
6981
function createGlobalSync() {
7082
const globalSDK = useGlobalSDK()
7183
const language = useLanguage()
@@ -77,12 +89,22 @@ function createGlobalSync() {
7789
const sessionLoads = new Map<string, Promise<void>>()
7890
const sessionMeta = new Map<string, { limit: number }>()
7991

92+
const sdkFor = (directory: string) => {
93+
const key = directoryKey(directory)
94+
const cached = sdkCache.get(key)
95+
if (cached) return cached
96+
const sdk = globalSDK.createClient({
97+
directory,
98+
throwOnError: true,
99+
})
100+
sdkCache.set(key, sdk)
101+
return sdk
102+
}
103+
104+
const queryOptionsApi = makeQueryOptionsApi(() => globalSDK.client, sdkFor)
105+
80106
const [configQuery, providerQuery, pathQuery] = useQueries(() => ({
81-
queries: [
82-
loadGlobalConfigQuery(globalSDK.client),
83-
loadProvidersQuery(null, globalSDK.client),
84-
loadPathQuery(null, globalSDK.client),
85-
],
107+
queries: [queryOptionsApi.globalConfig(), queryOptionsApi.providers(null), queryOptionsApi.path(null)],
86108
}))
87109

88110
const [globalStore, setGlobalStore] = createStore<GlobalStore>({
@@ -181,18 +203,6 @@ function createGlobalSync() {
181203
bootstrapInstance,
182204
})
183205

184-
const sdkFor = (directory: string) => {
185-
const key = directoryKey(directory)
186-
const cached = sdkCache.get(key)
187-
if (cached) return cached
188-
const sdk = globalSDK.createClient({
189-
directory,
190-
throwOnError: true,
191-
})
192-
sdkCache.set(key, sdk)
193-
return sdk
194-
}
195-
196206
const children = createChildStoreManager({
197207
owner,
198208
isBooting: (directory) => booting.has(directory),
@@ -209,7 +219,7 @@ function createGlobalSync() {
209219
clearSessionPrefetchDirectory(key)
210220
},
211221
translate: language.t,
212-
getSdk: sdkFor,
222+
queryOptions: queryOptionsApi,
213223
global: {
214224
provider: globalStore.provider,
215225
},
@@ -239,7 +249,7 @@ function createGlobalSync() {
239249
const limit = Math.max(store.limit + SESSION_RECENT_LIMIT, SESSION_RECENT_LIMIT)
240250
const promise = queryClient
241251
.fetchQuery({
242-
queryKey: loadSessionsQueryKey(key),
252+
...queryOptionsApi.sessions(key),
243253
queryFn: () =>
244254
loadRootSessionsWithFallback({
245255
directory,
@@ -368,7 +378,7 @@ function createGlobalSync() {
368378
setSessionTodo,
369379
vcsCache: children.vcsCache.get(key),
370380
loadLsp: () => {
371-
void queryClient.fetchQuery(loadLspQuery(key, sdkFor(directory)))
381+
void queryClient.fetchQuery(queryOptionsApi.lsp(key))
372382
},
373383
})
374384
})
@@ -422,6 +432,7 @@ function createGlobalSync() {
422432
},
423433
child: children.child,
424434
peek: children.peek,
435+
queryOptions: queryOptionsApi,
425436
// bootstrap,
426437
updateConfig: updateConfigMutation.mutateAsync,
427438
project: projectApi,
@@ -443,3 +454,7 @@ export function useGlobalSync() {
443454
if (!context) throw new Error("useGlobalSync must be used within GlobalSyncProvider")
444455
return context
445456
}
457+
458+
export function useQueryOptions() {
459+
return useGlobalSync().queryOptions
460+
}

packages/app/src/context/global-sync/child-store.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("createChildStoreManager", () => {
2222
onBootstrap() {},
2323
onDispose() {},
2424
translate: (key) => key,
25-
getSdk: () => null!,
25+
queryOptions: {} as any,
2626
global: { provider: null! },
2727
})
2828

packages/app/src/context/global-sync/child-store.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createRoot, getOwner, onCleanup, runWithOwner, type Owner } from "solid-js"
22
import { createStore, type SetStoreFunction, type Store } from "solid-js/store"
33
import { Persist, persisted } from "@/utils/persist"
4-
import type { OpencodeClient, ProviderListResponse, VcsInfo } from "@opencode-ai/sdk/v2/client"
4+
import type { ProviderListResponse, VcsInfo } from "@opencode-ai/sdk/v2/client"
55
import {
66
DIR_IDLE_TTL_MS,
77
MAX_DIR_STORES,
@@ -15,8 +15,7 @@ import {
1515
} from "./types"
1616
import { canDisposeDirectory, pickDirectoriesToEvict } from "./eviction"
1717
import { useQueries } from "@tanstack/solid-query"
18-
import { loadPathQuery, loadProvidersQuery } from "./bootstrap"
19-
import { loadLspQuery, loadMcpQuery } from "../global-sync"
18+
import { QueryOptionsApi } from "../global-sync"
2019
import { directoryKey, type DirectoryKey } from "./utils"
2120

2221
export function createChildStoreManager(input: {
@@ -26,7 +25,7 @@ export function createChildStoreManager(input: {
2625
onBootstrap: (directory: string) => void
2726
onDispose: (directory: string) => void
2827
translate: (key: string, vars?: Record<string, string | number>) => string
29-
getSdk: (directory: string) => OpencodeClient
28+
queryOptions: QueryOptionsApi
3029
global: {
3130
provider: ProviderListResponse
3231
}
@@ -182,17 +181,15 @@ export function createChildStoreManager(input: {
182181

183182
const init = () =>
184183
createRoot((dispose) => {
185-
const sdk = input.getSdk(directory)
186-
187184
const initialMeta = meta[0].value
188185
const initialIcon = icon[0].value
189186

190187
const [pathQuery, mcpQuery, lspQuery, providerQuery] = useQueries(() => ({
191188
queries: [
192-
loadPathQuery(key, sdk),
193-
loadMcpQuery(key, sdk),
194-
loadLspQuery(key, sdk),
195-
loadProvidersQuery(key, sdk),
189+
input.queryOptions.path(key),
190+
input.queryOptions.mcp(key),
191+
input.queryOptions.lsp(key),
192+
input.queryOptions.providers(key),
196193
],
197194
}))
198195

packages/app/src/pages/layout/sidebar-workspace.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Spinner } from "@opencode-ai/ui/spinner"
1414
import { Tooltip } from "@opencode-ai/ui/tooltip"
1515
import { type Session } from "@opencode-ai/sdk/v2/client"
1616
import { type LocalProject } from "@/context/layout"
17-
import { loadSessionsQueryKey, useGlobalSync } from "@/context/global-sync"
17+
import { useGlobalSync, useQueryOptions } from "@/context/global-sync"
1818
import { useLanguage } from "@/context/language"
1919
import { pathKey } from "@/utils/path-key"
2020
import { NewSessionItem, SessionItem, SessionSkeleton } from "./sidebar-items"
@@ -300,6 +300,7 @@ export const SortableWorkspace = (props: {
300300
const navigate = useNavigate()
301301
const params = useParams()
302302
const globalSync = useGlobalSync()
303+
const queryOptions = useQueryOptions()
303304
const language = useLanguage()
304305
const sortable = createSortable(props.directory)
305306
const [workspaceStore, setWorkspaceStore] = globalSync.child(props.directory, { bootstrap: false })
@@ -320,7 +321,7 @@ export const SortableWorkspace = (props: {
320321
const boot = createMemo(() => open() || active())
321322
const count = createMemo(() => sessions()?.length ?? 0)
322323
const hasMore = createMemo(() => workspaceStore.sessionTotal > count())
323-
const fetching = useIsFetching(() => ({ queryKey: loadSessionsQueryKey(props.directory) }))
324+
const fetching = useIsFetching(() => queryOptions.sessions(pathKey(props.directory)))
324325
const busy = createMemo(() => props.ctx.isBusy(props.directory))
325326
const loading = () => fetching() > 0 && count() === 0
326327
const touch = createMediaQuery("(hover: none)")
@@ -446,6 +447,7 @@ export const LocalWorkspace = (props: {
446447
mobile?: boolean
447448
}): JSX.Element => {
448449
const globalSync = useGlobalSync()
450+
const queryOptions = useQueryOptions()
449451
const language = useLanguage()
450452
const workspace = createMemo(() => {
451453
const [store, setStore] = globalSync.child(props.project.worktree)
@@ -454,7 +456,7 @@ export const LocalWorkspace = (props: {
454456
const slug = createMemo(() => base64Encode(props.project.worktree))
455457
const sessions = createMemo(() => sortedRootSessions(workspace().store, props.sortNow()))
456458
const count = createMemo(() => sessions()?.length ?? 0)
457-
const fetching = useIsFetching(() => ({ queryKey: loadSessionsQueryKey(props.project.worktree) }))
459+
const fetching = useIsFetching(() => queryOptions.sessions(pathKey(props.project.worktree)))
458460
const hasMore = createMemo(() => workspace().store.sessionTotal > count())
459461
const loading = () => fetching() > 0 && count() === 0
460462
const loadMore = async () => {

0 commit comments

Comments
 (0)