Files
opencode-manager/frontend/src/hooks/useCommands.ts
T
Chris Scott 79d6ba2f7e fix(commands): load each location's commands instead of trusting the builtin seed
The commands query seeded every new directory key with the builtin commands as initialData. With the app's 10s staleTime, React Query treated that seed as freshly fetched data, so switching to a session in another project never requested that location's commands and the slash menu showed only the builtins until some later invalidation.

Mark the seed as stale with initialDataUpdatedAt: 0 so each directory fetches its commands on mount.
2026-10-08 16:16:51 -04:00

59 lines
1.9 KiB
TypeScript

import { useCallback } from 'react'
import { useQuery } from '@tanstack/react-query'
import { listCommands } from '@/api/opencode'
import type { CommandInfo } from '@opencode-manager/shared/opencode'
import { BUILTIN_COMMANDS } from '@/lib/builtinCommands'
import { rankByMatch } from '@/lib/fuzzyMatch'
import { useRecentCommandsStore } from '@/stores/recentCommandsStore'
function sortCommandsByName(commands: CommandInfo[]): CommandInfo[] {
return [...commands].sort((a, b) => a.name.localeCompare(b.name))
}
const SORTED_BUILTIN_COMMANDS = sortCommandsByName([...BUILTIN_COMMANDS])
interface UseCommandsOptions {
directory?: string
enabled?: boolean
}
export function useCommands(options: UseCommandsOptions = {}) {
const { directory, enabled = true } = options
const recentNames = useRecentCommandsStore((state) => state.names)
const { data: commands, isLoading: loading, error } = useQuery({
queryKey: ['opencode', 'commands', directory ?? null],
queryFn: async () => {
const loaded = await listCommands(directory)
const allCommands = [...loaded, ...BUILTIN_COMMANDS]
const uniqueCommands = allCommands.filter((command, index, self) =>
index === self.findIndex((c) => c.name === command.name)
)
return sortCommandsByName(uniqueCommands)
},
enabled,
initialData: SORTED_BUILTIN_COMMANDS,
initialDataUpdatedAt: 0,
})
const searchCommands = useCallback((query: string) => rankByMatch(commands, query, {
getName: (command) => command.name,
getDescription: (command) => command.description,
recent: recentNames,
}), [commands, recentNames])
const findCommand = useCallback((name: string) => {
const lowerName = name.toLowerCase()
return commands.find((command) => command.name.toLowerCase() === lowerName)
}, [commands])
return {
commands,
recentNames,
loading,
error: error ? 'Failed to load commands' : null,
searchCommands,
findCommand,
}
}