Refactor making changes to home and how widgets are altered
This commit is contained in:
+16
-3
@@ -1,6 +1,6 @@
|
||||
import 'leaflet/dist/leaflet.css';
|
||||
import { Routes, Route, Navigate, Outlet } from 'react-router-dom';
|
||||
import { useRecipe, Grid, GridItem, useDialog } from "@chakra-ui/react";
|
||||
import { useRecipe, Grid, GridItem, useDialog, Flex, Heading } from "@chakra-ui/react";
|
||||
import { createContext } from "react";
|
||||
import Login from "@/Login.jsx";
|
||||
import Sidebar from "./components/Sidebar/Sidebar.jsx";
|
||||
@@ -44,7 +44,11 @@ function AppFoundation() {
|
||||
// if (user) return children;
|
||||
// else return <Navigate to="/login" replace />;
|
||||
// }
|
||||
|
||||
const Placeholder = ({ name }) => (
|
||||
<Flex width="100%" height="100%" align="center" justify="center" bg="gray.50"_dark={{ bg: "gray.900" }}>
|
||||
<Heading color="gray.400" _dark={{ bg: "gray.600" }} size="md">{name} Dashboard - Coming Soon</Heading>
|
||||
</Flex>
|
||||
);
|
||||
function App() {
|
||||
const sidebarContext = createContext(["Home", "Global Map", "Regional Map", "Volcano", "Admin"]);
|
||||
// const [user, setUser] = useState(null);
|
||||
@@ -56,7 +60,16 @@ function App() {
|
||||
|
||||
{/*Non-Public Routes*/}
|
||||
<Route element={<AppFoundation />}>
|
||||
<Route path="/home" element={<Home />} />
|
||||
|
||||
<Route path="/home" element={<Home />}>
|
||||
{/* We use our "pass" component to fill the Outlet until the real pages are built */}
|
||||
<Route index element={<Placeholder name="My" />} />
|
||||
<Route path="gas" element={<Placeholder name="Gas" />} />
|
||||
<Route path="seismic" element={<Placeholder name="Seismic" />} />
|
||||
<Route path="remote" element={<Placeholder name="Remote Sensing" />} />
|
||||
<Route path="daily" element={<Placeholder name="Daily Activity" />} />
|
||||
</Route>
|
||||
|
||||
<Route path="/global-map" element={<GlobalMap />} />
|
||||
<Route path="/regional-map" element={<RegionalMap />} />
|
||||
<Route path="/volcano" element={<Volcano />} />
|
||||
|
||||
@@ -1,186 +1,64 @@
|
||||
import { HStack, Flex, Tabs, Button } from "@chakra-ui/react";
|
||||
import { useState } from "react";
|
||||
import MyDashboard from "./HomeTabs/MyDashboard";
|
||||
import { HStack, Flex, Box } from "@chakra-ui/react";
|
||||
import { NavLink, Outlet, useLocation } from "react-router-dom";
|
||||
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";
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { CanvasHeaderTabs } from '../CanvasComponents/CanvasHeaderTabs';
|
||||
import { DASHBOARD_TABS } from "@/constants/viewKeys";
|
||||
|
||||
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([]);
|
||||
const location = useLocation();
|
||||
const isPersonalDashboard = location.pathname === "/home" || location.pathname === "/home/";
|
||||
|
||||
function appendWidget(name, w, h, min_w, minh, max_w, max_h) {
|
||||
saveLayoutState(); // Save state before anything else
|
||||
|
||||
const newID = uuid(); // could be replaced with something else like guid
|
||||
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, min_w, minh, max_w, max_h); // updating array containing info on layout of widgets
|
||||
|
||||
setLayout(prev => [
|
||||
...prev,
|
||||
{
|
||||
i: newID,
|
||||
x: (prev.length * W) % COLS,
|
||||
y: 0,
|
||||
w: w,
|
||||
h: h,
|
||||
minW: min_w,
|
||||
minH: minh,
|
||||
maxW: max_w,
|
||||
maxH: max_h,
|
||||
static: false,
|
||||
resizeHandles: ['se']
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
const onSave = () => {
|
||||
console.log("saving widgets...");
|
||||
// Set all widgets back to static: true
|
||||
setLayout(prevLayout =>
|
||||
prevLayout.map(item => ({
|
||||
...item,
|
||||
static: true
|
||||
}))
|
||||
);
|
||||
setIsEditable(false);
|
||||
setIsDelete(false);
|
||||
setOriginalLayout([]); // Clear backup layout
|
||||
setOriginalWidgets([]);
|
||||
}
|
||||
|
||||
const onEditWidgets = () => {
|
||||
console.log("Start deleting widgets...");
|
||||
saveLayoutState(); // Save state before anything else
|
||||
setLayout(prevLayout =>
|
||||
prevLayout.map(item => ({
|
||||
...item,
|
||||
static: false
|
||||
}))
|
||||
);
|
||||
setIsEditable(true);
|
||||
setIsDelete(true);
|
||||
}
|
||||
|
||||
const DeleteWidget = (ID) => {
|
||||
console.log("Deleting widget with id: ", ID);
|
||||
setLayout(layout => layout.filter(w => w.i !== ID));
|
||||
setWidgetArray(widgetArray => widgetArray.filter(w => w.i !== ID));
|
||||
}
|
||||
|
||||
const DeleteAll = () => {
|
||||
const confirm = window.confirm(
|
||||
"Are you sure you want to delete all widgets?"
|
||||
);
|
||||
if (confirm) {
|
||||
setLayout([]);
|
||||
setWidgetArray([]);
|
||||
onSave();
|
||||
}
|
||||
else return;
|
||||
}
|
||||
|
||||
|
||||
const saveLayoutState = () => {
|
||||
if ( !isEditable ) {
|
||||
setOriginalLayout(layout);
|
||||
setOriginalWidgets(widgetArray);
|
||||
// 1. 🎨 CSS Magic: Replicates Chakra's `<Tabs variant="line">` exactly
|
||||
const tabStyles = {
|
||||
fontSize: "lg",
|
||||
fontWeight: "normal",
|
||||
color: "fg.muted",
|
||||
px: 4, // Horizontal padding
|
||||
py: 2, // Vertical padding
|
||||
borderBottom: "2px solid transparent",
|
||||
mb: "-2px", // 🔥 The trick: Pulls the active border down to overlap the container's line
|
||||
transition: "all 0.2s",
|
||||
_hover: { color: "fg" },
|
||||
_activeLink: {
|
||||
color: "color", // Or whatever your primary text color is
|
||||
borderColor: "currentColor", // The active underline
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
|
||||
// Prop Forwarding
|
||||
const MyDashboardProps = { layout, onLayoutChange, isEditable, saveLayoutState, isDelete, DeleteWidget, widgetArray, appendWidget };
|
||||
const settingsButtonProps = { onEditWidgets, appendWidget };
|
||||
|
||||
return (
|
||||
/* 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>
|
||||
<Flex direction="column" height="100%" width="100%">
|
||||
|
||||
{/* EDIT MODE BUTTONS */}
|
||||
<HStack width="258px" justifyContent="flex-end">
|
||||
{isEditable ? (
|
||||
<>
|
||||
<Button onClick={onSave} borderRadius="md" bg="primary" _hover={{bg: "primaryLt"}}>Save</Button>
|
||||
<Button onClick={onCancel} borderRadius="md">Cancel</Button>
|
||||
{isDelete ? (
|
||||
<DeleteAllButton onDeleteAll={ DeleteAll } />
|
||||
):(null)
|
||||
}
|
||||
</>
|
||||
):(
|
||||
<>
|
||||
<SearchBar placeholder="Enter a volcano to search" size="sm" width="210px" />
|
||||
<SettingsButton {...settingsButtonProps}/>
|
||||
</>
|
||||
)}
|
||||
</HStack>
|
||||
{/* 2. 🛠️ SUB HEADER: Matches your original padding and background */}
|
||||
<Flex as="header" position="sticky" top="0" zIndex="dropdown" bg="base200" p={5} align="center" justify="space-between" width="100%">
|
||||
|
||||
{/* TABS CONTAINER - Replicates the Tabs.List line container */}
|
||||
<Flex borderBottom="2px solid" borderColor="border.muted" gap={2}>
|
||||
<Box as={NavLink} to="/home" end css={tabStyles}>My Dashboard</Box>
|
||||
<Box as={NavLink} to="/home/gas" css={tabStyles}>Gas</Box>
|
||||
<Box as={NavLink} to="/home/seismic" css={tabStyles}>Seismic</Box>
|
||||
<Box as={NavLink} to="/home/remote" css={tabStyles}>Remote Sensing</Box>
|
||||
<Box as={NavLink} to="/home/daily" css={tabStyles}>Daily Activity</Box>
|
||||
</Flex>
|
||||
|
||||
{/* CANVAS */}
|
||||
<Tabs.Content value="mydashboard">
|
||||
<MyDashboard {...MyDashboardProps}/>
|
||||
</Tabs.Content>
|
||||
<Tabs.Content value="gas">
|
||||
<Gas />
|
||||
</Tabs.Content>
|
||||
<Tabs.Content value="seismic">
|
||||
<Seismic />
|
||||
</Tabs.Content>
|
||||
<Tabs.Content value="remote">
|
||||
<RemoteSensing />
|
||||
</Tabs.Content>
|
||||
<Tabs.Content value="daily">
|
||||
<DailyActivity />
|
||||
</Tabs.Content>
|
||||
</Tabs.Root>
|
||||
{/* EDIT MODE BUTTONS & CONTROLS */}
|
||||
<HStack width="258px" justifyContent="flex-end">
|
||||
{isPersonalDashboard ? (
|
||||
<>
|
||||
<SearchBar placeholder="Enter a volcano to search" size="sm" width="210px" />
|
||||
{/* This will eventually trigger your Zustand store's edit mode! */}
|
||||
<SettingsButton />
|
||||
</>
|
||||
) : (
|
||||
<SearchBar placeholder="Enter a volcano to search" size="sm" width="210px" />
|
||||
)}
|
||||
</HStack>
|
||||
|
||||
</Flex>
|
||||
|
||||
{/* 3. 🛠️ CANVAS AREA */}
|
||||
<Flex flex="1" overflow="auto">
|
||||
<Outlet />
|
||||
</Flex>
|
||||
|
||||
</Flex>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// config/widgetRegistry.js
|
||||
|
||||
// We assume a 12-column grid.
|
||||
// w: 12 = full screen width
|
||||
// w: 6 = half screen width
|
||||
// w: 4 = one-third screen width
|
||||
// w: 3 = one-quarter screen width
|
||||
|
||||
export const WIDGET_REGISTRY = {
|
||||
"vaa_activity": {
|
||||
name: "VAA 7 Day Activity",
|
||||
description: "Displays 7-day flight level timelines across multiple volcanoes.",
|
||||
allowedSizes: {
|
||||
large: { w: 12, h: 8 }, // Spans the entire width of the screen
|
||||
}
|
||||
},
|
||||
|
||||
"db_cache_health": {
|
||||
name: "DB Cache Health",
|
||||
description: "Bar chart showing cache table operational status.",
|
||||
allowedSizes: {
|
||||
medium: { w: 4, h: 6 }, // 1/3 width
|
||||
large: { w: 6, h: 6 } // 1/2 width
|
||||
}
|
||||
},
|
||||
|
||||
"ivans_alerts": {
|
||||
name: "IVANS",
|
||||
description: "Feed of recent IVANS reports and reviewer statuses.",
|
||||
allowedSizes: {
|
||||
small: { w: 3, h: 6 }, // 1/4 width (tall and skinny)
|
||||
medium: { w: 4, h: 8 } // 1/3 width
|
||||
}
|
||||
},
|
||||
|
||||
"remote_sensing": {
|
||||
name: "Remote Sensing",
|
||||
description: "Feed of recent remote sensing reports.",
|
||||
allowedSizes: {
|
||||
medium: { w: 4, h: 5 }, // 1/3 width
|
||||
}
|
||||
},
|
||||
|
||||
"alert_levels": {
|
||||
name: "Alert Level Changes",
|
||||
description: "List of 7-day alert level changes with trend arrows.",
|
||||
allowedSizes: {
|
||||
small: { w: 3, h: 5 },
|
||||
medium: { w: 4, h: 5 }
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user