import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { Skeleton, Table, Tooltip, notification, Modal, Input, Form, Switch } 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 "~/libs/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("option") 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") }) } }) return (
{status === "pending" && } {status === "success" && ( is_system ? ( {t("managePrompts.systemPrompt")} ) : ( {t("managePrompts.quickPrompt")} ) }, { title: t("managePrompts.columns.actions"), render: (_, record) => (
) } ]} bordered dataSource={data} rowKey={(record) => record.id} /> )} setOpen(false)} footer={null}>
savePromptMutation(values)} layout="vertical" form={createForm}>
setOpenEdit(false)} footer={null}>
updatePromptMutation(values)} layout="vertical" form={editForm}>
) }