Files
Dwinzo_dev/app/src/components/ui/componets/AddButtons.tsx

318 lines
9.8 KiB
TypeScript
Raw Normal View History

2025-04-01 19:35:11 +05:30
import React, { useEffect } from "react";
2025-03-25 11:47:41 +05:30
import {
CleanPannel,
EyeIcon,
LockIcon,
} from "../../icons/RealTimeVisulationIcons";
import { panelData } from "../../../services/realTimeVisulization/zoneData/panel";
2025-03-27 09:06:26 +05:30
import { AddIcon } from "../../icons/ExportCommonIcons";
2025-04-01 11:33:10 +05:30
import { deletePanelApi } from "../../../services/realTimeVisulization/zoneData/deletePanel";
2025-04-01 19:35:11 +05:30
import { useSocketStore } from "../../../store/store";
import { clearPanel } from "../../../services/realTimeVisulization/zoneData/clearPanel";
import { lockPanel } from "../../../services/realTimeVisulization/zoneData/lockPanel";
2025-03-25 11:47:41 +05:30
// Define the type for `Side`
type Side = "top" | "bottom" | "left" | "right";
// Define the type for the props passed to the Buttons component
interface ButtonsProps {
selectedZone: {
zoneName: string;
activeSides: Side[];
panelOrder: Side[];
lockedPanels: Side[];
zoneId: string;
zoneViewPortTarget: number[];
zoneViewPortPosition: number[];
2025-03-25 11:47:41 +05:30
widgets: {
id: string;
type: string;
title: string;
panel: Side;
data: any;
}[];
};
setSelectedZone: React.Dispatch<
React.SetStateAction<{
zoneName: string;
activeSides: Side[];
panelOrder: Side[];
2025-04-01 11:33:10 +05:30
2025-03-25 11:47:41 +05:30
lockedPanels: Side[];
zoneId: string;
zoneViewPortTarget: number[];
zoneViewPortPosition: number[];
2025-03-25 11:47:41 +05:30
widgets: {
id: string;
type: string;
title: string;
panel: Side;
data: any;
}[];
}>
>;
hiddenPanels: Side[]; // Add this prop for hidden panels
setHiddenPanels: React.Dispatch<React.SetStateAction<Side[]>>; // Add this prop for updating hidden panels
}
const AddButtons: React.FC<ButtonsProps> = ({
selectedZone,
setSelectedZone,
setHiddenPanels,
hiddenPanels,
}) => {
2025-04-01 19:35:11 +05:30
const { visualizationSocket } = useSocketStore();
2025-03-25 11:47:41 +05:30
// Local state to track hidden panels
// Function to toggle lock/unlock a panel
const toggleLockPanel = async (side: Side) => {
2025-04-07 17:59:52 +05:30
console.log('side: ', side);
const email = localStorage.getItem("email") || "";
const organization = email?.split("@")[1]?.split(".")[0]; // Fallback value
2025-04-07 17:59:52 +05:30
//add api
2025-03-25 11:47:41 +05:30
const newLockedPanels = selectedZone.lockedPanels.includes(side)
? selectedZone.lockedPanels.filter((panel) => panel !== side)
: [...selectedZone.lockedPanels, side];
const updatedZone = {
...selectedZone,
lockedPanels: newLockedPanels,
};
let lockedPanel = {
organization: organization,
lockedPanel: newLockedPanels,
zoneId: selectedZone.zoneId,
};
if (visualizationSocket) {
visualizationSocket.emit("v2:viz-panel:locked", lockedPanel);
}
2025-03-25 11:47:41 +05:30
setSelectedZone(updatedZone);
// let response = await lockPanel(selectedZone.zoneId, organization, newLockedPanels)
// console.log('response: ', response);
// if (response.message === 'locked panel updated successfully') {
// // Update the selectedZone state
// setSelectedZone(updatedZone);
// }
2025-03-25 11:47:41 +05:30
};
// Function to toggle visibility of a panel
const toggleVisibility = (side: Side) => {
2025-03-25 11:47:41 +05:30
const isHidden = hiddenPanels.includes(side);
if (isHidden) {
// If the panel is already hidden, remove it from the hiddenPanels array
setHiddenPanels(hiddenPanels.filter((panel) => panel !== side));
} else {
// If the panel is visible, add it to the hiddenPanels array
setHiddenPanels([...hiddenPanels, side]);
}
};
// Function to clean all widgets from a panel
const cleanPanel = async (side: Side) => {
2025-04-07 17:59:52 +05:30
//add api
console.log('side: ', side);
const email = localStorage.getItem("email") || "";
const organization = email?.split("@")[1]?.split(".")[0]; // Fallback value
let clearPanel = {
organization: organization,
panelName: side,
zoneId: selectedZone.zoneId,
};
if (visualizationSocket) {
visualizationSocket.emit("v2:viz-panel:clear", clearPanel);
}
2025-03-25 11:47:41 +05:30
const cleanedWidgets = selectedZone.widgets.filter(
(widget) => widget.panel !== side
);
const updatedZone = {
...selectedZone,
widgets: cleanedWidgets,
};
// Update the selectedZone state
console.log('updatedZone: ', updatedZone);
2025-03-25 11:47:41 +05:30
setSelectedZone(updatedZone);
// let response = await clearPanel(selectedZone.zoneId, organization, side)
// console.log('response: ', response);
// if (response.message === 'PanelWidgets cleared successfully') {
// const cleanedWidgets = selectedZone.widgets.filter(
// (widget) => widget.panel !== side
// );
// const updatedZone = {
// ...selectedZone,
// widgets: cleanedWidgets,
// };
// // Update the selectedZone state
// setSelectedZone(updatedZone);
// }
2025-03-25 11:47:41 +05:30
};
// Function to handle "+" button click
2025-04-01 11:33:10 +05:30
const handlePlusButtonClick = async (side: Side) => {
2025-03-25 11:47:41 +05:30
if (selectedZone.activeSides.includes(side)) {
// Panel already exists: Remove widgets from that side and update activeSides
2025-04-01 11:33:10 +05:30
const email = localStorage.getItem("email") || "";
const organization = email?.split("@")[1]?.split(".")[0]; // Fallback value
2025-04-01 19:35:11 +05:30
// Remove all widgets associated with the side and update active sides
2025-03-25 11:47:41 +05:30
const cleanedWidgets = selectedZone.widgets.filter(
(widget) => widget.panel !== side
);
const newActiveSides = selectedZone.activeSides.filter((s) => s !== side);
2025-04-01 19:35:11 +05:30
2025-03-25 11:47:41 +05:30
const updatedZone = {
...selectedZone,
widgets: cleanedWidgets,
activeSides: newActiveSides,
panelOrder: newActiveSides,
};
2025-04-01 19:35:11 +05:30
let deletePanel = {
organization: organization,
panelName: side,
2025-04-04 15:31:16 +05:30
zoneId: selectedZone.zoneId,
};
2025-04-01 19:35:11 +05:30
if (visualizationSocket) {
2025-04-04 15:31:16 +05:30
visualizationSocket.emit("v2:viz-panel:delete", deletePanel);
2025-04-01 11:33:10 +05:30
}
2025-04-01 19:35:11 +05:30
setSelectedZone(updatedZone);
// API call to delete the panel
// try {
// const response = await deletePanelApi(selectedZone.zoneId, side, organization);
2025-04-04 15:31:16 +05:30
//
2025-04-01 19:35:11 +05:30
// if (response.message === "Panel deleted successfully") {
// } else {
2025-04-04 15:31:16 +05:30
//
2025-04-01 19:35:11 +05:30
// }
// } catch (error) {
2025-04-04 15:31:16 +05:30
//
2025-04-01 19:35:11 +05:30
// }
2025-03-25 11:47:41 +05:30
} else {
2025-04-04 15:31:16 +05:30
setHiddenPanels(hiddenPanels.filter((panel) => panel !== side));
// Panel does not exist: Create panel
try {
// Get email and organization safely with a default fallback
const email = localStorage.getItem("email") || "";
2025-04-01 19:35:11 +05:30
const organization = email?.split("@")[1]?.split(".")[0];
// Prevent duplicate side entries
const newActiveSides = selectedZone.activeSides.includes(side)
? [...selectedZone.activeSides]
: [...selectedZone.activeSides, side];
2025-04-01 19:35:11 +05:30
const updatedZone = {
...selectedZone,
activeSides: newActiveSides,
panelOrder: newActiveSides,
};
2025-04-01 19:35:11 +05:30
let addPanel = {
organization: organization,
zoneId: selectedZone.zoneId,
2025-04-04 15:31:16 +05:30
panelOrder: newActiveSides,
};
2025-04-01 19:35:11 +05:30
if (visualizationSocket) {
2025-04-04 15:31:16 +05:30
visualizationSocket.emit("v2:viz-panel:add", addPanel);
2025-04-01 19:35:11 +05:30
}
setSelectedZone(updatedZone);
// API call to create panels
// const response = await panelData(organization, selectedZone.zoneId, newActiveSides);
2025-04-04 15:31:16 +05:30
//
2025-04-01 19:35:11 +05:30
// if (response.message === "Panels created successfully") {
// } else {
2025-04-04 15:31:16 +05:30
//
2025-04-01 19:35:11 +05:30
// }
} catch (error) { }
2025-03-25 11:47:41 +05:30
}
};
2025-04-01 19:35:11 +05:30
2025-03-25 11:47:41 +05:30
return (
<>
<div>
{(["top", "right", "bottom", "left"] as Side[]).map((side) => (
<div key={side} className={`side-button-container ${side}`}>
{/* "+" Button */}
<button
className={`side-button ${side}${selectedZone.activeSides.includes(side) ? " active" : ""
}`}
onClick={() => handlePlusButtonClick(side)}
title={
selectedZone.activeSides.includes(side)
? `Remove all items and close ${side} panel`
: `Activate ${side} panel`
}
>
<div className="add-icon">
<AddIcon />
2025-03-25 11:47:41 +05:30
</div>
</button>
{/* Extra Buttons */}
{selectedZone.activeSides.includes(side) && (
<div className="extra-Bs">
{/* Hide Panel */}
<div
className={`icon ${hiddenPanels.includes(side) ? "active" : ""
}`}
title={
hiddenPanels.includes(side) ? "Show Panel" : "Hide Panel"
2025-03-25 14:41:51 +05:30
}
onClick={() => toggleVisibility(side)}
>
<EyeIcon
2025-04-04 15:31:16 +05:30
fill={
hiddenPanels.includes(side)
? "var(--primary-color)"
: "var(--text-color)"
}
/>
</div>
{/* Clean Panel */}
<div
className="icon"
title="Clean Panel"
onClick={() => cleanPanel(side)}
>
<CleanPannel />
</div>
{/* Lock/Unlock Panel */}
<div
className={`icon ${selectedZone.lockedPanels.includes(side) ? "active" : ""
}`}
title={
2025-03-27 09:06:26 +05:30
selectedZone.lockedPanels.includes(side)
? "Unlock Panel"
: "Lock Panel"
2025-03-27 09:06:26 +05:30
}
onClick={() => toggleLockPanel(side)}
>
<LockIcon
fill={
selectedZone.lockedPanels.includes(side)
? "var(--primary-color)"
: "var(--text-color)"
}
/>
</div>
2025-03-25 11:47:41 +05:30
</div>
)}
</div>
))}
</div>
</>
2025-03-25 11:47:41 +05:30
);
};
export default AddButtons;