claude-code-best/packages/remote-control-server/web/src/hooks/useCommands.ts
2026-05-01 21:39:30 +08:00

37 lines
1.0 KiB
TypeScript

import { useState, useEffect, useMemo } from 'react'
import type { ACPClient } from '../acp/client'
import type { AvailableCommand } from '../acp/types'
export interface UseCommandsResult {
/** List of available slash commands from the agent */
commands: AvailableCommand[]
/** Whether any commands are available */
hasCommands: boolean
}
/**
* Hook to manage available commands state.
* Follows the same pattern as useModels — event-driven with immediate callback.
*/
export function useCommands(client: ACPClient): UseCommandsResult {
const [commands, setCommands] = useState<AvailableCommand[]>(
client.availableCommands,
)
useEffect(() => {
const handleCommandsChanged = (newCommands: AvailableCommand[]) => {
setCommands(newCommands)
}
client.setAvailableCommandsChangedHandler(handleCommandsChanged)
return () => {
client.setAvailableCommandsChangedHandler(() => {})
}
}, [client])
const hasCommands = useMemo(() => commands.length > 0, [commands])
return { commands, hasCommands }
}