Collection
Industry / Water & Dams · v0.2.2Validated
Inspect pressure sensors, inlet flow and reported incidents on a distribution network map.
Distribution Pressure Map · Preview
Loading widget…
Preparing source code…
Idle
Industry / Water & Dams · v0.2.2Validated
Inspect pressure sensors, inlet flow and reported incidents on a distribution network map.
Loading widget…
Preparing source code…
Import @lumo/widgets/widget-utilities.css in your Tailwind stylesheet, alongside the Lumo theme.
import "@lumo/tokens/tokens.css";
import { PressureZonesWidget } from "@lumo/widgets/catalog/water/pressure-zones";
export default function Example() {
return <PressureZonesWidget />;
}Props for integrating the React component in your app.
snapshotPressureNetworkSnapshot | null (optional)simulateboolean (optional){
"snapshot": {
"network": "Canal district",
"observedAtLabel": "28 Nov · 09:15 CET",
"stale": false,
"lowBar": 3,
"criticalBar": 2,
"inletLps": 182,
"affectedProperties": 0,
"zones": [
{
"id": "intake",
"name": "Intake",
"x": 20,
"y": 55,
"pressureBar": 5.2
},
{
"id": "lock",
"name": "Old Lock",
"x": 65,
"y": 44,
"pressureBar": 2.7
}
],
"pipes": [
{
"from": "intake",
"to": "lock",
"status": "open"
}
]
}
}"use client";
import { CircleAlert, Network } from "lucide-react";
import { useEffect, useId, useMemo, useRef, useState } from "react";
import { useReducedMotion } from "motion/react";
import { AnimatedNumber, Badge, Button, WidgetCard, WidgetDialog } from "@lumo/ui";
import shared from "../../shared/operations-list.module.css";
import report from "../../shared/reading-report.module.css";
import { pressureDemo, type PressureDemoPhase } from "./pressure-zones.constants";
import type { PressureNetworkSnapshot } from "./pressure-zones.types";
import styles from "./pressure-zones.module.css";
export type { PressureNetworkSnapshot, PressureZone } from "./pressure-zones.types";
const nonnegative = (value: number | null | undefined): value is number =>
typeof value === "number" && Number.isFinite(value) && value >= 0;
const formatBar = (value: number | null | undefined) => nonnegative(value) ? `${value.toFixed(1)} bar` : "Unavailable";
export function PressureZonesWidget({ snapshot, simulate = true }: { index?: number; snapshot?: PressureNetworkSnapshot | null; simulate?: boolean } = {}) {
const reduced = useReducedMotion();
const [phase, setPhase] = useState<PressureDemoPhase>("normal");
const [ack, setAck] = useState(false);
const [selected, setSelected] = useState("N-04");
const [sheet, setSheet] = useState<"sensors" | "incident" | "confirm">();
const opener = useRef<HTMLButtonElement>(null);
const dialogId = useId();
const demo = snapshot === undefined;
const data = useMemo(() => demo ? pressureDemo(phase, ack) : snapshot, [ack, demo, phase, snapshot]);
useEffect(() => {
if (!demo || !simulate || reduced || phase !== "normal") return;
const timer = window.setTimeout(() => { setPhase("incident"); setAck(false); }, 4200);
return () => window.clearTimeout(timer);
}, [demo, phase, reduced, simulate]);
const zones = useMemo(() => [...new Map((data?.zones ?? []).filter(zone =>
zone.id && nonnegative(zone.x) && zone.x <= 100 && nonnegative(zone.y) && zone.y <= 100).map(zone => [zone.id, zone])).values()], [data?.zones]);
const point = zones.find(zone => zone.id === selected) ?? zones[0];
const zoneMap = useMemo(() => new Map(zones.map(zone => [zone.id, zone])), [zones]);
const incident = data?.incident;
const critical = nonnegative(data?.criticalBar) ? data.criticalBar : null;
const low = nonnegative(data?.lowBar) && (critical === null || data.lowBar >= critical) ? data.lowBar : null;
const limit = low ?? critical;
const belowLimit = limit === null || data?.stale ? 0 : zones.filter(zone => nonnegative(zone.pressureBar) && zone.pressureBar < limit).length;
const sensorAlert = belowLimit ? `${belowLimit} ${belowLimit === 1 ? "sensor" : "sensors"} below limit` : null;
const incidentStatus = incident && (incident.status === "isolated" ? "Isolated" : incident.acknowledged ? "Acknowledged" : "Open incident");
const freshness = [data?.stale ? "Last known" : "Updated", data?.observedAtLabel].filter(Boolean).join(" · ");
const footer = data?.stale ? freshness : [incidentStatus, sensorAlert].filter(Boolean).join(" · ") ||
(data?.observedAtLabel ? freshness : "No incident reported");
function state(value: number | null) {
if (!nonnegative(value)) return ["Unknown", "neutral"] as const;
if (data?.stale) return ["Out of date", "neutral"] as const;
if (critical !== null && value < critical) return ["Critical", "danger"] as const;
if (low !== null && value < low) return ["Low pressure", "warning"] as const;
return [critical === null && low === null ? "Reported" : "Within limits", "neutral"] as const;
}
function open(next: NonNullable<typeof sheet>, button: HTMLButtonElement) {
opener.current = button;
setSheet(next);
}
const aliveSheet = sheet === "sensors" ? zones.length > 0 : Boolean(sheet && incident);
return <WidgetDialog id={dialogId} className={styles.frame} placement="bottom" open={aliveSheet}
title={sheet === "sensors" ? "Pressure sensors" : sheet === "confirm" && demo ? "Confirm sample isolation" : "Network incident"}
returnFocusRef={opener} onClose={() => setSheet(undefined)}
surface={<WidgetCard title="Network Pressure" label="Distribution network pressure" description={data?.network}
size="large" icon={<Network />} className={shared.largeCard}
action={point && <Button type="button" size="xs" aria-haspopup="dialog" aria-label={`Sensors, selected ${point.name}`}
variant="ghost" onClick={event => open("sensors", event.currentTarget)}>Sensors</Button>}>
{point ? <div className={report.report}>
<div className={styles.map} role="group" aria-label="Distribution network map">
<div className={styles.plot}>
<svg viewBox="0 0 100 100" preserveAspectRatio="none" aria-hidden="true">
{(data?.pipes ?? []).map(({ from, to, status }, i) => {
const start = zoneMap.get(from);
const end = zoneMap.get(to);
return start && end ? <line key={`${from}-${to}-${i}`} x1={start.x} y1={start.y}
x2={end.x} y2={end.y} data-status={status ?? "open"} /> : null;
})}
</svg>
{zones.map(zone => {
const [label, tone] = state(zone.pressureBar);
return <div key={zone.id} className={styles.nodePosition} style={{ left: `${zone.x}%`, top: `${zone.y}%` }}>
<Button type="button" size="xs" variant="secondary" className={styles.node} data-tone={tone}
aria-label={`${zone.name}, ${formatBar(zone.pressureBar)}, ${label}`} aria-pressed={point.id === zone.id}
onClick={() => setSelected(zone.id)}>{tone === "danger" || tone === "warning" ? <CircleAlert aria-hidden="true" /> : <span aria-hidden="true" />}</Button>
</div>;
})}
</div>
</div>
<div className={report.reading}><div><span>{point.name} · {point.id}</span>
<strong>{nonnegative(point.pressureBar) ? <><AnimatedNumber value={point.pressureBar} decimals={1} /> <small>bar</small></> : "Unavailable"}</strong></div>
<Badge size="sm" tone={state(point.pressureBar)[1]}>{state(point.pressureBar)[0]}</Badge>
</div>
<dl className={report.facts}>
<div><dt>Inlet flow</dt><dd>{nonnegative(data?.inletLps) ? `${data.inletLps} L/s` : "Unavailable"}</dd></div>
<div><dt>Properties affected</dt><dd>{nonnegative(data?.affectedProperties) ? data.affectedProperties : "Unavailable"}</dd></div>
</dl>
<div className={report.foot}>
<span>{footer}</span>
{incident && <Button type="button" variant="secondary" size="xs" aria-haspopup="dialog"
onClick={event => open("incident", event.currentTarget)}>Incident</Button>}
</div>
</div> : <p className={shared.muted}>{data ? "No pressure sensors reported" : "Pressure report unavailable"}</p>}
</WidgetCard>}>
{sheet === "sensors" ? <>
{(data?.observedAtLabel || data?.stale) && <p className={shared.muted}>{freshness}</p>}
<ul className={shared.sheetList} data-widget-list>
{zones.map(zone => <li key={zone.id} data-selected={point?.id === zone.id || undefined}><button type="button" className={shared.row}
aria-pressed={point?.id === zone.id} onClick={() => { setSelected(zone.id); setSheet(undefined); }}>
<span className={shared.identity}><strong>{zone.name}</strong><span>{zone.id} · {state(zone.pressureBar)[0]}</span></span>
<strong className={shared.reading}>{formatBar(zone.pressureBar)}</strong>
</button></li>)}
</ul>
</> : incident && <div className={shared.details}>
<strong>{incident.label}</strong>
<p>{incidentStatus}</p>
{(data?.observedAtLabel || data?.stale) && <p>{freshness}</p>}
{demo && <div className={styles.actions}>
{sheet === "confirm" ? <>
<Button type="button" size="xs" variant="ghost" onClick={() => setSheet("incident")}>Cancel</Button>
<Button type="button" size="xs" onClick={() => { setAck(true); setPhase("isolated"); setSheet("incident"); }}>Confirm isolation</Button>
</> : phase === "incident" ? <>
{!ack && <Button type="button" size="xs" variant="secondary" onClick={() => setAck(true)}>Acknowledge</Button>}
<Button type="button" size="xs" onClick={() => setSheet("confirm")}>Isolate sample</Button>
</> : <Button type="button" size="xs" onClick={() => { setPhase("normal"); setSheet(undefined); }}>Reset sample</Button>}
</div>}
</div>}
</WidgetDialog>;
}
lucide-reactmotionreactreact-dom