refactor(ops): use units-embedded equipments, lazy-load config data
Ops view now reads equipment+role_points from state.units (returned by unit list API) instead of state.equipments, eliminating the loadEquipments() call on bootstrap. Config data (sources, equipments, events, points) is deferred until the user first switches to config view. On WS reconnect, loadEquipments is only refreshed when config view is active. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
0545388b85
commit
4338895e0a
|
|
@ -35,6 +35,8 @@ import { state } from "./state.js";
|
||||||
import { loadSources, saveSource } from "./sources.js";
|
import { loadSources, saveSource } from "./sources.js";
|
||||||
import { closeUnitModal, loadUnits, openCreateUnitModal, resetUnitForm, renderUnits, saveUnit } from "./units.js";
|
import { closeUnitModal, loadUnits, openCreateUnitModal, resetUnitForm, renderUnits, saveUnit } from "./units.js";
|
||||||
|
|
||||||
|
let _configLoaded = false;
|
||||||
|
|
||||||
function switchView(view) {
|
function switchView(view) {
|
||||||
state.activeView = view;
|
state.activeView = view;
|
||||||
const main = document.querySelector("main");
|
const main = document.querySelector("main");
|
||||||
|
|
@ -60,6 +62,15 @@ function switchView(view) {
|
||||||
|
|
||||||
if (view === "config") {
|
if (view === "config") {
|
||||||
startLogs();
|
startLogs();
|
||||||
|
if (!_configLoaded) {
|
||||||
|
_configLoaded = true;
|
||||||
|
withStatus(Promise.all([
|
||||||
|
loadSources(),
|
||||||
|
loadEquipments(),
|
||||||
|
loadEvents(),
|
||||||
|
loadPoints(),
|
||||||
|
]));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
stopLogs();
|
stopLogs();
|
||||||
}
|
}
|
||||||
|
|
@ -161,13 +172,13 @@ function bindEvents() {
|
||||||
|
|
||||||
document.addEventListener("equipments-updated", () => {
|
document.addEventListener("equipments-updated", () => {
|
||||||
renderUnits();
|
renderUnits();
|
||||||
renderOpsUnits();
|
// Re-fetch units so embedded equipment data stays in sync with config changes.
|
||||||
if (!state.selectedOpsUnitId) loadAllEquipmentCards();
|
loadUnits().catch(() => {});
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener("units-loaded", () => {
|
document.addEventListener("units-loaded", () => {
|
||||||
renderOpsUnits();
|
renderOpsUnits();
|
||||||
if (state.equipments.length > 0 && !state.selectedOpsUnitId) loadAllEquipmentCards();
|
if (!state.selectedOpsUnitId) loadAllEquipmentCards();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -182,10 +193,6 @@ async function bootstrap() {
|
||||||
|
|
||||||
await withStatus(loadUnits());
|
await withStatus(loadUnits());
|
||||||
startOps();
|
startOps();
|
||||||
await withStatus(loadSources());
|
|
||||||
await withStatus(loadEquipments());
|
|
||||||
await withStatus(loadEvents());
|
|
||||||
await withStatus(loadPoints());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ export function startPointSocket() {
|
||||||
_reconnectDelay = 1000;
|
_reconnectDelay = 1000;
|
||||||
if (_connectedOnce) {
|
if (_connectedOnce) {
|
||||||
loadUnits().catch(() => {});
|
loadUnits().catch(() => {});
|
||||||
loadEquipments().catch(() => {});
|
if (state.activeView === "config") loadEquipments().catch(() => {});
|
||||||
}
|
}
|
||||||
_connectedOnce = true;
|
_connectedOnce = true;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -84,18 +84,18 @@ function selectOpsUnit(unitId) {
|
||||||
state.opsPointEls.clear();
|
state.opsPointEls.clear();
|
||||||
|
|
||||||
if (!state.selectedOpsUnitId) {
|
if (!state.selectedOpsUnitId) {
|
||||||
renderOpsEquipments(state.equipments);
|
renderOpsEquipments(state.units.flatMap((u) => u.equipments || []));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filtered = state.equipments.filter((eq) => eq.unit_id === unitId);
|
const unit = state.unitMap.get(unitId);
|
||||||
renderOpsEquipments(filtered);
|
renderOpsEquipments(unit ? (unit.equipments || []) : []);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loadAllEquipmentCards() {
|
export function loadAllEquipmentCards() {
|
||||||
if (!dom.opsEquipmentArea) return;
|
if (!dom.opsEquipmentArea) return;
|
||||||
state.opsPointEls.clear();
|
state.opsPointEls.clear();
|
||||||
renderOpsEquipments(state.equipments);
|
renderOpsEquipments(state.units.flatMap((u) => u.equipments || []));
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderOpsEquipments(equipments) {
|
function renderOpsEquipments(equipments) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue