2025-07-22 11:58:10 -07:00
|
|
|
import GridLayout, { WidthProvider } from 'react-grid-layout';
|
2026-06-05 11:13:10 -07:00
|
|
|
import { useSlotRecipe, chakra, Flex, SimpleGrid } from '@chakra-ui/react';
|
2025-08-06 12:03:48 -07:00
|
|
|
import CustomResizeHandle from '../../CanvasComponents/CustomResizeHandle';
|
2025-08-08 13:00:33 -07:00
|
|
|
import FirstWidgetButton from '../../CanvasComponents/FirstWidgetButton';
|
2025-07-21 14:27:53 -07:00
|
|
|
|
2026-01-30 19:11:11 +00:00
|
|
|
export default function MyDashboard({layout, onLayoutChange, isEditable, isDelete, DeleteWidget, widgetArray, appendWidget}) {
|
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
|
|
|
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)
|
|
|
|
|
};
|
|
|
|
|
});
|
2026-06-04 15:30:01 -07:00
|
|
|
}
|
2025-07-22 13:41:32 -07:00
|
|
|
|
2025-08-11 11:31:15 -07:00
|
|
|
// Show large 'Add Widget' button when dashboard is empty
|
2025-08-11 14:17:21 -07:00
|
|
|
if ( !layout.length ) {
|
2025-08-08 13:00:33 -07:00
|
|
|
return (
|
|
|
|
|
<Flex
|
|
|
|
|
align="center"
|
|
|
|
|
justify="center"
|
2025-08-11 11:31:15 -07:00
|
|
|
h="100%"
|
2025-08-08 13:00:33 -07:00
|
|
|
w="100%"
|
|
|
|
|
bg="gray.150"
|
|
|
|
|
borderRadius="md"
|
|
|
|
|
>
|
2026-06-04 15:30:01 -07:00
|
|
|
<FirstWidgetButton appendWidget={ appendWidget } />
|
2025-08-08 13:00:33 -07:00
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 11:31:15 -07:00
|
|
|
// Show responsive grid of widgets
|
2025-08-04 12:04:33 -07:00
|
|
|
return (
|
2026-06-04 15:30:01 -07:00
|
|
|
<ResponsiveGridLayout
|
|
|
|
|
useCSSTransforms={false} // remove the sliding animation of the widgets
|
|
|
|
|
className="layout"
|
|
|
|
|
layout={layout}
|
|
|
|
|
cols={16} // Total columns in grid
|
2026-06-05 11:13:10 -07:00
|
|
|
rowHeight={40} // Height of a single row in px
|
2026-06-04 15:30:01 -07:00
|
|
|
isBounded={true} // Total width of the grid
|
|
|
|
|
margin={[15, 5]} // [horizontal, vertical] gap
|
|
|
|
|
onLayoutChange={onLayoutChange}
|
|
|
|
|
draggableHandle=".widget-handle" // Make only the header draggable
|
|
|
|
|
onDragStart={handleDragStart}
|
|
|
|
|
onDragStop={handleDragStop}
|
|
|
|
|
position="relative"
|
|
|
|
|
resizeHandle={<CustomResizeHandle isVisable={isEditable} />}
|
|
|
|
|
>
|
|
|
|
|
{ merged.map((item) => {
|
|
|
|
|
const Widget = item.widget;
|
|
|
|
|
return (
|
|
|
|
|
<Flex key={item.i} css={styles.root} boxShadow="md" height="100%" >
|
|
|
|
|
<Widget
|
|
|
|
|
id={item.i}
|
|
|
|
|
isEditable={isEditable}
|
|
|
|
|
isDelete={isDelete}
|
|
|
|
|
DeleteWidget={DeleteWidget}
|
|
|
|
|
appendWidget={appendWidget}
|
|
|
|
|
/>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</ResponsiveGridLayout>
|
2025-07-28 12:44:17 -07:00
|
|
|
);
|
2025-07-21 14:27:53 -07:00
|
|
|
}
|