Simplify daemon URL logic for dev/prod modes and remove unused action endpoints

Update daemon URL configuration to default to mock mode in dev (empty string) and same-origin in production. Refactor loadHistory to properly map API response rows to EnvironmentReading objects with vpd calculation. Remove unused endpoint parameter from BagDetail postAction function.
This commit is contained in:
grove5_admin committed 2026-04-29 22:49:18 -07:00
1 parent e2cd636afa
commit 5f070523be
2 files changed
+28 -20

No files matched your search

+23 -13
View File
@@ -13,11 +13,14 @@ function deepClone<T>(v: T): T {
}
// ── Daemon URL ─────────────────────────────────────────────────────────────
// VITE_DAEMON_URL overrides the default daemon URL to connect to live daemon.
// Default: use hostname from current page.
const DAEMON_URL = (import.meta as any).env?.VITE_DAEMON_URL as string
?? `${window.location.protocol}//${window.location.host}`;
const WS_URL = DAEMON_URL.replace(/^http/, 'ws') + '/ws';
// VITE_DAEMON_URL overrides daemon URL to connect to a live daemon.
// In dev, default to mock mode unless explicitly set.
// In production (UI served by daemon), default to same-origin.
const envDaemonUrl = ((import.meta as any).env?.VITE_DAEMON_URL as string | undefined)?.trim();
const DAEMON_URL = envDaemonUrl
? envDaemonUrl
: ((import.meta as any).env?.DEV ? '' : `${window.location.protocol}//${window.location.host}`);
const WS_URL = DAEMON_URL ? `${DAEMON_URL.replace(/^http/, 'ws')}/ws` : '';
// ── VPD calculation ────────────────────────────────────────────────────────
// Vapour Pressure Deficit from temp and RH.
@@ -292,15 +295,22 @@ function createStore() {
`${DAEMON_URL}/api/environment/${chamber_id}/history?hours=${hours}`
);
if (!res.ok) return;
const rows = await res.json();
const rows: any[] = await res.json();
if (!rows.length) return;
// Update the correct chamber's history
const chamber = state.chambers.find(c => c.id === chamber_id);
if (chamber && rows.length) {
// Also backfill global history from fruiting (has SCD43)
if (chamber_id === 'fruiting') {
state.environment.history = rows;
}
const readings: EnvironmentReading[] = rows.map(r => ({
ts: r.ts,
temp_c: r.temp_c ?? 0,
rh_pct: r.rh_pct ?? 0,
co2_ppm: r.co2_ppm ?? 0,
co2e_ppm: r.co2e_ppm ?? undefined,
voc_ppb: r.voc_ppb ?? 0,
vpd: calcVpd(r.temp_c ?? 0, r.rh_pct ?? 0),
}));
// Backfill global environment history from fruiting (has SCD43 — most accurate)
if (chamber_id === 'fruiting') {
state.environment.history = readings;
}
} catch (e) {
console.warn(`[cultivar] Failed to load history for ${chamber_id}`, e);