import { lazy, Suspense, useEffect, useState } from "react"; import { ActionIcon, AppShell, Burger, ColorSwatch, Group, Loader, NavLink, ScrollArea, Text, Tooltip, useComputedColorScheme, useMantineColorScheme, } from "@mantine/core"; import { useDisclosure } from "@mantine/hooks"; import { IconChartLine, IconCircleCheck, IconCircleDashed, IconFolder, IconMoon, IconSatellite, IconSun } from "@tabler/icons-react"; const Data = lazy(() => import("./pages/Data.jsx")); // Plotly, loaded only here const Stations = lazy(() => import("./pages/Stations.jsx")); const PAGES = [["data", "Data", IconChartLine], ["stations", "Stations", IconSatellite]]; // The address: #/?station=…&start=…&end=…; reloading keeps the page and the selection, Back works; without dates, the // last 5 UTC days, today included (end excluded: tomorrow) const day = n => new Date(Date.now() + n * 864e5).toISOString().slice(0, 10); function readAddress() { const [page, q = ""] = location.hash.replace(/^#\/?/, "").split("?"); const p = new URLSearchParams(q); return { page: PAGES.some(([n]) => n === page) ? page : "data", selection: { station: p.get("station") || "", start: p.get("start") || day(-4), end: p.get("end") || day(1) } }; } function address(page, s) { const q = new URLSearchParams(Object.entries(s).filter(([, v]) => v)).toString(); return `#/${page}${q ? "?" + q : ""}`; } function Header({ mobile, desktop, toggle }) { const { setColorScheme } = useMantineColorScheme(), scheme = useComputedColorScheme("light"); return ( AIMsight Diagnostic setColorScheme(scheme === "dark" ? "light" : "dark")}> {scheme === "dark" ? : } ); } // The analyses of the Data page (labeling/auto/analyses/), read again every 3 s: one folder per station, open by default if // it has one running, and inside one analysis per range (a green tick: its labels validated); a click opens Data on its // station and its days, and shows its report there. kind "support": the requests of the Support tab (labeling/auto/support/), // the same without labels to validate // The range of an analysis on two lines, too wide for the sidebar on one: the days "04.11 – 07.11" ("01.11" within the day), // under them the hours "21:12 – 14:12" (UTC) const dayMonth = t => `${t.slice(8, 10)}.${t.slice(5, 7)}`, hour = t => t.slice(11, 16); const days = a => (a.start.slice(0, 10) === a.end.slice(0, 10) ? dayMonth(a.start) : `${dayMonth(a.start)} – ${dayMonth(a.end)}`); const hours = a => `${hour(a.start)} – ${hour(a.end)}`; const STATE_COLOR = { done: "teal", error: "red", interrupted: "gray" }; // The state: a spinning wheel while running, otherwise a coloured dot (its name in the tooltip) const Dot = ({ state }) => (state === "running" ? : ); function Analyses({ kind = "analyses", open }) { const [list, setList] = useState([]), labels = kind === "analyses"; useEffect(() => { const read = () => fetch(`/api/${kind}`).then(r => r.ok && r.json()).then(l => l && setList(l)).catch(() => {}); // silently: every 3 s read(); const timer = setInterval(read, 3000); return () => clearInterval(timer); }, []); if (!list.length) return null; const stations = [...new Set(list.map(a => a.station))].sort(); return <> {labels ? "Analyses" : "Support"} (UTC) {stations.map(st => { const its = list.filter(a => a.station === st), busy = its.some(a => a.state === "running"); const validated = its.filter(a => a.validated).length; // the chevron of the folder stays Mantine's: a rightSection would take its place, and rotate with it return ( } label={{st}{labels && {validated}/{its.length}}{busy && }}> {its.map(a => ( open(a)} leftSection={} rightSection={labels && (a.validated ? : a.state === "done" && )} label={{days(a)}} description={{hours(a)}} /> ))} ); })} ; } function Sidebar({ page, selection, close, open }) { const title = t => {t}; return ( {title("Pages")} {PAGES.map(([n, name, Icon]) => ( } active={page === n} onClick={close} href={address(n, selection)} /> ))} { close(); open(a); }} /> { close(); open(a, "support"); }} /> ); } export default function App() { const [{ page, selection }, setPlace] = useState(readAddress); const [mobile, { toggle: toggleMobile, close: closeMobile }] = useDisclosure(false); const [desktop, { toggle: toggleDesktop }] = useDisclosure(true); const [analysis, setAnalysis] = useState(null); // the analysis whose report Data shows const [support, setSupport] = useState(null); // the support request whose answer Data shows useEffect(() => { const follow = () => setPlace(readAddress()); addEventListener("hashchange", follow); return () => removeEventListener("hashchange", follow); }, []); // the same address again (Load): read again, a new selection loads the data again const choose = s => { const a = address(page, s); if (location.hash === a) setPlace(readAddress()); else location.hash = a; }; // An analysis (or a support request) of the sidebar: Data on its station, days that cover its range (unless the range // shown already covers them) function openAnalysis(a, kind) { const s = selection, covers = s.station === a.station && s.start <= a.start.slice(0, 10) && a.end < s.end; const end = new Date(Date.parse(a.end.slice(0, 10)) + 864e5).toISOString().slice(0, 10); (kind === "support" ? setSupport : setAnalysis)(a.name); location.hash = address("data", covers ? s : { station: a.station, start: a.start.slice(0, 10), end }); } return (
}> {page === "stations" ? : } ); }