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:
1 parent
e2cd636afa
commit
5f070523be
2 files changed
+28
-20
No files matched your search
+23
-13
@@ -13,11 +13,14 @@ function deepClone<T>(v: T): T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ── Daemon URL ─────────────────────────────────────────────────────────────
|
// ── Daemon URL ─────────────────────────────────────────────────────────────
|
||||||
// VITE_DAEMON_URL overrides the default daemon URL to connect to live daemon.
|
// VITE_DAEMON_URL overrides daemon URL to connect to a live daemon.
|
||||||
// Default: use hostname from current page.
|
// In dev, default to mock mode unless explicitly set.
|
||||||
const DAEMON_URL = (import.meta as any).env?.VITE_DAEMON_URL as string
|
// In production (UI served by daemon), default to same-origin.
|
||||||
?? `${window.location.protocol}//${window.location.host}`;
|
const envDaemonUrl = ((import.meta as any).env?.VITE_DAEMON_URL as string | undefined)?.trim();
|
||||||
const WS_URL = DAEMON_URL.replace(/^http/, 'ws') + '/ws';
|
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 ────────────────────────────────────────────────────────
|
// ── VPD calculation ────────────────────────────────────────────────────────
|
||||||
// Vapour Pressure Deficit from temp and RH.
|
// Vapour Pressure Deficit from temp and RH.
|
||||||
@@ -292,15 +295,22 @@ function createStore() {
|
|||||||
`${DAEMON_URL}/api/environment/${chamber_id}/history?hours=${hours}`
|
`${DAEMON_URL}/api/environment/${chamber_id}/history?hours=${hours}`
|
||||||
);
|
);
|
||||||
if (!res.ok) return;
|
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 readings: EnvironmentReading[] = rows.map(r => ({
|
||||||
const chamber = state.chambers.find(c => c.id === chamber_id);
|
ts: r.ts,
|
||||||
if (chamber && rows.length) {
|
temp_c: r.temp_c ?? 0,
|
||||||
// Also backfill global history from fruiting (has SCD43)
|
rh_pct: r.rh_pct ?? 0,
|
||||||
if (chamber_id === 'fruiting') {
|
co2_ppm: r.co2_ppm ?? 0,
|
||||||
state.environment.history = rows;
|
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) {
|
} catch (e) {
|
||||||
console.warn(`[cultivar] Failed to load history for ${chamber_id}`, e);
|
console.warn(`[cultivar] Failed to load history for ${chamber_id}`, e);
|
||||||
|
|||||||
@@ -60,12 +60,10 @@
|
|||||||
// Quick actions
|
// Quick actions
|
||||||
let actionPending = $state<string | null>(null);
|
let actionPending = $state<string | null>(null);
|
||||||
|
|
||||||
async function postAction(endpoint: string, label: string) {
|
async function postAction(label: string) {
|
||||||
actionPending = label;
|
actionPending = label;
|
||||||
try {
|
await Promise.resolve();
|
||||||
await fetch(endpoint, { method: 'POST' });
|
actionPending = null;
|
||||||
} catch { /* mock: ignore */ }
|
|
||||||
finally { actionPending = null; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const daysSinceInoc = $derived(
|
const daysSinceInoc = $derived(
|
||||||
@@ -159,7 +157,7 @@
|
|||||||
<button
|
<button
|
||||||
class="action-btn action-harvest"
|
class="action-btn action-harvest"
|
||||||
disabled={actionPending !== null}
|
disabled={actionPending !== null}
|
||||||
onclick={() => postAction(`/api/bags/${bagId}/harvest`, 'harvest')}
|
onclick={() => postAction('harvest')}
|
||||||
type="button"
|
type="button"
|
||||||
>
|
>
|
||||||
<span class="action-icon">✂</span>
|
<span class="action-icon">✂</span>
|
||||||
@@ -172,7 +170,7 @@
|
|||||||
<button
|
<button
|
||||||
class="action-btn action-flag"
|
class="action-btn action-flag"
|
||||||
disabled={actionPending !== null}
|
disabled={actionPending !== null}
|
||||||
onclick={() => postAction(`/api/bags/${bagId}/flag`, 'flag')}
|
onclick={() => postAction('flag')}
|
||||||
type="button"
|
type="button"
|
||||||
>
|
>
|
||||||
<span class="action-icon">⚑</span>
|
<span class="action-icon">⚑</span>
|
||||||
|
|||||||
Reference in new issue
Block a user