Dwinzo/frontend/src/components/ui/sideBar/realTimeViz/design.tsx

162 lines
4.7 KiB
TypeScript
Raw Normal View History

2025-03-14 07:27:01 +00:00
import React, { useEffect, useState } from "react";
import RegularDropDown from "../../inputs/regularDropDown";
import { useWidgetStore } from "../../../../store/store";
import ChartComponent from "./chartComponent";
2025-03-18 04:21:33 +00:00
// Define Props Interface
2025-03-14 07:27:01 +00:00
interface Widget {
id: string;
2025-03-18 04:21:33 +00:00
type: string; // Chart type (e.g., "bar", "line")
panel: "top" | "bottom" | "left" | "right"; // Panel location
2025-03-14 07:27:01 +00:00
title: string;
fontFamily?: string;
fontSize?: string;
fontWeight?: string;
2025-03-18 04:21:33 +00:00
data: {
labels: string[];
datasets: {
data: number[];
backgroundColor: string;
borderColor: string;
borderWidth: number;
}[];
}; // Data for the chart
2025-03-14 07:27:01 +00:00
}
const Design = () => {
const [selectedName, setSelectedName] = useState("drop down");
const [selectedElement, setSelectedElement] = useState("drop down");
const [selectedFont, setSelectedFont] = useState("drop down");
const [selectedSize, setSelectedSize] = useState("drop down");
const [selectedWeight, setSelectedWeight] = useState("drop down");
2025-03-18 04:21:33 +00:00
// Zustand Store Hooks
2025-03-14 07:27:01 +00:00
const { selectedChartId, setSelectedChartId, widgets, setWidgets } =
2025-03-18 04:21:33 +00:00
useWidgetStore();
2025-03-14 07:27:01 +00:00
2025-03-18 04:21:33 +00:00
// Log Selected Chart ID for Debugging
2025-03-14 07:27:01 +00:00
2025-03-18 04:21:33 +00:00
// Handle Widget Updates
2025-03-14 07:27:01 +00:00
const handleUpdateWidget = (updatedProperties: Partial<Widget>) => {
2025-03-18 04:21:33 +00:00
if (!selectedChartId) return;
// Update the selectedChartId
const updatedChartId = {
...selectedChartId,
...updatedProperties,
};
setSelectedChartId(updatedChartId);
2025-03-14 07:27:01 +00:00
// Update the widgets array
const updatedWidgets = widgets.map((widget) =>
2025-03-18 04:21:33 +00:00
widget.id === selectedChartId.id
? { ...widget, ...updatedProperties }
2025-03-14 07:27:01 +00:00
: widget
);
setWidgets(updatedWidgets);
};
2025-03-18 04:21:33 +00:00
// Default Chart Data
const defaultChartData = {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"],
datasets: [
{
data: [65, 59, 80, 81, 56, 55, 40],
backgroundColor: "#5c87df",
borderColor: "#ffffff",
borderWidth: 1,
},
],
};
2025-03-14 07:27:01 +00:00
return (
<div className="design">
2025-03-18 04:21:33 +00:00
{/* Title of the Selected Widget */}
2025-03-14 07:27:01 +00:00
<div className="selectedWidget">
2025-03-18 04:21:33 +00:00
{selectedChartId?.title || "Widget 1"}
2025-03-14 07:27:01 +00:00
</div>
2025-03-18 04:21:33 +00:00
{/* Chart Component */}
2025-03-14 07:27:01 +00:00
<div className="reviewChart">
{selectedChartId && (
2025-03-14 07:27:01 +00:00
<ChartComponent
2025-03-18 04:21:33 +00:00
type={selectedChartId.type}
title={selectedChartId.title}
data={selectedChartId.data || defaultChartData} // Use widget data or default
2025-03-14 07:27:01 +00:00
/>
)}
2025-03-14 07:27:01 +00:00
</div>
2025-03-18 04:21:33 +00:00
{/* Options Container */}
2025-03-14 07:27:01 +00:00
<div className="optionsContainer">
2025-03-18 04:21:33 +00:00
{/* Name Dropdown */}
2025-03-14 07:27:01 +00:00
<div className="option">
<span>Name</span>
<RegularDropDown
2025-03-18 04:21:33 +00:00
header={selectedChartId?.title || "Select Name"}
2025-03-14 07:27:01 +00:00
options={["Option 1", "Option 2", "Option 3"]}
onSelect={(value) => {
setSelectedName(value);
handleUpdateWidget({ title: value });
}}
/>
</div>
2025-03-18 04:21:33 +00:00
{/* Element Dropdown */}
2025-03-14 07:27:01 +00:00
<div className="option">
<span>Element</span>
<RegularDropDown
2025-03-18 04:21:33 +00:00
header={selectedChartId?.type || "Select Element"}
options={["bar", "line", "pie", "doughnut", "radar", "polarArea"]} // Valid chart types
2025-03-14 07:27:01 +00:00
onSelect={(value) => {
setSelectedElement(value);
2025-03-18 04:21:33 +00:00
handleUpdateWidget({ type: value });
2025-03-14 07:27:01 +00:00
}}
/>
</div>
2025-03-18 04:21:33 +00:00
{/* Font Family Dropdown */}
2025-03-14 07:27:01 +00:00
<div className="option">
<span>Font Family</span>
<RegularDropDown
2025-03-18 04:21:33 +00:00
header={selectedChartId?.fontFamily || "Select Font"}
2025-03-14 07:27:01 +00:00
options={["Arial", "Roboto", "Sans-serif"]}
onSelect={(value) => {
setSelectedFont(value);
handleUpdateWidget({ fontFamily: value });
}}
/>
</div>
2025-03-18 04:21:33 +00:00
{/* Size Dropdown */}
2025-03-14 07:27:01 +00:00
<div className="option">
<span>Size</span>
<RegularDropDown
2025-03-18 04:21:33 +00:00
header={selectedChartId?.fontSize || "Select Size"}
2025-03-14 07:27:01 +00:00
options={["12px", "14px", "16px", "18px"]}
onSelect={(value) => {
setSelectedSize(value);
handleUpdateWidget({ fontSize: value });
}}
/>
</div>
2025-03-18 04:21:33 +00:00
{/* Weight Dropdown */}
2025-03-14 07:27:01 +00:00
<div className="option">
<span>Weight</span>
<RegularDropDown
2025-03-18 04:21:33 +00:00
header={selectedChartId?.fontWeight || "Select Weight"}
2025-03-14 07:27:01 +00:00
options={["Light", "Regular", "Bold"]}
onSelect={(value) => {
setSelectedWeight(value);
handleUpdateWidget({ fontWeight: value });
}}
/>
</div>
</div>
</div>
);
};
export default Design;