diff --git a/gui/src/app/pages/HomePage/Sidebar.tsx b/gui/src/app/pages/HomePage/Sidebar.tsx index 6e1e8c9..b7d63bc 100644 --- a/gui/src/app/pages/HomePage/Sidebar.tsx +++ b/gui/src/app/pages/HomePage/Sidebar.tsx @@ -1,14 +1,17 @@ import { ProjectContext } from "@SpCore/ProjectContextProvider"; +import { modelHasUnsavedChanges } from "@SpCore/ProjectDataModel"; import LoadProjectWindow from "@SpPages/LoadProjectWindow"; import SaveProjectWindow from "@SpPages/SaveProjectWindow"; import ModalWindow, { useModalWindow } from "@fi-sci/modal-window"; -import { List, ListItem } from "@mui/material"; import Button from "@mui/material/Button"; import Divider from "@mui/material/Divider"; import Drawer from "@mui/material/Drawer"; +import Link from "@mui/material/Link"; +import List from "@mui/material/List"; +import ListItem from "@mui/material/ListItem"; import Toolbar from "@mui/material/Toolbar"; -import { FunctionComponent, useContext } from "react"; -import { Link } from "react-router-dom"; +import { FunctionComponent, useContext, useMemo } from "react"; +import { useNavigate } from "react-router-dom"; type Sidebar = { hasUnsavedChanges: boolean; @@ -33,7 +36,12 @@ const Sidebar: FunctionComponent = ({ collapsed, }) => { // note: this is close enough to pass in directly if we wish - const { update } = useContext(ProjectContext); + const { data, update } = useContext(ProjectContext); + + const navigate = useNavigate(); + + const dataModified = useMemo(() => modelHasUnsavedChanges(data), [data]); + const { visible: saveProjectVisible, handleOpen: saveProjectOpen, @@ -67,7 +75,19 @@ const Sidebar: FunctionComponent = ({ {exampleLinks.map((example, i) => ( - + { + if ( + !dataModified || + window.confirm( + "Are you sure you want to load this example? This will overwrite your current project.", + ) + ) { + navigate(`?project=${example.link}`, { replace: true }); + } + }} + > {example.name}