97 lines
3.2 KiB
React
97 lines
3.2 KiB
React
import { useEffect, useState } from "react";
|
|
import { Navigate, Route, Routes, useNavigate } from "react-router-dom";
|
|
import { api, applySession, clearSession, isDirty } from "./api.js";
|
|
import { SessionContext } from "./session.js";
|
|
import { Login } from "./pages/Login.jsx";
|
|
import { Shell } from "./pages/Shell.jsx";
|
|
import { Overview } from "./pages/Overview.jsx";
|
|
import { CaseList, CaseDetail } from "./pages/Cases.jsx";
|
|
import { MatterList, MatterDetail } from "./pages/Matters.jsx";
|
|
import { Rooms } from "./pages/Rooms.jsx";
|
|
import { Search } from "./pages/Search.jsx";
|
|
import { Sources } from "./pages/Sources.jsx";
|
|
import { Users } from "./pages/Users.jsx";
|
|
import { Work } from "./pages/Work.jsx";
|
|
import { Settings } from "./pages/Settings.jsx";
|
|
import { Reader } from "./pages/Reader.jsx";
|
|
|
|
export function App() {
|
|
const [session, setSession] = useState(undefined);
|
|
const navigate = useNavigate();
|
|
|
|
useEffect(() => {
|
|
let active = true;
|
|
api("/api/v1/auth/session")
|
|
.then((data) => {
|
|
if (!active) return;
|
|
applySession(data);
|
|
setSession(data);
|
|
})
|
|
.catch(() => {
|
|
if (!active) return;
|
|
clearSession();
|
|
setSession(null);
|
|
});
|
|
return () => {
|
|
active = false;
|
|
};
|
|
}, []);
|
|
|
|
async function refresh() {
|
|
const data = await api("/api/v1/auth/session");
|
|
applySession(data);
|
|
setSession(data);
|
|
return data;
|
|
}
|
|
|
|
async function changeScope(next) {
|
|
if (isDirty() && !window.confirm("Ungespeicherte Eingaben verwerfen und den Datenraum wechseln?")) {
|
|
return;
|
|
}
|
|
const data = await api("/api/v1/auth/session/scope", { method: "POST", body: { kind: next.kind, id: next.id } });
|
|
applySession(data);
|
|
setSession(data);
|
|
navigate("/");
|
|
}
|
|
|
|
async function logout() {
|
|
try {
|
|
await api("/api/v1/auth/logout", { method: "POST" });
|
|
} catch {
|
|
/* Die lokale Sitzung endet auch, wenn der Server schon abgemeldet hat. */
|
|
}
|
|
clearSession();
|
|
setSession(null);
|
|
navigate("/login");
|
|
}
|
|
|
|
if (session === undefined) {
|
|
return <p className="boot">Tsumugu wird geladen …</p>;
|
|
}
|
|
|
|
const value = { session, setSession, refresh, changeScope, logout };
|
|
|
|
return (
|
|
<SessionContext.Provider value={value}>
|
|
<Routes>
|
|
<Route path="/login" element={session ? <Navigate to="/" replace /> : <Login />} />
|
|
<Route element={session ? <Shell /> : <Navigate to="/login" replace />}>
|
|
<Route index element={<Overview />} />
|
|
<Route path="cases" element={<CaseList />} />
|
|
<Route path="cases/:id" element={<CaseDetail />} />
|
|
<Route path="matters" element={<MatterList />} />
|
|
<Route path="matters/:id" element={<MatterDetail />} />
|
|
<Route path="matters/:id/items/:bindingId" element={<Reader />} />
|
|
<Route path="cases/:id/items/:bindingId" element={<Reader />} />
|
|
<Route path="work" element={<Work />} />
|
|
<Route path="settings" element={<Settings />} />
|
|
<Route path="rooms" element={<Rooms />} />
|
|
<Route path="sources" element={<Sources />} />
|
|
<Route path="users" element={<Users />} />
|
|
<Route path="search" element={<Search />} />
|
|
</Route>
|
|
</Routes>
|
|
</SessionContext.Provider>
|
|
);
|
|
}
|