2025-07-22 11:58:10 -07:00
|
|
|
import GridLayout, { WidthProvider } from 'react-grid-layout';
|
2025-07-23 13:44:24 -07:00
|
|
|
import { useSlotRecipe, chakra, Flex } from '@chakra-ui/react';
|
2025-07-21 14:27:53 -07:00
|
|
|
|
2025-08-05 15:08:18 -07:00
|
|
|
export default function MyDashboard({layout, onLayoutChange, isEditable, isDelete, DeleteWidget, widgetArray}) {
|
2025-08-05 12:57:18 -07:00
|
|
|
console.log("rendering MyDashboard");
|
2025-08-04 12:04:33 -07:00
|
|
|
const recipe = useSlotRecipe({ key: "widget" });
|
|
|
|
|
const styles = recipe(); // using root recipe for widget below
|
|
|
|
|
const ResponsiveGridLayout = WidthProvider(GridLayout); // canvas width now adjusts to window size
|
|
|
|
|
const handleDragStart = () => { document.body.style.userSelect = "none"; }; // don't allow text highlighting when dragging
|
|
|
|
|
const handleDragStop = () => { document.body.style.userSelect = ""; }; // allow text highlighting all other times
|
|
|
|
|
let merged = [];
|
2025-07-22 11:58:10 -07:00
|
|
|
|
2025-08-04 12:04:33 -07:00
|
|
|
console.log("widgetArray: ", widgetArray);
|
|
|
|
|
console.log("layout: ", layout);
|
|
|
|
|
if (widgetArray.length && layout.length) {
|
|
|
|
|
const widgetMap = new Map(widgetArray.map(item => [item.i, item]));
|
|
|
|
|
merged = layout.map(item => {
|
|
|
|
|
return {
|
|
|
|
|
...item,
|
|
|
|
|
...widgetMap.get(item.i)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
};
|
2025-07-22 13:41:32 -07:00
|
|
|
|
2025-08-04 12:04:33 -07:00
|
|
|
return (
|
|
|
|
|
<chakra.div width="100%">
|
|
|
|
|
<ResponsiveGridLayout
|
|
|
|
|
className="layout"
|
|
|
|
|
layout={layout}
|
|
|
|
|
cols={12} // Total columns in grid
|
|
|
|
|
rowHeight={30} //Height of a single row in px
|
|
|
|
|
isBounded={true} // Total width of the grid
|
|
|
|
|
margin={[10, 10]} // [horizontal, vertical] gap
|
|
|
|
|
onLayoutChange={onLayoutChange}
|
|
|
|
|
draggableHandle=".widget-handle" // Make only the header draggable
|
|
|
|
|
onDragStart={handleDragStart}
|
|
|
|
|
onDragStop={handleDragStop}
|
|
|
|
|
>
|
|
|
|
|
{merged.map((item) => {
|
|
|
|
|
const Widget = item.widget;
|
|
|
|
|
return (
|
|
|
|
|
<Flex key={item.i} css={styles.root} height="100%" >
|
|
|
|
|
<Widget id={item.i} isEditable={isEditable} isDelete={isDelete} DeleteWidget={DeleteWidget}/>
|
|
|
|
|
</Flex>
|
|
|
|
|
)
|
|
|
|
|
})}
|
|
|
|
|
</ResponsiveGridLayout>
|
2025-07-28 12:44:17 -07:00
|
|
|
</chakra.div>
|
|
|
|
|
);
|
2025-07-21 14:27:53 -07:00
|
|
|
}
|