Prompt gallery (#957)
- [x] show prompt instead of app in autocomplete - [x] use proper array/list for db (tags) - [x] don't do <dyad-prompt> - replace inline
This commit is contained in:
81
src/hooks/usePrompts.ts
Normal file
81
src/hooks/usePrompts.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { IpcClient } from "@/ipc/ipc_client";
|
||||
|
||||
export interface PromptItem {
|
||||
id: number;
|
||||
title: string;
|
||||
description: string | null;
|
||||
content: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export function usePrompts() {
|
||||
const queryClient = useQueryClient();
|
||||
const listQuery = useQuery({
|
||||
queryKey: ["prompts"],
|
||||
queryFn: async (): Promise<PromptItem[]> => {
|
||||
const ipc = IpcClient.getInstance();
|
||||
return ipc.listPrompts();
|
||||
},
|
||||
meta: { showErrorToast: true },
|
||||
});
|
||||
|
||||
const createMutation = useMutation({
|
||||
mutationFn: async (params: {
|
||||
title: string;
|
||||
description?: string;
|
||||
content: string;
|
||||
}): Promise<PromptItem> => {
|
||||
const ipc = IpcClient.getInstance();
|
||||
return ipc.createPrompt(params);
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["prompts"] });
|
||||
},
|
||||
meta: {
|
||||
showErrorToast: true,
|
||||
},
|
||||
});
|
||||
|
||||
const updateMutation = useMutation({
|
||||
mutationFn: async (params: {
|
||||
id: number;
|
||||
title: string;
|
||||
description?: string;
|
||||
content: string;
|
||||
}): Promise<void> => {
|
||||
const ipc = IpcClient.getInstance();
|
||||
return ipc.updatePrompt(params);
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["prompts"] });
|
||||
},
|
||||
meta: {
|
||||
showErrorToast: true,
|
||||
},
|
||||
});
|
||||
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: async (id: number): Promise<void> => {
|
||||
const ipc = IpcClient.getInstance();
|
||||
return ipc.deletePrompt(id);
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["prompts"] });
|
||||
},
|
||||
meta: {
|
||||
showErrorToast: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
prompts: listQuery.data ?? [],
|
||||
isLoading: listQuery.isLoading,
|
||||
error: listQuery.error,
|
||||
refetch: listQuery.refetch,
|
||||
createPrompt: createMutation.mutateAsync,
|
||||
updatePrompt: updateMutation.mutateAsync,
|
||||
deletePrompt: deleteMutation.mutateAsync,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user