mirror of
https://github.com/Routstr/routstr-core.git
synced 2026-08-09 11:04:36 +00:00
49 lines
1.4 KiB
TypeScript
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),
|
|
});
|
|
};
|
|
}
|