import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { Skeleton, Table, Tooltip, notification, Modal, Input, Form, Switch, Segmented, Tag } from "antd" import { Trash2, Pen, Computer, Zap } from "lucide-react" import { useState } from "react" import { useTranslation } from "react-i18next" import { deletePromptById, getAllPrompts, savePrompt, updatePrompt } from "@/db" import { getAllCopilotPrompts, setAllCopilotPrompts } from "@/services/application" import { tagColors } from "@/utils/color" export const PromptBody = () => { const queryClient = useQueryClient() const [open, setOpen] = useState(false) const [openEdit, setOpenEdit] = useState(false) const [editId, setEditId] = useState("") const [createForm] = Form.useForm() const [editForm] = Form.useForm() const { t } = useTranslation(["settings", "common"]) const [selectedSegment, setSelectedSegment] = useState<"custom" | "copilot">( "custom" ) const [openCopilotEdit, setOpenCopilotEdit] = useState(false) const [editCopilotId, setEditCopilotId] = useState("") const [editCopilotForm] = Form.useForm() const { data, status } = useQuery({ queryKey: ["fetchAllPrompts"], queryFn: getAllPrompts }) const { data: copilotData, status: copilotStatus } = useQuery({ queryKey: ["fetchCopilotPrompts"], queryFn: getAllCopilotPrompts }) const { mutate: deletePrompt } = useMutation({ mutationFn: deletePromptById, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["fetchAllPrompts"] }) notification.success({ message: t("managePrompts.notification.deletedSuccess"), description: t("managePrompts.notification.deletedSuccessDesc") }) }, onError: (error) => { notification.error({ message: t("managePrompts.notification.error"), description: error?.message || t("managePrompts.notification.someError") }) } }) const { mutate: savePromptMutation, isPending: savePromptLoading } = useMutation({ mutationFn: savePrompt, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["fetchAllPrompts"] }) setOpen(false) createForm.resetFields() notification.success({ message: t("managePrompts.notification.addSuccess"), description: t("managePrompts.notification.addSuccessDesc") }) }, onError: (error) => { notification.error({ message: t("managePrompts.notification.error"), description: error?.message || t("managePrompts.notification.someError") }) } }) const { mutate: updatePromptMutation, isPending: isUpdatingPrompt } = useMutation({ mutationFn: async (data: any) => { return await updatePrompt({ ...data, id: editId }) }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["fetchAllPrompts"] }) setOpenEdit(false) editForm.resetFields() notification.success({ message: t("managePrompts.notification.updatedSuccess"), description: t("managePrompts.notification.updatedSuccessDesc") }) }, onError: (error) => { notification.error({ message: t("managePrompts.notification.error"), description: error?.message || t("managePrompts.notification.someError") }) } }) const { mutate: updateCopilotPrompt, isPending: isUpdatingCopilotPrompt } = useMutation({ mutationFn: async (data: any) => { return await setAllCopilotPrompts([ { key: data.key, prompt: data.prompt } ]) }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["fetchCopilotPrompts"] }) setOpenCopilotEdit(false) editCopilotForm.resetFields() notification.success({ message: t("managePrompts.notification.updatedSuccess"), description: t("managePrompts.notification.updatedSuccessDesc") }) }, onError: (error) => { notification.error({ message: t("managePrompts.notification.error"), description: error?.message || t("managePrompts.notification.someError") }) } }) function customPrompts() { return (
{status === "pending" && } {status === "success" && ( ( {content} ) }, { title: t("managePrompts.columns.prompt"), dataIndex: "content", key: "content", render: (content) => ( {content} ) }, { title: t("managePrompts.columns.type"), dataIndex: "is_system", key: "is_system", render: (is_system) => ( {is_system ? ( <> {" "} {t("managePrompts.systemPrompt")} ) : ( <> {" "} {t("managePrompts.quickPrompt")} )} ) }, { title: t("managePrompts.columns.actions"), render: (_, record) => (
) } ]} bordered dataSource={data} rowKey={(record) => record.id} /> )} ) } function copilotPrompts() { return (
{copilotStatus === "pending" && } {copilotStatus === "success" && (
( {t(`common:copilot.${content}`)} ) }, { title: t("managePrompts.columns.prompt"), dataIndex: "prompt", key: "prompt", render: (content) => ( {content} ) }, { render: (_, record) => (
) } ]} bordered dataSource={copilotData} rowKey={(record) => record.key} /> )} ) } return (
{ setSelectedSegment(value as "custom" | "copilot") }} />
{selectedSegment === "custom" && customPrompts()} {selectedSegment === "copilot" && copilotPrompts()} setOpen(false)} footer={null}>
savePromptMutation(values)} layout="vertical" form={createForm}>
setOpenEdit(false)} footer={null}>
updatePromptMutation(values)} layout="vertical" form={editForm}>
setOpenCopilotEdit(false)} footer={null}>
updateCopilotPrompt({ key: editCopilotId, prompt: values.prompt }) } layout="vertical" form={editCopilotForm}> { if (value && value.includes("{text}")) { return Promise.resolve() } return Promise.reject( new Error( t("managePrompts.form.prompt.missingTextPlaceholder") ) ) } } ]}>
) }