Collection
Mobility / Smart City · v0.2.2Validated
Compare night-time sound levels with each sensor's reported limit and inspect individual readings.
Overnight Sound Levels · Preview
Loading widget…
Preparing source code…
Idle
Mobility / Smart City · v0.2.2Validated
Compare night-time sound levels with each sensor's reported limit and inspect individual readings.
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 { NoiseLevelWidget } from "@lumo/widgets/catalog/smart-city/noise-level";
export default function Example() {
return <NoiseLevelWidget />;
}Props for integrating the React component in your app.
snapshotNightNoiseSnapshot | null (optional){
"snapshot": {
"periodLabel": "28 Nov · 00:08–00:10",
"sensors": [
{
"id": "pier",
"name": "Canal Landing",
"limitDba": 55,
"readings": [
{
"id": "1",
"label": "00:08",
"dba": 51
},
{
"id": "2",
"label": "00:09",
"dba": 54
}
]
}
]
}
}"use client";
import { useMemo, useState } from "react";
import { AudioLines } from "lucide-react";
import { useHorizontalSwipe } from "@lumo/hooks";
import { AnimatedNumber, Badge, Button, WidgetCard, WidgetPagination } from "@lumo/ui";
import { WidgetReadingSeries } from "../../shared/WidgetReadingSeries";
import shared from "../../shared/operations-list.module.css";
import styles from "../../shared/reading-report.module.css";
import { noiseDemo } from "./noise-level.constants";
import type { NightNoiseSnapshot } from "./noise-level.types";
export type { NightNoiseReading, NightNoiseSensor, NightNoiseSnapshot } from "./noise-level.types";
const calibrated = (value: number | null | undefined): number | null =>
typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 200 ? value : null;
const labelFor = (value: number | null) => value === null ? "Unavailable" : `${value.toFixed(1)} dB(A)`;
export function NoiseLevelWidget({ snapshot = noiseDemo }: { index?: number; snapshot?: NightNoiseSnapshot | null } = {}) {
const [sensorId, setSensorId] = useState<string>();
const [readingId, setReadingId] = useState<string>();
const sensors = snapshot?.sensors ?? [];
const page = Math.max(0, sensors.findIndex(sensor => sensor.id === sensorId));
const sensor = sensors[page];
const select = (next: number) => {
if (sensors[next] && next !== page) { setSensorId(sensors[next].id); setReadingId(undefined); }
};
const swipe = useHorizontalSwipe(direction => select(page + (direction === "left" ? 1 : -1)), sensors.length > 1);
const points = useMemo(() => [...new Map((sensor?.readings ?? []).map(reading => [reading.id, reading])).values()]
.slice(-24).map(reading => ({ ...reading, value: calibrated(reading.dba) })), [sensor?.readings]);
const values = points.flatMap(point => point.value === null ? [] : [point.value]);
const peak = values.length ? Math.max(...values) : null;
const peakReading = peak === null ? undefined : points.find(point => point.value === peak);
const equivalent = peak === null ? null : peak + 10 * Math.log10(values.reduce((total, reading) => total + 10 ** ((reading - peak) / 10), 0) / values.length);
const selected = points.find(point => point.id === readingId);
const value = selected ? selected.value : equivalent;
const limit = calibrated(sensor?.limitDba);
const over = limit !== null && equivalent !== null && equivalent > limit;
const readingOver = limit !== null && value !== null && value > limit;
return <WidgetCard title="Night Noise" label="Night noise level" description={snapshot?.periodLabel} size="small" icon={<AudioLines />} className={`${shared.smallCard} ${styles.compact}`}>
{sensor ? <div className={`${styles.report} ${styles.swipe}`} {...swipe} data-studio-swipe-ignore>
<div className={styles.reading}><div><span>{sensor.name} · {selected ? selected.label : "Leq"}</span>
<strong>{value === null ? "Unavailable" : <><AnimatedNumber value={value} decimals={1} blur={false} /> <small>dB(A)</small></>}</strong></div>
{limit !== null && <Badge size="sm" tone={readingOver ? "warning" : "neutral"}>{value === null ? "Limit set" : readingOver ? "Above limit" : "Within limit"}</Badge>}
</div>
{points.length ? <WidgetReadingSeries points={points} activeId={selected?.id} onSelect={setReadingId}
label="Inspect sound readings" color={over ? "var(--warning)" : "var(--tone-violet)"} threshold={limit ?? undefined}
describe={point => `${point.label}, ${labelFor(point.value)}`} /> : <p className={shared.muted}>No readings</p>}
<div className={styles.foot}><span>{limit === null ? `${values.length} reported` : `Limit ${limit} dB(A)`}</span>
<div className={styles.footActions}>
{peakReading && <Button type="button" variant={selected?.id === peakReading.id ? "secondary" : "ghost"} size="xs"
aria-label={`Show peak reading, ${labelFor(peak)}`}
aria-pressed={selected?.id === peakReading.id} onClick={() => setReadingId(peakReading.id)}>Peak {peak?.toFixed(1)}</Button>}
<Button type="button" variant={selected ? "ghost" : "secondary"} size="xs" aria-label="Show Leq summary" aria-pressed={!selected}
onClick={() => setReadingId(undefined)}>Leq</Button>
</div>
</div>
<WidgetPagination page={page} count={sensors.length} onChange={select} label="Noise sensors" pageLabel="Sensor" pageLabels={sensors.map(item => item.name)} />
</div> : <p className={shared.muted}>{snapshot ? "No noise sensors" : "Sound report unavailable"}</p>}
</WidgetCard>;
}
lucide-reactmotionreactreact-dom