Files
Web-Frontend/client/src/components/Canvas/views/Home.jsx
T

173 lines
4.9 KiB
React
Raw Normal View History

2025-07-31 09:50:40 -07:00
import { HStack, Flex, Tabs } from "@chakra-ui/react";
2025-08-05 15:08:18 -07:00
import MyDashboard from "./HomeTabs/MyDashboard";
2025-08-05 12:57:18 -07:00
import { SearchBar } from "../CanvasComponents/SearchBar";
import { SettingsButton } from "../CanvasComponents/SettingsButton";
import { DailyActivity } from "./HomeTabs/DailyActivity";
import { Gas } from "./HomeTabs/Gas";
import { Seismic } from "./HomeTabs/Seismic";
import { RemoteSensing } from "./HomeTabs/RemoteSensing";
import { Deformation } from "./HomeTabs/Deformation"
2025-08-05 12:57:18 -07:00
import { CancelButton } from "../CanvasComponents/CancelButton";
import { SaveButton } from "../CanvasComponents/SaveButton";
import DeleteAllButton from "../CanvasComponents/DeleteAllButton";
2025-08-06 16:27:08 -07:00
import { v4 as uuid } from 'uuid';
import { CanvasHeaderTabs } from '../CanvasComponents/CanvasHeaderTabs';
import { DASHBOARD_TABS } from "@/constants/viewKeys";
2025-08-06 16:27:08 -07:00
2025-08-12 15:27:33 -07:00
export default function Home(props) {
2025-08-04 15:38:19 -07:00
// deconstructing props
const { view, layout, setLayout, setOriginalLayout, onLayoutChange, onChangeView, onCancel, setOriginalWidgets,
isDelete, setIsDelete, beginEditIfNeeded, isEditable, setIsEditable, widgetArray, setWidgetArray } = props;
2025-07-24 12:08:10 -07:00
function appendWidget(name, w, h, minw, minh, maxw, maxh) {
beginEditIfNeeded(); // Save state before anything else
const newID = uuid(); // could be replaced with something else like guid
2025-08-11 14:53:17 -07:00
const COLS = 16;
const W = w ?? 3;
const H = h ?? 3;
setWidgetArray([ // updating widget array containing info on the types of widgets
...widgetArray,
{i: newID, widget: name }
]);
// onAddWidget(newID, w, h, minw, minh, maxw, maxh); // updating array containing info on layout of widgets
2025-07-24 12:08:10 -07:00
setLayout(prev => [
2025-08-04 12:04:33 -07:00
...prev,
{
i: newID,
2025-08-11 14:53:17 -07:00
x: (prev.length * W) % COLS,
y: 0,
2025-08-06 16:27:08 -07:00
w: w,
h: h,
minW: minw,
minH: minh,
maxW: maxw,
maxH: maxh,
2025-08-06 12:03:48 -07:00
static: false,
resizeHandles: ['se']
},
2025-07-24 12:08:10 -07:00
]);
2025-08-06 16:27:08 -07:00
}
const onSave = () => {
2025-07-28 12:44:17 -07:00
console.log("saving widgets...");
// Set all widgets back to static: true
setLayout(prevLayout =>
prevLayout.map(item => ({
...item,
static: true
}))
);
setIsEditable(false);
2025-07-28 12:44:17 -07:00
setIsDelete(false);
setOriginalLayout([]); // Clear backup layout
setOriginalWidgets([]);
2025-07-24 16:05:42 -07:00
}
2025-08-12 15:27:33 -07:00
const onEditWidgets = () => {
2025-07-28 12:44:17 -07:00
console.log("Start deleting widgets...");
beginEditIfNeeded(); // Save state before anything else
2025-08-12 15:27:33 -07:00
setLayout(prevLayout =>
prevLayout.map(item => ({
...item,
static: false
}))
);
2025-07-28 12:44:17 -07:00
setIsEditable(true);
setIsDelete(true);
}
2025-08-04 15:38:19 -07:00
const DeleteWidget = (ID) => {
console.log("Deleting widget with id: ", ID);
setLayout(layout => layout.filter(w => w.i !== ID));
2025-08-05 15:19:46 -07:00
setWidgetArray(widgetArray => widgetArray.filter(w => w.i !== ID));
2025-07-24 12:08:10 -07:00
}
2025-07-31 09:50:40 -07:00
const DeleteAll = () => {
const confirm = window.confirm(
"Are you sure you want to delete all widgets?"
);
if (confirm) {
setLayout([]);
2025-08-04 12:04:33 -07:00
setWidgetArray([]);
2025-07-31 09:50:40 -07:00
onSave();
}
else return;
}
2025-08-04 15:38:19 -07:00
// Prop Forwarding
const MyDashboardProps = { layout, onLayoutChange, isEditable, beginEditIfNeeded, isDelete, DeleteWidget, widgetArray, appendWidget };
2025-08-12 15:27:33 -07:00
const settingsButtonProps = { onEditWidgets, appendWidget };
2025-08-04 15:38:19 -07:00
2025-07-24 08:52:20 -07:00
return (
2025-07-24 12:08:10 -07:00
/* HEADER */
<Tabs.Root
2025-08-05 17:12:59 -07:00
width="100%"
height="100%"
lazyMount
value={ view }
2025-08-04 15:38:19 -07:00
variant="line"
2025-07-29 12:08:36 -07:00
// Prevent navigation away from 'My Dashboard' while in Edit mode
onValueChange={(e) => {
const nextView = e.value;
onChangeView(nextView);
}}
>
2025-07-24 08:52:20 -07:00
<Flex
as="header"
position="sticky"
top="0"
zIndex="dropdown" // zIndex: 1100
2025-07-24 08:52:20 -07:00
bg="base200"
pl={5}
pr={2}
2025-07-29 11:08:36 -07:00
pt={3}
2025-08-06 16:25:54 -07:00
pb={2}
2025-07-24 08:52:20 -07:00
align="center"
justify="space-between"
2025-08-05 17:12:59 -07:00
width="100%"
2025-07-24 08:52:20 -07:00
>
<CanvasHeaderTabs tabs={DASHBOARD_TABS} />
2025-08-05 17:12:59 -07:00
<HStack width="258px" justifyContent="flex-end">
2025-07-31 09:50:40 -07:00
{isEditable ? (
<>
2025-07-31 09:50:40 -07:00
<SaveButton onSave={ onSave } />
<CancelButton onCancel={ onCancel } />
{isDelete ? (
<DeleteAllButton onDeleteAll={ DeleteAll } />
):(null)
}
</>
):(
<>
<SearchBar />
2025-08-04 15:38:19 -07:00
<SettingsButton {...settingsButtonProps}/>
</>
)}
2025-07-24 08:52:20 -07:00
</HStack>
</Flex>
2025-07-24 12:08:10 -07:00
{/* CANVAS */}
2025-08-05 15:08:18 -07:00
<Tabs.Content value="mydashboard">
<MyDashboard {...MyDashboardProps}/>
2025-07-24 08:52:20 -07:00
</Tabs.Content>
<Tabs.Content value="gas">
<Gas />
</Tabs.Content>
<Tabs.Content value="seismic">
<Seismic />
</Tabs.Content>
<Tabs.Content value="remote">
2025-07-24 08:52:20 -07:00
<RemoteSensing />
</Tabs.Content>
<Tabs.Content value="deformation">
<Deformation />
</Tabs.Content>
<Tabs.Content value="daily">
2025-07-24 08:52:20 -07:00
<DailyActivity />
</Tabs.Content>
</Tabs.Root>
);
}