Spaces:
Running
Running
File size: 1,464 Bytes
79278ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { fetchLlmPrompts, createLlmPrompt, updateLlmPrompt, setDefaultLlmPrompt, deleteLlmPrompt } from './llmPromptApi';
export const useLlmPrompts = () => {
const queryClient = useQueryClient();
const { data: prompts = [], isLoading, error } = useQuery({
queryKey: ['llmPrompts'],
queryFn: fetchLlmPrompts,
});
const createMutation = useMutation({
mutationFn: createLlmPrompt,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['llmPrompts'] });
},
});
const updateMutation = useMutation({
mutationFn: updateLlmPrompt,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['llmPrompts'] });
},
});
const setDefaultMutation = useMutation({
mutationFn: setDefaultLlmPrompt,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['llmPrompts'] });
},
});
const deleteMutation = useMutation({
mutationFn: deleteLlmPrompt,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['llmPrompts'] });
},
});
return {
prompts,
isLoading,
error: error ? (error instanceof Error ? error.message : 'Failed to fetch prompturations') : null,
createPrompt: createMutation.mutateAsync,
updatePrompt: updateMutation.mutateAsync,
setAsDefaultPrompt: setDefaultMutation.mutateAsync,
deletePrompt: deleteMutation.mutateAsync,
};
}; |