drawDB/src/App.jsx

90 lines
2.3 KiB
React
Raw Normal View History

2024-02-26 19:17:20 +08:00
import { BrowserRouter, Routes, Route, useLocation } from "react-router-dom";
import { useLayoutEffect } from "react";
import Editor from "./pages/Editor";
import Survey from "./pages/Survey";
import BugReport from "./pages/BugReport";
2024-01-07 11:10:34 +08:00
import Shortcuts from "./pages/Shortcuts";
2023-11-30 15:18:41 +08:00
import Templates from "./pages/Templates";
2024-02-26 19:17:20 +08:00
import LandingPage from "./pages/LandingPage";
2024-03-10 04:39:46 +08:00
import SettingsContextProvider from "./context/SettingsContext";
2024-06-15 00:27:44 +08:00
import { useSettings } from "./hooks";
2024-04-06 18:59:39 +08:00
import NotFound from "./pages/NotFound";
2024-02-26 19:17:20 +08:00
2024-04-06 10:19:13 +08:00
export default function App() {
2023-09-19 20:46:33 +08:00
return (
2024-03-10 04:39:46 +08:00
<SettingsContextProvider>
<BrowserRouter>
<RestoreScroll />
2024-01-07 11:10:34 +08:00
<Routes>
2024-02-26 19:17:20 +08:00
<Route path="/" element={<LandingPage />} />
2024-03-10 01:42:09 +08:00
<Route
path="/editor"
element={
2024-03-10 04:39:46 +08:00
<ThemedPage>
2024-03-10 01:42:09 +08:00
<Editor />
2024-03-10 04:39:46 +08:00
</ThemedPage>
}
/>
<Route
path="/survey"
element={
<ThemedPage>
<Survey />
</ThemedPage>
}
/>
<Route
path="/shortcuts"
element={
<ThemedPage>
<Shortcuts />
</ThemedPage>
}
/>
<Route
2024-04-06 10:19:13 +08:00
path="/bug-report"
2024-03-10 04:39:46 +08:00
element={
<ThemedPage>
<BugReport />
</ThemedPage>
2024-03-10 01:42:09 +08:00
}
/>
2024-01-07 11:10:34 +08:00
<Route path="/templates" element={<Templates />} />
2024-04-06 18:59:39 +08:00
<Route path="*" element={<NotFound />} />
2024-01-07 11:10:34 +08:00
</Routes>
2024-03-10 04:39:46 +08:00
</BrowserRouter>
</SettingsContextProvider>
2023-09-19 20:46:33 +08:00
);
}
2024-04-06 10:19:13 +08:00
function ThemedPage({ children }) {
const { setSettings } = useSettings();
useLayoutEffect(() => {
const theme = localStorage.getItem("theme");
if (theme === "dark") {
setSettings((prev) => ({ ...prev, mode: "dark" }));
const body = document.body;
if (body.hasAttribute("theme-mode")) {
body.setAttribute("theme-mode", "dark");
}
} else {
setSettings((prev) => ({ ...prev, mode: "light" }));
const body = document.body;
if (body.hasAttribute("theme-mode")) {
body.setAttribute("theme-mode", "light");
}
}
}, [setSettings]);
return children;
}
function RestoreScroll() {
const location = useLocation();
useLayoutEffect(() => {
2024-04-06 10:19:13 +08:00
window.scroll(0, 0);
}, [location.pathname]);
return null;
}