(args: {
api: ApiClient | null
machineId?: string | null
enabled?: boolean
})
| 4 | import { queryKeys } from '@/lib/query-keys' |
| 5 | |
| 6 | export function useCursorModelsForMachine(args: { |
| 7 | api: ApiClient | null |
| 8 | machineId?: string | null |
| 9 | enabled?: boolean |
| 10 | }): { |
| 11 | availableModels: CursorModelSummary[] |
| 12 | cliModelSkus: CursorModelSummary[] |
| 13 | currentModelId: string | null |
| 14 | isLoading: boolean |
| 15 | error: string | null |
| 16 | refetch: () => void |
| 17 | } { |
| 18 | const { api, machineId } = args |
| 19 | const enabled = Boolean(args.enabled && api && machineId) |
| 20 | |
| 21 | const query = useQuery({ |
| 22 | queryKey: machineId |
| 23 | ? queryKeys.machineCursorModels(machineId) |
| 24 | : ['machine-cursor-models', 'unknown'] as const, |
| 25 | queryFn: async () => { |
| 26 | if (!api) { |
| 27 | throw new Error('API unavailable') |
| 28 | } |
| 29 | if (!machineId) { |
| 30 | throw new Error('Cursor models target unavailable') |
| 31 | } |
| 32 | return await api.getMachineCursorModels(machineId) |
| 33 | }, |
| 34 | enabled, |
| 35 | staleTime: 60_000, |
| 36 | retry: false, |
| 37 | }) |
| 38 | |
| 39 | return { |
| 40 | availableModels: query.data?.availableModels ?? [], |
| 41 | cliModelSkus: query.data?.cliModelSkus ?? [], |
| 42 | currentModelId: query.data?.currentModelId ?? null, |
| 43 | isLoading: query.isLoading, |
| 44 | error: query.data?.success === false |
| 45 | ? (query.data.error ?? 'Failed to load Cursor models') |
| 46 | : query.error instanceof Error |
| 47 | ? query.error.message |
| 48 | : query.error |
| 49 | ? 'Failed to load Cursor models' |
| 50 | : null, |
| 51 | refetch: () => { |
| 52 | void query.refetch() |
| 53 | } |
| 54 | } |
| 55 | } |
no test coverage detected