Files
routstr-core/ui/lib/hooks/useModels.ts
T
2025-10-21 22:07:47 +02:00

49 lines
1.4 KiB
TypeScript

import { useQuery, useQueryClient } from '@tanstack/react-query';
import { ModelService } from '../api/services/models';
// Query keys for models
const modelKeys = {
all: ['models'] as const,
list: () => [...modelKeys.all, 'list'] as const,
detail: (id: string) => [...modelKeys.all, 'detail', id] as const,
test: (id: string) => [...modelKeys.all, 'test', id] as const,
};
/**
* Hook to fetch the list of available models
*/
export function useModels(options?: {
team_id?: string;
return_wildcard_routes?: boolean;
enabled?: boolean;
}) {
return useQuery({
queryKey: modelKeys.list(),
queryFn: () => ModelService.listModels(options),
enabled: options?.enabled ?? true,
});
}
/**
* Hook to fetch detailed information about a specific model
*/
export function useModelInfo(modelId: string, options?: { enabled?: boolean }) {
return useQuery({
queryKey: modelKeys.detail(modelId),
queryFn: () => ModelService.getModelInfo(modelId),
enabled: !!modelId && (options?.enabled ?? true),
});
}
export function usePrefetchModelInfo() {
// This is only used for this function, not needed in the larger scope
const queryClient = useQueryClient();
return async (modelId: string) => {
await queryClient.prefetchQuery({
queryKey: modelKeys.detail(modelId),
queryFn: () => ModelService.getModelInfo(modelId),
});
};
}