mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 10:36:22 +08:00
52 lines
1.1 KiB
TypeScript
52 lines
1.1 KiB
TypeScript
import {
|
|
useMutateQueryResource,
|
|
useMutation,
|
|
} from '@affine/admin/use-mutation';
|
|
import { useQuery } from '@affine/admin/use-query';
|
|
import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks';
|
|
import { getPromptsQuery, updatePromptMutation } from '@affine/graphql';
|
|
import { toast } from 'sonner';
|
|
|
|
import type { Prompt } from './prompts';
|
|
|
|
export const usePrompt = () => {
|
|
const { data } = useQuery({
|
|
query: getPromptsQuery,
|
|
});
|
|
|
|
const { trigger } = useMutation({
|
|
mutation: updatePromptMutation,
|
|
});
|
|
|
|
const revalidate = useMutateQueryResource();
|
|
|
|
const updatePrompt = useAsyncCallback(
|
|
async ({
|
|
name,
|
|
messages,
|
|
}: {
|
|
name: string;
|
|
messages: Prompt['messages'];
|
|
}) => {
|
|
await trigger({
|
|
name,
|
|
messages,
|
|
})
|
|
.then(async () => {
|
|
await revalidate(getPromptsQuery);
|
|
toast.success('Prompt updated successfully');
|
|
})
|
|
.catch(e => {
|
|
toast(e.message);
|
|
console.error(e);
|
|
});
|
|
},
|
|
[revalidate, trigger]
|
|
);
|
|
|
|
return {
|
|
prompts: data.listCopilotPrompts,
|
|
updatePrompt,
|
|
};
|
|
};
|