import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { Skeleton, Table, Tooltip, notification, Modal, Input, Form, Switch, Segmented } 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" 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") const [selectedSegment, setSelectedSegment] = useState<"custom" | "copilot">( "custom" ) const { data, status } = useQuery({ queryKey: ["fetchAllPrompts"], queryFn: getAllPrompts }) 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") }) } }) 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} /> )} ) } return (
{ setSelectedSegment(value as "custom" | "copilot") }} />
{selectedSegment === "custom" && customPrompts()} setOpen(false)} footer={null}>
savePromptMutation(values)} layout="vertical" form={createForm}>
setOpenEdit(false)} footer={null}>
updatePromptMutation(values)} layout="vertical" form={editForm}>
) }