import { useState, useEffect, useRef } from "react"; import { useWidgetStore } from "../../../../../store/useWidgetStore"; import ChartComponent from "../../../sidebarLeft/visualization/widgets/ChartComponent"; import RegularDropDown from "../../../../ui/inputs/RegularDropDown"; import { WalletIcon } from "../../../../icons/3dChartIcons"; import SimpleCard from "../../../../ui/realTimeVis/floating/SimpleCard"; interface Widget { id: string; type?: string; panel: "top" | "bottom" | "left" | "right"; title?: string; header?: string; fontFamily?: string; fontSize?: string; fontWeight?: string; className?: string; data?: { labels: string[]; datasets: { data: number[]; backgroundColor: string; borderColor: string; borderWidth: number; }[]; }; value?: string; per?: string; } interface ChartElement { tagName: string; className: string; textContent: string; selector: string; } const Design = () => { const [selectedFont, setSelectedFont] = useState("drop down"); const [selectedSize, setSelectedSize] = useState("drop down"); const [selectedWeight, setSelectedWeight] = useState("drop down"); const [elementColor, setElementColor] = useState("#6f42c1"); const [showColorPicker, setShowColorPicker] = useState(false); const [chartElements, setChartElements] = useState([]); const [selectedElementToStyle, setSelectedElementToStyle] = useState< string | null >(null); const [nameInput, setNameInput] = useState(""); const chartRef = useRef(null); const { selectedChartId, setSelectedChartId, widgets, setWidgets } = useWidgetStore(); // Initialize name input and extract elements when selectedChartId changes useEffect(() => { setNameInput(selectedChartId?.header || selectedChartId?.title || ""); if (!chartRef.current) return; const timer = setTimeout(() => { const chartContainer = chartRef.current; if (!chartContainer) return; const elements = Array.from(chartContainer.querySelectorAll("*")) .filter((el) => { const tagName = el.tagName.toLowerCase(); return !["script", "style", "meta", "link", "head"].includes(tagName); }) .map((el, index) => { const tagName = el.tagName.toLowerCase(); const className = typeof el.className === "string" ? el.className : ""; const textContent = el.textContent?.trim() || ""; let selector = tagName; if (className && typeof className === "string") { const classList = className .split(/\s+/) .filter((c) => c.length > 0); if (classList.length > 0) { selector += "." + classList.join("."); } } if (!className || className.trim() === "") { const parent = el.parentElement; if (parent) { const siblings = Array.from(parent.children).filter( (child) => child.tagName.toLowerCase() === tagName ); const position = siblings.indexOf(el) + 1; selector += `:nth-of-type(${position})`; } } return { tagName, className, textContent, selector, }; }); setChartElements(elements); }, 300); return () => clearTimeout(timer); }, [selectedChartId]); const applyStyles = () => { if (!selectedElementToStyle || !chartRef.current) return; const element = chartRef.current.querySelector(selectedElementToStyle); if (!element) return; const elementToStyle = element as HTMLElement; if (selectedFont !== "drop down") { elementToStyle.style.fontFamily = selectedFont; } if (selectedSize !== "drop down") { elementToStyle.style.fontSize = selectedSize; } if (selectedWeight !== "drop down") { elementToStyle.style.fontWeight = selectedWeight.toLowerCase(); } if (elementColor) { elementToStyle.style.color = elementColor; } }; useEffect(() => { applyStyles(); }, [ selectedFont, selectedSize, selectedWeight, elementColor, selectedElementToStyle, ]); const handleUpdateWidget = (updatedProperties: Partial) => { if (!selectedChartId) return; const updatedChartId = { ...selectedChartId, ...updatedProperties, }; setSelectedChartId(updatedChartId); const updatedWidgets = widgets.map((widget) => widget.id === selectedChartId.id ? { ...widget, ...updatedProperties } : widget ); setWidgets(updatedWidgets); }; const handleNameChange = (e: React.ChangeEvent) => { const newName = e.target.value; setNameInput(newName); if (selectedChartId?.title) { handleUpdateWidget({ title: newName }); } else if (selectedChartId?.header) { handleUpdateWidget({ header: newName }); } }; const defaultChartData = { labels: ["January", "February", "March", "April", "May", "June", "July"], datasets: [ { data: [65, 59, 80, 81, 56, 55, 40], backgroundColor: "#6f42c1", borderColor: "#b392f0", borderWidth: 1, }, ], }; const elementOptions = chartElements.map((el) => { let displayName = el.tagName; if (el.className) displayName += `.${el.className}`; if (el.textContent) displayName += ` (${el.textContent.substring(0, 20)}${ el.textContent.length > 20 ? "..." : "" })`; return { display: displayName, value: el.selector, }; }); return (
{selectedChartId?.title || selectedChartId?.header || "Widget 1"}
{selectedChartId?.title ? ( ) : ( )}
Element to Style 0 ? elementOptions.map((opt) => opt.display) : ["No elements found"] } onSelect={(value) => { const selected = elementOptions.find( (opt) => opt.display === value ); setSelectedElementToStyle(selected?.value || null); }} />
Name
{selectedChartId?.title && (
Chart Type { handleUpdateWidget({ type: value }); }} />
)}
Font Family setSelectedFont(value)} />
Size setSelectedSize(value)} />
Weight setSelectedWeight(value)} />
setShowColorPicker((prev) => !prev)} > Element Color
{showColorPicker && (
{ setElementColor(e.target.value); if (selectedChartId?.data) { handleUpdateWidget({ data: { ...selectedChartId.data, datasets: [ { ...selectedChartId.data.datasets[0], backgroundColor: e.target.value, }, ], }, }); } }} /> {elementColor}
)}
); }; export default Design;