From 0b74842560be6b4c784686359ec71b880c34b993 Mon Sep 17 00:00:00 2001 From: bonamin Date: Thu, 22 Oct 2026 18:15:38 +0300 Subject: [PATCH] feat(frontend): settings page, Greek translation, comparison and chart options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The end user is Greek and the owner wants to confirm at a glance that every inverter is working without one cloud-shaded reading raising doubt. - Settings page (gear in the header), stored on the server and shared by every viewer; saving asks for the API key once per device. - Output comparison: latest reading or average of the last 3/5 (evens out a passing cloud); bar scale auto (round ceiling above the best) / highest = 100% / custom kW; percentage vs fleet median, best inverter or the custom kW. - Power chart: 30 min to last month plus "last production run", a per-visit range picker on the card, optional light/strong smoothing. Series reporting slower than the chart bucket (INV5, every 5 min) are now interpolated across short gaps — on 1-min buckets their line used to vanish entirely; night gaps still break the line. - Greek/English: per-device switch in the header, shared default in settings, Greek number/date formats; issue texts translated from code + params. - AC voltage shown as phase (L-N) or line (L-L, x√3, matching the inverter screens); lifetime total says how many inverters it covers; the inverter's own status text appears on its card. Co-Authored-By: Claude Opus 5.5 --- frontend/src/App.jsx | 40 +- frontend/src/components/Charts.jsx | 94 +++- frontend/src/components/Comparison.jsx | 124 ++++- .../src/components/EditInverterDialog.jsx | 48 +- frontend/src/components/Health.jsx | 36 +- frontend/src/components/Icon.jsx | 12 + frontend/src/components/InverterCard.jsx | 20 +- frontend/src/lib/api.js | 2 + frontend/src/lib/apiKey.js | 19 + frontend/src/lib/format.js | 58 ++- frontend/src/lib/i18n.jsx | 481 ++++++++++++++++++ frontend/src/lib/settings.jsx | 63 +++ frontend/src/main.jsx | 8 +- frontend/src/pages/Dashboard.jsx | 132 +++-- frontend/src/pages/InverterDetail.jsx | 123 +++-- frontend/src/pages/Settings.jsx | 243 +++++++++ 16 files changed, 1302 insertions(+), 201 deletions(-) create mode 100644 frontend/src/lib/apiKey.js create mode 100644 frontend/src/lib/i18n.jsx create mode 100644 frontend/src/lib/settings.jsx create mode 100644 frontend/src/pages/Settings.jsx diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index f0a1176..7104495 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -5,8 +5,10 @@ import { useInstallPrompt } from './hooks/useInstallPrompt' import { usePoll } from './hooks/usePoll' import { api } from './lib/api' import { ago } from './lib/format' +import { useT } from './lib/i18n' import Dashboard from './pages/Dashboard' import InverterDetail from './pages/InverterDetail' +import Settings from './pages/Settings' function useTick(ms) { const [, setN] = useState(0) @@ -17,6 +19,7 @@ function useTick(ms) { } function LiveChip({ summary, error }) { + const { t } = useT() useTick(10_000) if (error && !summary) return null if (!summary) return null @@ -28,7 +31,11 @@ function LiveChip({ summary, error }) { style={{ background: ok ? 'var(--good)' : 'var(--critical)' }} aria-hidden="true" /> - {error ? 'Connection lost' : ok ? `Live · ${ago(summary.datalogger.last_ingest_at)}` : 'Datalogger offline'} + {error + ? t('live.connectionLost') + : ok + ? t('live.live', { ago: ago(summary.datalogger.last_ingest_at) }) + : t('live.dataloggerOffline')} ) } @@ -37,26 +44,38 @@ export default function App() { // One summary poll shared by both pages, so navigating is instant const summaryPoll = usePoll(() => api.summary(), 30_000) const install = useInstallPrompt() + const { t, lang, setLang } = useT() + const iconButton = + 'inline-flex min-h-9 min-w-9 items-center justify-center gap-1 rounded-lg border border-line px-2 text-xs font-medium text-ink-2 hover:text-ink' return (
- + - Solar Monitor + {t('app.title')} -
+
{install && ( - )} + + + +
@@ -64,6 +83,7 @@ export default function App() { } /> } /> + } /> } /> diff --git a/frontend/src/components/Charts.jsx b/frontend/src/components/Charts.jsx index 326d454..617a559 100644 --- a/frontend/src/components/Charts.jsx +++ b/frontend/src/components/Charts.jsx @@ -12,6 +12,7 @@ import { } from 'recharts' import { useThemeColors } from '../lib/colors' import { dateTimeLabel, dayLabel, timeLabel } from '../lib/format' +import { useT } from '../lib/i18n' export function Legend({ series }) { if (series.length < 2) return null @@ -28,6 +29,7 @@ export function Legend({ series }) { } function TooltipBox({ title, rows, total }) { + const { t } = useT() return (
{title}
@@ -46,7 +48,7 @@ function TooltipBox({ title, rows, total }) { ))} {total && ( - Total + {t('chart.total')} {total} )} @@ -58,26 +60,83 @@ function TooltipBox({ title, rows, total }) { const axisTick = (c) => ({ fill: c.muted, fontSize: 11 }) -/** Insert an empty point wherever readings stop for more than a couple of - * buckets (night, outage), so the line breaks instead of bridging the gap. */ -function withGaps(points, bucket) { - if (!bucket || points.length < 2) return points - const out = [points[0]] - for (let i = 1; i < points.length; i++) { - if (points[i].t - points[i - 1].t > bucket * 2.5) out.push({ t: points[i - 1].t + bucket }) - out.push(points[i]) +// Silences up to this long are drawn through; longer ones (night, outage) +// break the line. Covers a unit reporting every ~5 min plus a missed cycle. +const BRIDGE_SECONDS = 900 +const SMOOTHING_RADIUS = { off: 0, light: 1, strong: 3 } + +/** + * Chart rows from API points: a gap row where the whole fleet went silent, + * values interpolated where one series reports less often than the bucket + * (INV5 every ~5 min on a 1-min chart would otherwise have no line at all), + * then an optional centred moving average. Interpolated values are flagged + * `__est` so the tooltip can mark them. + */ +function prepare(points, keys, bucket, smoothing) { + const maxGap = Math.max(BRIDGE_SECONDS, (bucket || 0) * 2.5) + const rows = [] + points.forEach((p, i) => { + if (i && p.t - points[i - 1].t > maxGap) rows.push({ t: points[i - 1].t + (bucket || 1) }) + rows.push({ ...p }) + }) + for (const key of keys) { + let prev = -1 + rows.forEach((r, i) => { + if (r[key] == null) return + if (prev >= 0 && i - prev > 1 && r.t - rows[prev].t <= maxGap) { + const a = rows[prev] + for (let j = prev + 1; j < i; j++) { + rows[j][key] = a[key] + ((r[key] - a[key]) * (rows[j].t - a.t)) / (r.t - a.t) + rows[j][`${key}__est`] = true + } + } + prev = i + }) } - return out + const radius = SMOOTHING_RADIUS[smoothing] ?? 0 + if (radius) { + for (const key of keys) { + const raw = rows.map((r) => r[key]) + rows.forEach((r, i) => { + if (raw[i] == null) return + // Only neighbours on the same unbroken stretch of line + let sum = raw[i] + let n = 1 + for (let j = i - 1; j >= i - radius && j >= 0 && raw[j] != null; j--) { + sum += raw[j] + n++ + } + for (let j = i + 1; j <= i + radius && j < raw.length && raw[j] != null; j++) { + sum += raw[j] + n++ + } + r[key] = sum / n + }) + } + } + return rows } /** * Lines over time. points: [{t: unix seconds, [series.key]: number}] * series: [{key, name, color}]. Gaps (night, outages) stay gaps. */ -export function TimeLines({ points, series, from, to, bucket, format, axisFormat, height = 240, showTotal = false }) { +export function TimeLines({ + points, + series, + from, + to, + bucket, + format, + axisFormat, + height = 240, + showTotal = false, + smoothing = 'off', +}) { const c = useThemeColors() const span = to - from - const data = useMemo(() => withGaps(points, bucket), [points, bucket]) + const keys = series.map((s) => s.key).join(',') + const data = useMemo(() => prepare(points, keys.split(','), bucket, smoothing), [points, keys, bucket, smoothing]) return (
@@ -105,9 +164,10 @@ export function TimeLines({ points, series, from, to, bucket, format, axisFormat cursor={{ stroke: c.axis, strokeWidth: 1 }} content={({ active, payload, label }) => { if (!active || !payload?.length) return null + const row = payload[0].payload const rows = series - .filter((s) => payload[0].payload[s.key] != null) - .map((s) => ({ ...s, value: format(payload[0].payload[s.key]) })) + .filter((s) => row[s.key] != null) + .map((s) => ({ ...s, value: (row[`${s.key}__est`] ? '≈ ' : '') + format(row[s.key]) })) const total = showTotal ? format(series.reduce((sum, s) => sum + (payload[0].payload[s.key] ?? 0), 0)) : null @@ -121,6 +181,7 @@ export function TimeLines({ points, series, from, to, bucket, format, axisFormat name={s.name} stroke={s.color} strokeWidth={2} + type={smoothing === 'off' ? 'linear' : 'monotone'} dot={false} activeDot={{ r: 4, stroke: c.surface, strokeWidth: 2 }} connectNulls={false} @@ -191,10 +252,11 @@ export function DayBars({ days, series, format, axisFormat, height = 220 }) { } export function EnergyFootnote({ estimated }) { + const { t } = useT() return (
- {estimated && '≈ Estimated from AC power readings until the energy counters are decoded'} - axis in kWh + {estimated && t('energy.footEstimated')} + {t('energy.axis')}
) } diff --git a/frontend/src/components/Comparison.jsx b/frontend/src/components/Comparison.jsx index 2ea8a58..07e98da 100644 --- a/frontend/src/components/Comparison.jsx +++ b/frontend/src/components/Comparison.jsx @@ -1,35 +1,112 @@ import { Link } from 'react-router-dom' import { seriesCssVar } from '../lib/colors' import { pct, power } from '../lib/format' +import { useT } from '../lib/i18n' +import { useSettings } from '../lib/settings' import Icon from './Icon' import { STATES } from './Health' +/** Smallest "round" number ≥ x (1, 1.5, 2, 2.5, 3, 4, 5, 6, 8 × 10ⁿ), so the + * auto scale doesn't jump with every reading. */ +function niceCeil(x) { + if (!(x > 0)) return 1 + const mag = 10 ** Math.floor(Math.log10(x)) + const step = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10].find((s) => s * mag >= x * 0.9999) + return step * mag +} + +function median(values) { + const v = [...values].sort((a, b) => a - b) + const m = Math.floor(v.length / 2) + return v.length % 2 ? v[m] : (v[m - 1] + v[m]) / 2 +} + +/** AC power to compare: the latest reading, or the mean of the last n, which + * evens out one reading taken while a cloud shaded that array. */ +function watts(st, n) { + if (!st.health.online) return null + if (n > 1) { + const recent = (st.recent_power ?? []) + .slice(0, n) + .map((p) => p.ac_power_total_w) + .filter((w) => w != null) + if (recent.length) return recent.reduce((a, b) => a + b, 0) / recent.length + } + return st.latest?.ac_power_total_w ?? null +} + /** * Side-by-side output of every inverter — the quickest way to spot one * that is producing far less than the others (blown string fuse, tripped - * breaker, disconnected string). + * breaker, disconnected string). Averaging, scale and percentage reference + * come from the shared settings. */ export default function Comparison({ summary, ids }) { - const rows = summary.inverters + const { t } = useT() + const { settings } = useSettings() + const n = settings.comparison_readings + const customW = settings.comparison_custom_kw ? settings.comparison_custom_kw * 1000 : null + // "Custom" without a value falls back to the default rather than breaking + const scale = settings.comparison_scale === 'custom' && !customW ? 'auto' : settings.comparison_scale + const reference = + settings.comparison_reference === 'custom' && !customW ? 'median' : settings.comparison_reference + // Per kWp once every producing inverter has its array size (as the backend decides) const perKwp = summary.fleet_median_w_per_kwp !== null - const value = (st) => { - const w = st.health.online ? st.latest?.ac_power_total_w ?? null : null - if (w === null) return null - return perKwp && st.inverter.capacity_kwp ? w / st.inverter.capacity_kwp : w + + const rows = summary.inverters.map((st) => { + const w = watts(st, n) + const kwp = st.inverter.capacity_kwp + return { st, w, spec: w === null ? null : perKwp && kwp ? w / kwp : w } + }) + const specs = rows.map((r) => r.spec).filter((v) => v !== null) + const best = specs.length ? Math.max(...specs) : null + const med = specs.length >= 2 ? median(specs) : null + + const ratio = (r) => { + if (r.w === null) return null + if (reference === 'custom') return r.w / customW + if (reference === 'best') return best > 0 ? r.spec / best : null + return med > 0 ? r.spec / med : null } - const max = Math.max(1, ...rows.map((st) => value(st) ?? 0)) + const ceiling = scale === 'auto' ? niceCeil(best ?? 0) : best + const barPct = (r) => { + if (r.w === null) return 0 + const f = scale === 'custom' ? r.w / customW : ceiling > 0 ? r.spec / ceiling : 0 + return Math.min(100, Math.max(r.w > 0 ? 1 : 0, f * 100)) + } + + const caption = [ + n > 1 ? t('cmp.avg', { n }) : t('cmp.latest'), + perKwp && scale !== 'custom' && reference !== 'custom' ? t('cmp.perKwp') : null, + reference === 'custom' + ? t('cmp.vsCustom', { v: power(customW) }) + : reference === 'best' + ? t('cmp.vsBest') + : t('cmp.vsMedian'), + ] + .filter(Boolean) + .join(' · ') + const fullBar = + scale === 'custom' + ? power(customW) + : scale === 'auto' && best > 0 + ? perKwp + ? `${Math.round(ceiling)} W/kWp` + : power(ceiling) + : null return (
-
-

Output comparison

- {perKwp ? 'per kWp · vs median' : 'now · vs median'} +
+

{t('cmp.title')}

+ {caption}
    - {rows.map((st) => { - const v = value(st) + {rows.map((r) => { + const { st } = r const flagged = st.health.issues.some((i) => i.code === 'underperforming') - const state = STATES[st.health.state] + const state = STATES[st.health.state] ?? STATES.unknown + const over = scale === 'custom' && r.w > customW return (
  • {st.inverter.name} - {v === null ? ( + {r.w === null ? ( - {state.label} + {t(`state.${STATES[st.health.state] ? st.health.state : 'unknown'}`)} ) : ( <> - {power(st.latest.ac_power_total_w)} + {power(r.w)} {flagged && } - {pct(st.health.relative_output)} + {pct(ratio(r))} )}
-
+
@@ -80,6 +157,9 @@ export default function Comparison({ summary, ids }) { ) })} + {fullBar && ( +
{t('cmp.fullBar', { v: fullBar })}
+ )}
) } diff --git a/frontend/src/components/EditInverterDialog.jsx b/frontend/src/components/EditInverterDialog.jsx index 51293b4..ff177bb 100644 --- a/frontend/src/components/EditInverterDialog.jsx +++ b/frontend/src/components/EditInverterDialog.jsx @@ -1,17 +1,9 @@ import { useState } from 'react' import { api } from '../lib/api' +import { rememberApiKey, storedApiKey } from '../lib/apiKey' +import { useT } from '../lib/i18n' import Icon from './Icon' -const KEY_STORAGE = 'solar.apiKey' - -function storedKey() { - try { - return localStorage.getItem(KEY_STORAGE) ?? '' - } catch { - return '' - } -} - function Field({ label, hint, children }) { return (