import { useRef } from "react"; import System from "@/models/system"; import showToast from "@/utils/toast"; import { useModal } from "@/hooks/useModal"; import ModalWrapper from "@/components/ModalWrapper"; import EditVariableModal from "./EditVariableModal"; import { titleCase } from "text-case"; import truncate from "truncate"; import { Trash } from "@phosphor-icons/react"; /** * A row component for displaying a system prompt variable * @param {{id: number|null, key: string, value: string, description: string, type: string}} variable - The system prompt variable to display * @param {Function} onRefresh - A function to call when the variable is refreshed * @returns {JSX.Element} A JSX element for displaying the variable */ export default function VariableRow({ variable, onRefresh }) { const rowRef = useRef(null); const { isOpen, openModal, closeModal } = useModal(); const handleDelete = async () => { if (!variable.id) return; if ( !window.confirm( `Are you sure you want to delete the variable "${variable.key}"?\nThis action is irreversible.` ) ) return false; try { await System.promptVariables.delete(variable.id); rowRef?.current?.remove(); showToast("Variable deleted successfully", "success", { clear: true }); if (onRefresh) onRefresh(); } catch (error) { console.error("Error deleting variable:", error); showToast("Failed to delete variable", "error", { clear: true }); } }; const getTypeColorTheme = (type) => { switch (type) { case "system": return { bg: "bg-blue-600/20", text: "text-blue-400 light:text-blue-800", }; case "dynamic": return { bg: "bg-green-600/20", text: "text-green-400 light:text-green-800", }; default: return { bg: "bg-yellow-600/20", text: "text-yellow-400 light:text-yellow-800", }; } }; const colorTheme = getTypeColorTheme(variable.type); return ( <> {variable.key} {typeof variable.value === "function" ? variable.value() : truncate(variable.value, 50)} {truncate(variable.description || "-", 50)} {titleCase(variable.type)} {variable.type === "static" && ( <> )} ); }