Spaces:
Running
Running
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; | |
import { fetchLLMConfigs, createLLMConfig, updateLLMConfig, setDefaultLLMConfig, deleteLLMConfig } from './llmConfigApi'; | |
export const useLLMConfigs = () => { | |
const queryClient = useQueryClient(); | |
const { data: configs = [], isLoading, error } = useQuery({ | |
queryKey: ['llmConfigs'], | |
queryFn: fetchLLMConfigs, | |
}); | |
const createMutation = useMutation({ | |
mutationFn: createLLMConfig, | |
onSuccess: () => { | |
queryClient.invalidateQueries({ queryKey: ['llmConfigs'] }); | |
}, | |
}); | |
const updateMutation = useMutation({ | |
mutationFn: updateLLMConfig, | |
onSuccess: () => { | |
queryClient.invalidateQueries({ queryKey: ['llmConfigs'] }); | |
}, | |
}); | |
const setDefaultMutation = useMutation({ | |
mutationFn: setDefaultLLMConfig, | |
onSuccess: () => { | |
queryClient.invalidateQueries({ queryKey: ['llmConfigs'] }); | |
}, | |
}); | |
const deleteMutation = useMutation({ | |
mutationFn: deleteLLMConfig, | |
onSuccess: () => { | |
queryClient.invalidateQueries({ queryKey: ['llmConfigs'] }); | |
}, | |
}); | |
return { | |
configs, | |
isLoading, | |
error: error ? (error instanceof Error ? error.message : 'Failed to fetch configurations') : null, | |
createConfig: createMutation.mutateAsync, | |
updateConfig: updateMutation.mutateAsync, | |
setAsDefaultConfig: setDefaultMutation.mutateAsync, | |
deleteConfig: deleteMutation.mutateAsync, | |
}; | |
}; |