import CommonDialog from "./CommonDialog"; import { useState, useEffect, useRef } from "react"; const style = { padding: "5px 2px 2px 5px", margin: "2px 0", }; export default function TextChartDialog({ open, close, addAnnotation, deleteAnnotation, popupData, }: { plotlyData: any; open: boolean; close: () => void; addAnnotation: (annotation: any) => void; updateAnnotation?: (annotation: any) => void; deleteAnnotation: (annotation: any) => void; popupData: any | null; }) { // Prevent multiple renderings const hasLoaded = useRef(false); const defaultPopupData = { text: "", color: "#0088CC", size: 18, bordercolor: "#822661", arrowcolor: "#822661", bgcolor: "#000000", arrowsize: 1, arrowwidth: 2, yanchor: "above", }; // Use a single state object to hold all form data const [formData, setFormData] = useState(defaultPopupData); const [editMode, setEditMode] = useState(false); // Handle initialization when dialog opens useEffect(() => { if (open && popupData?.annotation && !hasLoaded.current) { const annotation = popupData.annotation; // Get properties from annotation for editing let data = { text: annotation.text || "", color: annotation.font?.color || defaultPopupData.color, size: annotation.font?.size || defaultPopupData.size, bordercolor: annotation.bordercolor || defaultPopupData.bordercolor, bgcolor: annotation.bgcolor || defaultPopupData.bgcolor, arrowcolor: annotation.arrowcolor || defaultPopupData.arrowcolor, arrowsize: annotation.arrowsize || defaultPopupData.arrowsize, arrowwidth: annotation.arrowwidth || defaultPopupData.arrowwidth, yanchor: "above", }; // Determine position based on annotation coordinates if (annotation.y !== undefined && annotation.ay !== undefined) { data.yanchor = annotation.y < annotation.ay ? "above" : "below"; } setFormData(data); setEditMode(true); hasLoaded.current = true; } else if (!open) { // Reset when dialog closes setFormData(defaultPopupData); setEditMode(false); hasLoaded.current = false; } else if (open && !popupData?.annotation) { // Reset for new annotations setFormData(defaultPopupData); setEditMode(false); } }, [open, popupData]); function onChange(e: any) { const name = e.target.id.replace("addtext_", ""); let value = e.target.value; // Convert numeric values if (name === "size" || name === "arrowsize" || name === "arrowwidth") { value = parseFloat(value); } setFormData((prev: any) => ({ ...prev, [name]: value })); } function onClose() { close(); } function onSubmit() { if (formData.text) { const dataToSubmit = { ...formData }; // Add the annotation reference for editing if (editMode && popupData?.annotation) { dataToSubmit.annotation = popupData.annotation; } addAnnotation(dataToSubmit); close(); } else { if (document.getElementById("popup_textarea_warning")) { document.getElementById("popup_textarea_warning")!.style.display = "block"; } if (document.getElementById("addtext_text")) { document.getElementById("addtext_text")!.style.border = "1px solid red"; } } } function onDelete() { if (editMode && popupData) { deleteAnnotation(popupData); } close(); } return ( ); }