Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ export default function App() {
// Kept in App so the choice survives leaving and re-entering the area.
const [systemTab, setSystemTab] = useState<SystemTab>("transmissions");
const [refreshKey, setRefreshKey] = useState(0);
// The last poll failed. The data stays on screen; only the badge changes.
const [offline, setOffline] = useState(false);

// Poll live status every 30 s.
// Poll live status every 30 s. The cards and charts update in place — a
// failed poll keeps the last reading on screen rather than emptying the
// page, which would flicker once every time the network hiccups.
useEffect(() => {
let active = true;
const load = () =>
fetchStatus()
.then((s) => {
if (!active) return;
setStatus(s);
setOffline(false);
setRefreshKey((value) => value + 1);
})
.catch(() => active && setStatus(null));
.catch(() => active && setOffline(true));
load();
const timer = setInterval(load, 30_000);
return () => {
Expand Down Expand Up @@ -111,7 +116,7 @@ export default function App() {
<option value="en">{t("english")}</option>
<option value="de">{t("german")}</option>
</select>
<ServiceStatus status={status} />
<ServiceStatus status={status} offline={offline} />
</div>
</header>

Expand Down
21 changes: 17 additions & 4 deletions src/ui/src/components/DiagnosticsView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import {
Area,
CartesianGrid,
Expand Down Expand Up @@ -74,20 +74,33 @@ export function DiagnosticsView({
const { locale, t } = useI18n();
const [diagnostics, setDiagnostics] = useState<Diagnostics | null>(null);
const [error, setError] = useState<string | null>(null);
// What the tables on screen show. The 30-second refresh swaps their values;
// only a different day or inverter empties them, since keeping one
// inverter's rows under another's name would be wrong rather than merely
// stale.
const shownQuery = useRef<string | null>(null);

useEffect(() => {
let cancelled = false;
setDiagnostics(null);
const query = `${date ?? ""}|${serial ?? "all"}`;
if (shownQuery.current !== query) {
setDiagnostics(null);
}
setError(null);
fetchDiagnostics(date, serial)
.then((data) => !cancelled && setDiagnostics(data))
.then((data) => {
if (cancelled) return;
shownQuery.current = query;
setDiagnostics(data);
})
.catch((reason: unknown) => !cancelled && setError(String(reason)));
return () => {
cancelled = true;
};
}, [date, serial, refreshKey]);

if (error) {
// A failed refresh keeps the tables that are already up.
if (error && !diagnostics) {
return (
<p className="py-8 text-center text-sm text-muted-foreground">
{t("loadError", { error })}
Expand Down
38 changes: 34 additions & 4 deletions src/ui/src/components/HistoryChart.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ReactNode, useEffect, useMemo, useState } from "react";
import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";
import {
Area,
Bar,
Expand Down Expand Up @@ -93,10 +93,23 @@ export function HistoryChart({
const [year, setYear] = useState<string | null>(null);
const [currentYear, setCurrentYear] = useState<string | null>(null);
const [refresh, setRefresh] = useState(0);
// The chart animates when it first appears. A periodic refresh replaces the
// data of a chart that is already on screen, and replaying the animation
// there reads as flicker.
const [animate, setAnimate] = useState(true);
// What the data on screen belongs to. A refresh keeps it and swaps the
// values underneath; a different range, inverter or period does not, since
// showing one period's data under another's heading would be a lie.
const shownQuery = useRef<string | null>(null);

useEffect(() => {
let cancelled = false;
setHistory(null);
const query = [range, serial ?? "all", day, week, month, year].join("|");
const sameChart = shownQuery.current === query;
if (!sameChart) {
setHistory(null);
setAnimate(true);
}
setError(null);
const period =
range === "day"
Expand All @@ -111,7 +124,11 @@ export function HistoryChart({
fetchHistory(range, serial, false, period)
.then((data) => {
if (cancelled) return;
shownQuery.current = query;
setHistory(data);
if (sameChart) {
setAnimate(false);
}
if (range === "day" && data.date && day === null) {
setDay(data.date);
}
Expand Down Expand Up @@ -154,7 +171,7 @@ export function HistoryChart({
const live = isDay && day !== null && day === today;

let chart: ReactNode;
if (error) {
if (error && !history) {
chart = (
<p className="py-12 text-center text-sm text-muted-foreground">
{t("loadError", { error })}
Expand All @@ -177,7 +194,13 @@ export function HistoryChart({
);
} else {
if (isDay) {
chart = <DayHistoryChart history={history} allInverters={serial === null} />;
chart = (
<DayHistoryChart
history={history}
allInverters={serial === null}
animate={animate}
/>
);
} else if (range === "week") {
chart = <WeekHistoryView history={history} />;
} else if (range === "month") {
Expand Down Expand Up @@ -248,6 +271,7 @@ export function HistoryChart({
dataKey={dataKey}
fill={`var(--color-${dataKey})`}
radius={[4, 4, 0, 0]}
isAnimationActive={animate}
/>
))}
</BarChart>
Expand Down Expand Up @@ -310,9 +334,13 @@ export function HistoryChart({
function DayHistoryChart({
history,
allInverters,
animate,
}: {
history: History;
allInverters: boolean;
/// False once the chart is on screen and only its data is being replaced,
/// so a refresh does not replay the entry animation.
animate: boolean;
}) {
const { locale, t } = useI18n();
const availableSeries = useMemo(
Expand Down Expand Up @@ -545,6 +573,7 @@ function DayHistoryChart({
activeDot={{ r: 3 }}
connectNulls={false}
hide={hiddenSeries.has(item.key)}
isAnimationActive={animate}
animationDuration={500}
/>
) : (
Expand All @@ -561,6 +590,7 @@ function DayHistoryChart({
dot={false}
activeDot={{ r: 4, strokeWidth: 2 }}
hide={hiddenSeries.has(item.key)}
isAnimationActive={animate}
animationDuration={500}
/>
),
Expand Down
11 changes: 10 additions & 1 deletion src/ui/src/components/ServiceStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import {
} from "@/components/ui/popover";
import { useI18n } from "@/lib/i18n";

export function ServiceStatus({ status }: { status: Status | null }) {
export function ServiceStatus({
status,
offline = false,
}: {
status: Status | null;
/// The last poll did not reach the service. Its previous reading stays on
/// screen, so the badge is what says the numbers are no longer live.
offline?: boolean;
}) {
const { t } = useI18n();

if (!status) {
Expand All @@ -22,6 +30,7 @@ export function ServiceStatus({ status }: { status: Status | null }) {
return (
<div className="flex min-w-0 items-center gap-1">
<Badge variant="outline">v{status.version}</Badge>
{offline && <Badge variant="destructive">{t("offline")}</Badge>}
{status.lastError && (
<Popover>
<PopoverTrigger asChild>
Expand Down