Spaces:
Running
Running
| 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, | |
| }; | |
| }; |