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

186 lines
5.7 KiB
React
Raw Normal View History

import { HStack, Flex, Tabs, Button } from "@chakra-ui/react";
2026-06-04 15:30:01 -07:00
import { useState } from "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 { 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
2026-06-04 15:30:01 -07:00
export default function Home() {
const [isEditable, setIsEditable] = useState(false); // editable=true means static=false (vice versa)
const [isDelete, setIsDelete] = useState(false);
const [originalLayout, setOriginalLayout] = useState([]);
const [originalWidgets, setOriginalWidgets] = useState([]);
const [layout, setLayout] = useState([]);
const [widgetArray, setWidgetArray] = useState([]);
function appendWidget(name, w, h, min_w, minh, max_w, max_h) {
saveLayoutState(); // Save state before anything else
2025-07-24 12:08:10 -07:00
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;
2025-08-11 14:53:17 -07:00
setWidgetArray([ // updating widget array containing info on the types of widgets
...widgetArray,
{i: newID, widget: name }
]);
2026-06-04 15:30:01 -07:00
// onAddWidget(newID, w, h, min_w, minh, max_w, max_h); // 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,
x: (prev.length * W) % COLS,
2025-08-11 14:53:17 -07:00
y: 0,
w: w,
h: h,
2026-06-04 15:30:01 -07:00
minW: min_w,
minH: minh,
2026-06-04 15:30:01 -07:00
maxW: max_w,
maxH: max_h,
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...");
2026-06-04 15:30:01 -07:00
saveLayoutState(); // 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;
}
2026-06-04 15:30:01 -07:00
const saveLayoutState = () => {
if ( !isEditable ) {
setOriginalLayout(layout);
setOriginalWidgets(widgetArray);
}
};
const onCancel = () => {
console.log("canceling changes...");
console.log(originalWidgets);
if (originalLayout.length) {
setLayout(originalLayout.map(item => ({
...item,
static: true
})));
}
setWidgetArray(originalWidgets);
setIsEditable(false);
setIsDelete(false);
setOriginalLayout([]);
setOriginalWidgets([]);
}
const onLayoutChange = (newLayout) => setLayout(newLayout);
2025-08-04 15:38:19 -07:00
// Prop Forwarding
2026-06-04 15:30:01 -07:00
const MyDashboardProps = { layout, onLayoutChange, isEditable, saveLayoutState, isDelete, DeleteWidget, widgetArray, appendWidget };
const settingsButtonProps = { onEditWidgets, appendWidget };
2025-08-04 15:38:19 -07:00
2025-07-24 08:52:20 -07:00
return (
2026-06-04 15:30:01 -07:00
/* SUB HEADER */
<Tabs.Root width="100%" height="100%" lazyMount variant="line">
<Flex as="header" position="sticky" top="0" zIndex="dropdown" bg="base200" p={5} align="center" justify="space-between" width="100%">
{/* TABS */}
{/* <CanvasHeaderTabs tabs={DASHBOARD_TABS} />*/} {/* TODO: rename to SubHeaderTabs */}
<Tabs.List>
{DASHBOARD_TABS.map(({ value, label }) =>
<Tabs.Trigger key={value} value={value} fontSize="lg">
{ label }
</Tabs.Trigger>
)}
</Tabs.List>
{/* EDIT MODE BUTTONS */}
2025-08-05 17:12:59 -07:00
<HStack width="258px" justifyContent="flex-end">
2025-07-31 09:50:40 -07:00
{isEditable ? (
<>
<Button onClick={onSave} borderRadius="md" bg="primary" _hover={{bg: "primaryLt"}}>Save</Button>
<Button onClick={onCancel} borderRadius="md">Cancel</Button>
2025-07-31 09:50:40 -07:00
{isDelete ? (
<DeleteAllButton onDeleteAll={ DeleteAll } />
):(null)
}
</>
):(
<>
<SearchBar placeholder="Enter a volcano to search" size="sm" width="210px" />
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>
2025-07-24 08:52:20 -07:00
<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="daily">
2025-07-24 08:52:20 -07:00
<DailyActivity />
</Tabs.Content>
</Tabs.Root>
);
}