2026-11-13 11:32:43 +01:00
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" ;
2026-11-17 12:25:43 +01:00
import { IconChartLine , IconCircleCheck , IconCircleDashed , IconFolder , IconMoon , IconSatellite , IconSun } from "@tabler/icons-react" ;
2026-11-13 11:32:43 +01:00
2026-11-16 12:40:03 +01:00
const Data = lazy (() => import ( "./pages/Data.jsx" )); // Plotly, loaded only here
2026-11-17 08:59:51 +01:00
const Stations = lazy (() => import ( "./pages/Stations.jsx" ));
const PAGES = [[ "data" , "Data" , IconChartLine ], [ "stations" , "Stations" , IconSatellite ]];
2026-11-13 11:32:43 +01:00
2026-11-16 12:40:03 +01:00
// 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 * 864 e5 ). toISOString (). slice ( 0 , 10 );
function readAddress () {
2026-11-13 11:32:43 +01:00
const [ page , q = "" ] = location . hash . replace ( /^#\/?/ , "" ). split ( "?" );
const p = new URLSearchParams ( q );
return { page : PAGES . some (([ n ]) => n === page ) ? page : "data" ,
2026-11-16 12:40:03 +01:00
selection : { station : p . get ( "station" ) || "" , start : p . get ( "start" ) || day ( - 4 ), end : p . get ( "end" ) || day ( 1 ) } };
2026-11-13 11:32:43 +01:00
}
2026-11-16 12:40:03 +01:00
function address ( page , s ) {
2026-11-13 11:32:43 +01:00
const q = new URLSearchParams ( Object . entries ( s ). filter (([, v ]) => v )). toString ();
return `#/ ${ page }${ q ? "?" + q : "" } ` ;
}
2026-11-16 12:40:03 +01:00
function Header ({ mobile , desktop , toggle }) {
const { setColorScheme } = useMantineColorScheme (), scheme = useComputedColorScheme ( "light" );
2026-11-13 11:32:43 +01:00
return (
2026-11-16 12:40:03 +01:00
< AppShell.Header className = "header" >
2026-11-13 11:32:43 +01:00
< Group h = "100%" px = "md" justify = "space-between" wrap = "nowrap" >
< Group gap = "sm" wrap = "nowrap" >
2026-11-16 12:40:03 +01:00
< 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" />
2026-11-13 11:32:43 +01:00
< Text fw = { 700 } size = "lg" > AIMsight </ Text >
< Text size = "sm" opacity = { 0.6 }> Diagnostic </ Text >
</ Group >
< Group gap = "sm" wrap = "nowrap" >
2026-11-16 12:40:03 +01:00
< 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 } />}
2026-11-13 11:32:43 +01:00
</ ActionIcon >
</ Tooltip >
</ Group >
</ Group >
</ AppShell.Header >
);
}
2026-11-16 12:40:03 +01:00
// The analyses of the Data page (labeling/auto/analyses/), read again every 3 s: one folder per station, open by default if
2026-11-17 12:25:43 +01:00
// 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
2026-11-16 12:40:03 +01:00
// The range of an analysis on one line: "01.11 · 10:30– 11:40" within the day, otherwise "04.11 21:12 – 07.11 14:12" (UTC)
const dayMonth = t => ` ${ t . slice ( 8 , 10 ) } . ${ t . slice ( 5 , 7 ) } ` , hour = t => t . slice ( 11 , 16 );
const range = a => ( a . start . slice ( 0 , 10 ) === a . end . slice ( 0 , 10 )
? ` ${ dayMonth ( a . start ) } · ${ hour ( a . start ) } – ${ hour ( a . end ) } ` : ` ${ dayMonth ( a . start ) } ${ hour ( a . start ) } – ${ dayMonth ( a . end ) } ${ 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 ({ open }) {
const [ list , setList ] = useState ([]);
2026-11-13 11:32:43 +01:00
useEffect (() => {
2026-11-16 12:40:03 +01:00
const read = () => fetch ( "/api/analyses" ). 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 );
2026-11-13 11:32:43 +01:00
}, []);
2026-11-16 12:40:03 +01:00
if ( ! list . length ) return null ;
const stations = [... new Set ( list . map ( a => a . station ))]. sort ();
2026-11-13 11:32:43 +01:00
return <>
< Text size = "xs" fw = { 700 } tt = "uppercase" c = "dimmed" px = "sm" pt = "lg" pb = "xs" > Analyses ( UTC )</ Text >
{ stations . map ( st => {
2026-11-16 12:40:03 +01:00
const its = list . filter ( a => a . station === st ), busy = its . some ( a => a . state === "running" );
2026-11-17 12:25:43 +01:00
const validated = its . filter ( a => a . validated ). length ;
2026-11-16 12:40:03 +01:00
// the chevron of the folder stays Mantine's: a rightSection would take its place, and rotate with it
2026-11-13 11:32:43 +01:00
return (
2026-11-16 12:40:03 +01:00
< NavLink key = { st } defaultOpened = { busy } childrenOffset = { 22 } leftSection = {< IconFolder size = { 16 } />}
2026-11-17 12:25:43 +01:00
label = {< Group gap = { 6 } wrap = "nowrap" >{ st }< 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 >}>
2026-11-16 12:40:03 +01:00
{ its . map ( a => (
< NavLink key = { a . name } py = { 4 } onClick = {() => open ( a )} leftSection = {< Dot state = { a . state } />}
2026-11-17 12:25:43 +01:00
rightSection = { 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 >}
2026-11-16 12:40:03 +01:00
label = {< Text size = "xs" ff = "monospace" style = {{ whiteSpace : "nowrap" }}>{ range ( a )}</ Text >} />
2026-11-13 11:32:43 +01:00
))}
</ NavLink >
);
})}
</>;
}
2026-11-16 12:40:03 +01:00
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 >;
2026-11-13 11:32:43 +01:00
return (
< AppShell.Navbar p = "xs" >
< ScrollArea type = "scroll" >
2026-11-16 12:40:03 +01:00
{ 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 )} />
2026-11-13 11:32:43 +01:00
))}
2026-11-16 12:40:03 +01:00
< Analyses open = { a => { close (); open ( a ); }} />
2026-11-13 11:32:43 +01:00
</ ScrollArea >
</ AppShell.Navbar >
);
}
export default function App () {
2026-11-16 12:40:03 +01:00
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
2026-11-13 11:32:43 +01:00
useEffect (() => {
2026-11-16 12:40:03 +01:00
const follow = () => setPlace ( readAddress ());
addEventListener ( "hashchange" , follow );
return () => removeEventListener ( "hashchange" , follow );
2026-11-13 11:32:43 +01:00
}, []);
2026-11-17 08:59:51 +01:00
// the same address again (Load): read again, a new selection loads the data again
2026-11-17 08:51:48 +01:00
const choose = s => { const a = address ( page , s ); if ( location . hash === a ) setPlace ( readAddress ()); else location . hash = a ; };
2026-11-16 12:40:03 +01:00
// An analysis of the sidebar: Data on its station, days that cover its range (unless the range shown already covers them)
function openAnalysis ( a ) {
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 )) + 864 e5 ). toISOString (). slice ( 0 , 10 );
setAnalysis ( a . name );
location . hash = address ( "data" , covers ? s : { station : a . station , start : a . start . slice ( 0 , 10 ), end });
2026-11-13 11:32:43 +01:00
}
return (
< AppShell header = {{ height : 64 }} padding = "lg"
2026-11-16 12:40:03 +01:00
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 } />
2026-11-13 11:32:43 +01:00
< AppShell.Main bg = "var(--mantine-color-default-hover)" >
2026-11-17 08:59:51 +01:00
< Suspense fallback = {< Loader m = "xl" />}>
{ page === "stations" ? < Stations selection = { selection } />
: < Data selection = { selection } choose = { choose } analysis = { analysis } setAnalysis = { setAnalysis } />}
</ Suspense >
2026-11-13 11:32:43 +01:00
</ AppShell.Main >
</ AppShell >
);
}