Refactor: moved Widget and CanvasHeader code into Canvas.jsx to handle both as a parent element

This commit is contained in:
Daniel S. Hansen
2025-07-21 13:01:16 -07:00
parent 06edb481e9
commit a1f2fcc9be
3 changed files with 153 additions and 70 deletions
+35 -16
View File
@@ -1,15 +1,34 @@
import { useState } from 'react' import { useState } from 'react'
import Header from './components/layout/Header/Header.jsx'; import Header from './components/layout/Header/Header.jsx';
import Sidebar from './components/layout/Sidebar/Sidebar.jsx'; import Sidebar from './components/layout/Sidebar/Sidebar.jsx';
import Widget from './components/layout/Canvas/Widget.jsx' import Canvas from './components/layout/Canvas/Canvas.jsx';
import { Grid, GridItem, useRecipe } from "@chakra-ui/react" import { Grid, GridItem, useRecipe, Box } from '@chakra-ui/react';
import CanvasHeader from './components/layout/Canvas/CanvasHeader.jsx'
// REMOVE AFTER TESTING
import CanvasHeader from './components/layout/Canvas/CanvasHeader.jsx';
import { GridLayout } from 'react-grid-layout';
function App() { function App() {
const recipe = useRecipe({ key: "canvas" }); const recipe = useRecipe({ key: "canvas" });
const styles = recipe(); const styles = recipe();
// TEST TEST TEST
// return (
// <Box>
// <Canvas /> {/* ← This should render something visible */}
// {/* <GridLayout>
// {layout.map((item) => (
// <div key={item.i}>
// <Widget title="VDAP Widget" />
// </div>
// ))}
// </GridLayout> */}
// </Box>
// );
// }
return ( return (
<Grid css={styles} <Grid css={styles}
templateRows="1fr 11fr" templateRows="1fr 11fr"
@@ -24,19 +43,19 @@ function App() {
<Sidebar /> <Sidebar />
</GridItem> </GridItem>
<GridItem colSpan={1}> <GridItem colSpan={1}>
<Grid {/* <Grid
templateRows="repeat(12, 1fr)" templateRows="repeat(12, 1fr)"
templateColumns="repeat(4, 1fr)" templateColumns="repeat(4, 1fr)"
height="100%" height="100%"
width="100%" width="100%"
> > */}
<GridItem colSpan={4} rowSpan={1}> <GridItem colSpan={1}>
<CanvasHeader /> <Canvas />
</GridItem> </GridItem>
<GridItem colSpan={4} rowSpan={5}> {/* <GridItem colSpan={4} rowSpan={5}>
<Widget /> <Canvas />
</GridItem> </GridItem> */}
</Grid> {/* </Grid> */}
</GridItem> </GridItem>
</Grid> </Grid>
); );
@@ -0,0 +1,45 @@
// This component defines a generic Widget that can be dynamically resized and
// dragged inside the canvas
// Content not included, must be populated using custom charts plots and notifications
import { useState } from 'react';
import CanvasHeader from './CanvasHeader';
import GridLayout from 'react-grid-layout';
import Widget from './Widget.jsx';
import { Box } from '@chakra-ui/react';
export default function Canvas() {
// Define widgets initial position and size
// TODO Add dynamic adding/deleting of widgets via options button
const [layout, setLayout] = useState([
{ i: 'myWidget1', x: 0, y: 0, w: 3, h: 3, minH: 3, static: false },
{ i: 'myWidget2', x: 3, y: 0, w: 3, h: 3, minH: 3, static: false },
{ i: 'myWidget3', x: 6, y: 0, w: 3, h: 3, minH: 3, static: false }
]);
const onLayoutChange = (newLayout) => {
setLayout(newLayout);
console.log('Updated layout:', newLayout)
};
return (
<Box height="100%" width="100%">
<CanvasHeader />
<GridLayout
className="layout"
layout={layout}
cols={12} // Total columns in grid
rowHeight={30} //Height of a single row in px
width={1200} // Total width of the grid
margin={[10, 10]} // [horizontal, vertical] gap
onLayoutChange={onLayoutChange}
draggableHandle=".widget-handle" // Make only the header draggable
>
{layout.map((item) => (
<div key={item.i} style={{ background: '#f0f0f0', borderRadius: 4 }}>
<Widget title="VDAP Widget" />
</div>
))}
</GridLayout>
</Box>
);
}
+73 -54
View File
@@ -1,57 +1,76 @@
// This component defines a generic Widget that can be dynamically resized and // Widget.jsx
// dragged inside the canvas export default function Widget({ title }) {
// Content not included, must be populated using custom charts plots and notifications
import { useState } from 'react';
import GridLayout from 'react-grid-layout';
export default function Widget() {
// Define widgets initial position and size
// TODO Add dynamic adding/deleting of widgets via options button
const [layout, setLayout] = useState([
{ i: 'myWidget1', x: 0, y: 0, w: 3, h: 3, minH: 3, static: false },
{ i: 'myWidget2', x: 3, y: 0, w: 3, h: 3, minH: 3, static: false },
{ i: 'myWidget3', x: 6, y: 0, w: 3, h: 3, minH: 3, static: false }
]);
const onLayoutChange = (newLayout) => {
setLayout(newLayout);
console.log('Updated layout:', newLayout)
};
return ( return (
<GridLayout <>
className="layout" <div
layout={layout} className="widget-handle"
cols={12} // Total columns in grid style={{ padding: '4px', cursor: 'move', background: '#ddd' }}
rowHeight={30} //Height of a single row in px
width={1200} // Total width of the grid
margin={[10, 10]} // [horizontal, vertical] gap
onLayoutChange={onLayoutChange}
draggableHandle=".widget-handle" // Make only the header draggable
> >
<div key="myWidget1" style={{ background: '#f0f0f0', borderRadius: 4 }}> {title}
<div className="widget-handle" style={{ padding: '4px', cursor: 'move', background: '#ddd' }}> </div>
VDAP Widget <div style={{ padding: '8px' }}>
</div> This is the widget content - drag or resize me!
<div style={{ padding: '8px' }}> </div>
This is the widget content - drag or resize me! </>
</div>
</div><div key="myWidget2" style={{ background: '#f0f0f0', borderRadius: 4 }}>
<div className="widget-handle" style={{ padding: '4px', cursor: 'move', background: '#ddd' }}>
VDAP Widget
</div>
<div style={{ padding: '8px' }}>
This is the widget content - drag or resize me!
</div>
</div><div key="myWidget3" style={{ background: '#f0f0f0', borderRadius: 4 }}>
<div className="widget-handle" style={{ padding: '4px', cursor: 'move', background: '#ddd' }}>
VDAP Widget
</div>
<div style={{ padding: '8px' }}>
This is the widget content - drag or resize me!
</div>
</div>
</GridLayout>
); );
} }
// The below code has been moved to Canvas.jsx
// // This component defines a generic Widget that can be dynamically resized and
// // dragged inside the canvas
// // Content not included, must be populated using custom charts plots and notifications
// import { useState } from 'react';
// import GridLayout from 'react-grid-layout';
// export default function Widget() {
// // Define widgets initial position and size
// // TODO Add dynamic adding/deleting of widgets via options button
// const [layout, setLayout] = useState([
// { i: 'myWidget1', x: 0, y: 0, w: 3, h: 3, minH: 3, static: false },
// { i: 'myWidget2', x: 3, y: 0, w: 3, h: 3, minH: 3, static: false },
// { i: 'myWidget3', x: 6, y: 0, w: 3, h: 3, minH: 3, static: false }
// ]);
// const onLayoutChange = (newLayout) => {
// setLayout(newLayout);
// console.log('Updated layout:', newLayout)
// };
// return (
// <GridLayout
// className="layout"
// layout={layout}
// cols={12} // Total columns in grid
// rowHeight={30} //Height of a single row in px
// width={1200} // Total width of the grid
// margin={[10, 10]} // [horizontal, vertical] gap
// onLayoutChange={onLayoutChange}
// draggableHandle=".widget-handle" // Make only the header draggable
// >
// <div key="myWidget1" style={{ background: '#f0f0f0', borderRadius: 4 }}>
// <div className="widget-handle" style={{ padding: '4px', cursor: 'move', background: '#ddd' }}>
// VDAP Widget
// </div>
// <div style={{ padding: '8px' }}>
// This is the widget content - drag or resize me!
// </div>
// </div><div key="myWidget2" style={{ background: '#f0f0f0', borderRadius: 4 }}>
// <div className="widget-handle" style={{ padding: '4px', cursor: 'move', background: '#ddd' }}>
// VDAP Widget
// </div>
// <div style={{ padding: '8px' }}>
// This is the widget content - drag or resize me!
// </div>
// </div><div key="myWidget3" style={{ background: '#f0f0f0', borderRadius: 4 }}>
// <div className="widget-handle" style={{ padding: '4px', cursor: 'move', background: '#ddd' }}>
// VDAP Widget
// </div>
// <div style={{ padding: '8px' }}>
// This is the widget content - drag or resize me!
// </div>
// </div>
// </GridLayout>
// );
// }