import { useEffect, useRef, useState } from "react";
import { Button, Card, Group, Select, TextInput } from "@mantine/core";
import { notifications } from "@mantine/notifications";
// A request to the API; a refusal of the server shows as a notification -> the JSON returned, or null
export async function call(path, method = "GET", body) {
try {
// every POST, PUT, DELETE declares JSON, even without a body: the server refuses it otherwise (its guard)
const r = await fetch(path, method === "GET" ? { method }
: { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body ?? {}) });
const json = await r.json().catch(() => ({ error: r.statusText }));
if (r.ok) return json;
notifications.show({ color: "red", title: "Refused", message: json.error || r.statusText });
} catch (e) {
notifications.show({ color: "red", title: "Server unreachable", message: e.message });
}
return null;
}
// The station and the range (UTC, To excluded; empty: the whole period), shared by the pages through the address
export function Selector({ selection, choose }) {
const [stations, setStations] = useState([]), [s, setS] = useState(selection);
useEffect(() => { call("/api/stations").then(l => l && setStations(l)); }, []);
useEffect(() => setS(selection), [selection]); // a new object at each change of the address
return (
);
}
// A markdown rendered by the server (report, summary, transcript), at the height of its content once its images are loaded;
// sandboxed: no script runs in it
export function Page({ title, src }) {
const frame = useRef();
const fit = () => { const b = frame.current?.contentDocument?.body; if (b) frame.current.style.height = `${b.scrollHeight + 20}px`; };
return ;
}