152 lines
8.6 KiB
React
152 lines
8.6 KiB
React
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: #/<page>?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 (
|
||
<AppShell.Header className="header">
|
||
<Group h="100%" px="md" justify="space-between" wrap="nowrap">
|
||
<Group gap="sm" wrap="nowrap">
|
||
<Burger opened={mobile} onClick={toggle.mobile} hiddenFrom="sm" size="sm" color="white" aria-label="Menu" />
|
||
<Burger opened={desktop} onClick={toggle.desktop} visibleFrom="sm" size="sm" color="white" aria-label="Sidebar" />
|
||
<Text fw={700} size="lg">AIMsight</Text>
|
||
<Text size="sm" opacity={0.6}>Diagnostic</Text>
|
||
</Group>
|
||
<Group gap="sm" wrap="nowrap">
|
||
<Tooltip label={scheme === "dark" ? "Light mode" : "Dark mode"}>
|
||
<ActionIcon variant="subtle" color="white" size="lg" aria-label="Change theme"
|
||
onClick={() => setColorScheme(scheme === "dark" ? "light" : "dark")}>
|
||
{scheme === "dark" ? <IconSun size={20} /> : <IconMoon size={20} />}
|
||
</ActionIcon>
|
||
</Tooltip>
|
||
</Group>
|
||
</Group>
|
||
</AppShell.Header>
|
||
);
|
||
}
|
||
|
||
// 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" ? <Loader size={10} color="orange" />
|
||
: <Tooltip label={state}><ColorSwatch size={8} color={`var(--mantine-color-${STATE_COLOR[state] || "gray"}-6)`} withShadow={false} /></Tooltip>);
|
||
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 <>
|
||
<Text size="xs" fw={700} tt="uppercase" c="dimmed" px="sm" pt="lg" pb="xs">{labels ? "Analyses" : "Support"} (UTC)</Text>
|
||
{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 (
|
||
<NavLink key={st} defaultOpened={busy} childrenOffset={22} leftSection={<IconFolder size={16} />}
|
||
label={<Group gap={6} wrap="nowrap">{st}{labels && <Tooltip label={`${validated} of ${its.length} validated`}><Text span size="xs" c="dimmed">{validated}/{its.length}</Text></Tooltip>}{busy && <Loader size={10} color="orange" />}</Group>}>
|
||
{its.map(a => (
|
||
<NavLink key={a.name} py={4} onClick={() => open(a)} leftSection={<Dot state={a.state} />}
|
||
rightSection={labels && (a.validated ? <Tooltip label={`labels validated ${a.validated}`}>
|
||
<IconCircleCheck size={14} color="var(--mantine-color-green-6)" /></Tooltip>
|
||
: a.state === "done" && <Tooltip label="labels to validate"><IconCircleDashed size={14} color="var(--mantine-color-dimmed)" /></Tooltip>)}
|
||
label={<Text size="xs" ff="monospace" style={{ whiteSpace: "nowrap" }}>{days(a)}</Text>}
|
||
description={<Text span size="xs" ff="monospace" c="dimmed" style={{ whiteSpace: "nowrap" }}>{hours(a)}</Text>} />
|
||
))}
|
||
</NavLink>
|
||
);
|
||
})}
|
||
</>;
|
||
}
|
||
|
||
function Sidebar({ page, selection, close, open }) {
|
||
const title = t => <Text size="xs" fw={700} tt="uppercase" c="dimmed" px="sm" pt="lg" pb="xs">{t}</Text>;
|
||
return (
|
||
<AppShell.Navbar p="xs">
|
||
<ScrollArea type="scroll">
|
||
{title("Pages")}
|
||
{PAGES.map(([n, name, Icon]) => (
|
||
<NavLink key={n} label={name} leftSection={<Icon size={18} />} active={page === n} onClick={close}
|
||
href={address(n, selection)} />
|
||
))}
|
||
<Analyses open={a => { close(); open(a); }} />
|
||
<Analyses kind="support" open={a => { close(); open(a, "support"); }} />
|
||
</ScrollArea>
|
||
</AppShell.Navbar>
|
||
);
|
||
}
|
||
|
||
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 (
|
||
<AppShell header={{ height: 64 }} padding="lg"
|
||
navbar={{ width: 260, breakpoint: "sm", collapsed: { mobile: !mobile, desktop: !desktop } }}>
|
||
<Header mobile={mobile} desktop={desktop} toggle={{ mobile: toggleMobile, desktop: toggleDesktop }} />
|
||
<Sidebar page={page} selection={selection} close={closeMobile} open={openAnalysis} />
|
||
<AppShell.Main bg="var(--mantine-color-default-hover)">
|
||
<Suspense fallback={<Loader m="xl" />}>
|
||
{page === "stations" ? <Stations selection={selection} />
|
||
: <Data selection={selection} choose={choose} analysis={analysis} setAnalysis={setAnalysis}
|
||
support={support} setSupport={setSupport} />}
|
||
</Suspense>
|
||
</AppShell.Main>
|
||
</AppShell>
|
||
);
|
||
}
|