import React, { useEffect, useState } from "react"; import { Trash, SendHorizontal } from "lucide-react"; import { Portal, Button, Dialog, CloseButton } from "@chakra-ui/react"; import { useTokenCosts } from "@trustgraph/react-state"; import TextField from "../common/TextField"; const EditDialog = ({ open, onOpenChange, model, create }) => { const state = useTokenCosts(); const [newModel, setNewModel] = useState(""); const [input, setInput] = useState(0); const [output, setOutput] = useState(0); useEffect(() => { if (!model) return; const models = state.tokenCosts.filter((row) => row.model == model); if (models.length < 1) return; setNewModel(model); setInput(models[0].input_price * 1000000); setOutput(models[0].output_price * 1000000); }, [state.tokenCosts, model, create]); const onEdit = () => { // Create is different from edit existing if (create) { // When creating, the order is... // 1) write the prompt template, // 2) get the template index // 3) add this prompt ID to the index if not already there state.updateTokenCost({ model: newModel, input_price: input, output_price: output, onSuccess: () => onOpenChange(false), }); } else { state.updateTokenCost({ model: model, input_price: input, output_price: output, onSuccess: () => onOpenChange(false), }); } }; const onDelete = () => { // Shouldn't happen, but can't delete a prompt that hasn't been created // yet if (create) return; state.deleteTokenCosts({ model: model, onSuccess: () => onOpenChange(false), }); }; return ( { onOpenChange(x.open); }} > {create && Create model cost} {!create && ( Edit model cost: {model} )} {create && ( setNewModel(v)} required={true} /> )} setInput(v)} required={true} /> setOutput(v)} required={true} /> { // If a 'create' operation, there's nothing to delete, only // present if an existing prompt exists } {!create && ( )} ); }; export default EditDialog;