2025-09-09 15:57:35 +01:00
|
|
|
import * as React from "react";
|
2025-08-13 14:23:48 +01:00
|
|
|
import { Link } from "react-router";
|
|
|
|
|
import Logo from "/MAV.svg";
|
2025-08-29 10:07:59 +01:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
2025-09-09 15:57:35 +01:00
|
|
|
import { faGear, faListCheck } from "@fortawesome/free-solid-svg-icons";
|
|
|
|
|
import type { VersionFieldType } from "../../types/types";
|
|
|
|
|
|
2025-09-12 08:21:52 +01:00
|
|
|
async function fetchVersions(
|
|
|
|
|
signal?: AbortSignal
|
|
|
|
|
): Promise<VersionFieldType | undefined> {
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch("http://192.168.75.11/api/versions", { signal });
|
|
|
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
|
|
|
return res.json();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2025-09-09 15:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pad = (n: number) => String(n).padStart(2, "0");
|
|
|
|
|
const normalizeToMs = (ts: number) => (ts < 1e12 ? ts * 1000 : ts); // seconds → ms if needed
|
|
|
|
|
|
|
|
|
|
function formatFromMs(ms: number, tz: "local" | "utc" = "local") {
|
|
|
|
|
const d = new Date(ms);
|
|
|
|
|
const h = tz === "utc" ? d.getUTCHours() : d.getHours();
|
|
|
|
|
const m = tz === "utc" ? d.getUTCMinutes() : d.getMinutes();
|
|
|
|
|
const s = tz === "utc" ? d.getUTCSeconds() : d.getSeconds();
|
|
|
|
|
const day = tz === "utc" ? d.getUTCDate() : d.getDate();
|
|
|
|
|
const month = (tz === "utc" ? d.getUTCMonth() : d.getMonth()) + 1;
|
|
|
|
|
const year = tz === "utc" ? d.getUTCFullYear() : d.getFullYear();
|
|
|
|
|
return `${pad(h)}:${pad(m)}:${pad(s)} ${pad(day)}-${pad(month)}-${year}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function Header() {
|
|
|
|
|
const [offsetMs, setOffsetMs] = React.useState<number | null>(null);
|
|
|
|
|
const [nowMs, setNowMs] = React.useState<number>(Date.now());
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
const ac = new AbortController();
|
|
|
|
|
fetchVersions(ac.signal)
|
|
|
|
|
.then((data) => {
|
2025-09-12 08:21:52 +01:00
|
|
|
if (!data) throw new Error("No data");
|
|
|
|
|
const serverMs = normalizeToMs(data?.timeStamp);
|
2025-09-09 15:57:35 +01:00
|
|
|
setOffsetMs(serverMs - Date.now());
|
|
|
|
|
})
|
2025-09-12 08:21:52 +01:00
|
|
|
.catch((err) => {
|
|
|
|
|
console.log(err);
|
|
|
|
|
});
|
|
|
|
|
return () => ac.abort("failed");
|
2025-09-09 15:57:35 +01:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
let timer: number;
|
|
|
|
|
const schedule = () => {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
setNowMs(now);
|
|
|
|
|
const delay = 1000 - (now % 1000);
|
|
|
|
|
timer = window.setTimeout(schedule, delay);
|
|
|
|
|
};
|
|
|
|
|
schedule();
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const serverNowMs = offsetMs == null ? nowMs : nowMs + offsetMs;
|
|
|
|
|
const localStr = formatFromMs(serverNowMs, "local");
|
|
|
|
|
const utcStr = formatFromMs(serverNowMs, "utc");
|
2025-08-13 14:23:48 +01:00
|
|
|
|
|
|
|
|
return (
|
2025-08-29 10:07:59 +01:00
|
|
|
<div className="relative bg-[#253445] border-b border-gray-500 items-center mx-auto px-2 sm:px-6 lg:px-8 p-4 flex flex-row justify-between">
|
2025-09-08 15:21:17 +01:00
|
|
|
{/* Left: Logo */}
|
2025-08-13 14:23:48 +01:00
|
|
|
<div className="w-30">
|
|
|
|
|
<Link to={"/"}>
|
2025-09-08 15:21:17 +01:00
|
|
|
<img src={Logo} alt="Logo" width={150} height={150} />
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
2025-09-09 15:57:35 +01:00
|
|
|
{/* Right: Texts stacked + icons */}
|
2025-09-17 14:39:56 +01:00
|
|
|
<div className="flex items-center space-x-24">
|
2025-09-09 15:57:35 +01:00
|
|
|
<div className="flex flex-col leading-tight text-white text-sm tabular-nums">
|
|
|
|
|
<h2>Local: {localStr}</h2>
|
|
|
|
|
<h2>UTC: {utcStr}</h2>
|
|
|
|
|
</div>
|
2025-09-17 14:39:56 +01:00
|
|
|
<div className="flex flex-row space-x-8">
|
2025-09-12 08:21:52 +01:00
|
|
|
<Link to={"/session-settings"}>
|
2025-09-17 14:39:56 +01:00
|
|
|
<FontAwesomeIcon
|
|
|
|
|
className="text-white"
|
|
|
|
|
icon={faListCheck}
|
|
|
|
|
size="2x"
|
|
|
|
|
/>
|
2025-09-12 08:21:52 +01:00
|
|
|
</Link>
|
|
|
|
|
<Link to={"/system-settings"}>
|
2025-09-17 14:39:56 +01:00
|
|
|
<FontAwesomeIcon className="text-white" icon={faGear} size="2x" />
|
2025-09-12 08:21:52 +01:00
|
|
|
</Link>
|
|
|
|
|
</div>
|
2025-08-13 14:23:48 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-09-09 15:57:35 +01:00
|
|
|
}
|