@sjvair/monitor-map 4.0.3 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +94 -14
  2. package/dist/lib/LazyMapLayout.svelte +53 -0
  3. package/dist/lib/LazyMapLayout.svelte.d.ts +7 -0
  4. package/dist/lib/LazyMonitorDetailPanel.svelte +33 -0
  5. package/dist/lib/LazyMonitorDetailPanel.svelte.d.ts +4 -0
  6. package/dist/lib/LoadError.svelte +19 -0
  7. package/dist/lib/LoadError.svelte.d.ts +7 -0
  8. package/dist/lib/LoadScreen.svelte +10 -1
  9. package/dist/lib/LoadScreen.svelte.d.ts +6 -16
  10. package/dist/lib/MonitorDetailPanel.svelte +108 -28
  11. package/dist/lib/MonitorMapLayout.svelte +21 -1
  12. package/dist/lib/collocation-sites/collocations-map-integration.svelte.js +1 -1
  13. package/dist/lib/core.d.ts +12 -0
  14. package/dist/lib/core.js +10 -0
  15. package/dist/lib/data-chart/DataChart.svelte +63 -13
  16. package/dist/lib/data-chart/DataChart.svelte.d.ts +10 -5
  17. package/dist/lib/data-chart/DateRange.d.ts +4 -0
  18. package/dist/lib/data-chart/DateRange.js +9 -1
  19. package/dist/lib/data-chart/chart-utils.d.ts +2 -1
  20. package/dist/lib/data-chart/chart-utils.js +19 -23
  21. package/dist/lib/data-chart/level-colors.d.ts +6 -0
  22. package/dist/lib/data-chart/level-colors.js +5 -0
  23. package/dist/lib/data-chart/requests.d.ts +5 -2
  24. package/dist/lib/data-chart/requests.js +2 -3
  25. package/dist/lib/data-chart/tooltip.d.ts +2 -1
  26. package/dist/lib/data-chart/tooltip.js +1 -9
  27. package/dist/lib/ev-stations/ev-stations-cluster-renderer.js +1 -1
  28. package/dist/lib/ev-stations/ev-stations-map-integration.svelte.js +5 -7
  29. package/dist/lib/hms/hms-fire-map-integration.svelte.js +1 -1
  30. package/dist/lib/hms/hms-smoke-map-integration.svelte.js +1 -1
  31. package/dist/lib/index.d.ts +8 -3
  32. package/dist/lib/index.js +6 -1
  33. package/dist/lib/lazy-chunks.d.ts +2 -0
  34. package/dist/lib/lazy-chunks.js +6 -0
  35. package/dist/lib/lazy-routes.d.ts +12 -0
  36. package/dist/lib/lazy-routes.js +18 -0
  37. package/dist/lib/map/Map.svelte +1 -1
  38. package/dist/lib/map/MapShell.svelte +9 -3
  39. package/dist/lib/map/MapShell.svelte.d.ts +5 -0
  40. package/dist/lib/map/integrations/click-manager.js +1 -1
  41. package/dist/lib/map/integrations/map-geojson-integration.svelte.d.ts +13 -0
  42. package/dist/lib/map/integrations/map-geojson-integration.svelte.js +31 -5
  43. package/dist/lib/map/integrations/map-icon-manager.js +1 -1
  44. package/dist/lib/map/integrations/map-layer-integration.svelte.js +1 -1
  45. package/dist/lib/map/integrations/tooltip.svelte.js +1 -1
  46. package/dist/lib/map/map-style-state.svelte.d.ts +10 -0
  47. package/dist/lib/map/map-style-state.svelte.js +57 -6
  48. package/dist/lib/monitors/components/MonitorClusterTooltip.svelte +1 -1
  49. package/dist/lib/monitors/monitor-details.svelte.d.ts +15 -0
  50. package/dist/lib/monitors/monitor-details.svelte.js +63 -0
  51. package/dist/lib/monitors/monitors-cluster-renderer.js +1 -1
  52. package/dist/lib/monitors/monitors-icon-manager.svelte.js +1 -1
  53. package/dist/lib/monitors/monitors-map-integration.svelte.js +5 -7
  54. package/dist/lib/monitors/monitors.svelte.d.ts +22 -1
  55. package/dist/lib/monitors/monitors.svelte.js +75 -8
  56. package/dist/lib/monitors/panel-pollutant.d.ts +37 -0
  57. package/dist/lib/monitors/panel-pollutant.js +43 -0
  58. package/dist/lib/options/OptionsBar.svelte +8 -1
  59. package/dist/lib/options/OptionsMenu.svelte +10 -1
  60. package/dist/lib/search/Search.svelte +1 -1
  61. package/dist/lib/shared-load.d.ts +5 -0
  62. package/dist/lib/shared-load.js +14 -0
  63. package/package.json +13 -2
  64. package/src/lib/LazyMapLayout.svelte +53 -0
  65. package/src/lib/LazyMonitorDetailPanel.svelte +33 -0
  66. package/src/lib/LoadError.svelte +19 -0
  67. package/src/lib/LoadScreen.svelte +10 -1
  68. package/src/lib/MonitorDetailPanel.svelte +108 -28
  69. package/src/lib/MonitorMapLayout.svelte +21 -1
  70. package/src/lib/collocation-sites/collocations-map-integration.svelte.ts +1 -1
  71. package/src/lib/core.ts +17 -0
  72. package/src/lib/data-chart/DataChart.svelte +63 -13
  73. package/src/lib/data-chart/DateRange.ts +11 -1
  74. package/src/lib/data-chart/chart-utils.ts +23 -26
  75. package/src/lib/data-chart/level-colors.ts +13 -0
  76. package/src/lib/data-chart/requests.ts +9 -5
  77. package/src/lib/data-chart/tooltip.ts +2 -10
  78. package/src/lib/ev-stations/ev-stations-cluster-renderer.ts +1 -1
  79. package/src/lib/ev-stations/ev-stations-map-integration.svelte.ts +5 -5
  80. package/src/lib/hms/hms-fire-map-integration.svelte.ts +1 -1
  81. package/src/lib/hms/hms-smoke-map-integration.svelte.ts +1 -1
  82. package/src/lib/index.ts +8 -3
  83. package/src/lib/lazy-chunks.ts +7 -0
  84. package/src/lib/lazy-routes.ts +20 -0
  85. package/src/lib/map/Map.svelte +1 -1
  86. package/src/lib/map/MapShell.svelte +9 -3
  87. package/src/lib/map/integrations/click-manager.ts +1 -1
  88. package/src/lib/map/integrations/map-geojson-integration.svelte.ts +33 -4
  89. package/src/lib/map/integrations/map-icon-manager.ts +1 -1
  90. package/src/lib/map/integrations/map-layer-integration.svelte.ts +1 -1
  91. package/src/lib/map/integrations/tooltip.svelte.ts +1 -1
  92. package/src/lib/map/map-style-state.svelte.ts +59 -7
  93. package/src/lib/monitors/components/MonitorClusterTooltip.svelte +1 -1
  94. package/src/lib/monitors/monitor-details.svelte.ts +66 -0
  95. package/src/lib/monitors/monitors-cluster-renderer.ts +1 -1
  96. package/src/lib/monitors/monitors-icon-manager.svelte.ts +1 -1
  97. package/src/lib/monitors/monitors-map-integration.svelte.ts +5 -5
  98. package/src/lib/monitors/monitors.svelte.ts +93 -10
  99. package/src/lib/monitors/panel-pollutant.ts +76 -0
  100. package/src/lib/options/OptionsBar.svelte +8 -1
  101. package/src/lib/options/OptionsMenu.svelte +10 -1
  102. package/src/lib/search/Search.svelte +1 -1
  103. package/src/lib/shared-load.ts +15 -0
  104. /package/dist/lib/map/{map.svelte.d.ts → map-manager.svelte.d.ts} +0 -0
  105. /package/dist/lib/map/{map.svelte.js → map-manager.svelte.js} +0 -0
  106. /package/src/lib/map/{map.svelte.ts → map-manager.svelte.ts} +0 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sjvair/monitor-map",
3
3
  "description": "SJVAir's interactive monitor map",
4
- "version": "4.0.3",
4
+ "version": "4.2.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/SJVAir/monitor-map"
@@ -16,7 +16,8 @@
16
16
  "check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
17
17
  "format": "prettier --write .",
18
18
  "lint": "prettier --check . && eslint .",
19
- "test": "vitest run"
19
+ "test": "vitest run",
20
+ "test:bundle": "npm run build:lib && node tests/bundle/check-bundle.mjs"
20
21
  },
21
22
  "type": "module",
22
23
  "svelte": "./dist/lib/index.js",
@@ -26,6 +27,16 @@
26
27
  "svelte": "./dist/lib/index.js",
27
28
  "types": "./dist/lib/index.d.ts",
28
29
  "default": "./dist/lib/index.js"
30
+ },
31
+ "./core": {
32
+ "svelte": "./dist/lib/core.js",
33
+ "types": "./dist/lib/core.d.ts",
34
+ "default": "./dist/lib/core.js"
35
+ },
36
+ "./routes": {
37
+ "svelte": "./dist/lib/lazy-routes.js",
38
+ "types": "./dist/lib/lazy-routes.d.ts",
39
+ "default": "./dist/lib/lazy-routes.js"
29
40
  }
30
41
  },
31
42
  "files": [
@@ -0,0 +1,53 @@
1
+ <script lang="ts">
2
+ import type { Component, Snippet } from "svelte";
3
+ import LoadScreen from "./LoadScreen.svelte";
4
+ import LoadError from "./LoadError.svelte";
5
+ import { loadMonitorDetailPanel, loadMonitorMapLayout } from "./lazy-chunks";
6
+ import { useMonitorMapRouter } from "./router-context";
7
+
8
+ interface Props {
9
+ children: Snippet;
10
+ }
11
+
12
+ let { children }: Props = $props();
13
+
14
+ const { route, basePath } = useMonitorMapRouter();
15
+
16
+ let Layout: Component<Props> | null = $state.raw(null);
17
+ let failed = $state(false);
18
+
19
+ function load() {
20
+ failed = false;
21
+ loadMonitorMapLayout().then(
22
+ (module) => (Layout = module.default),
23
+ (error) => {
24
+ console.error(error);
25
+ failed = true;
26
+ }
27
+ );
28
+ }
29
+
30
+ load();
31
+ // On a link straight to a monitor, fetch its panel alongside the layout; the panel's own
32
+ // wrapper only mounts (and would only ask for it) once the layout has arrived. A failure is
33
+ // reported by that wrapper when it asks again.
34
+ if (route.pathname.startsWith(`${basePath}/monitor/`)) {
35
+ loadMonitorDetailPanel().catch(() => {});
36
+ }
37
+ </script>
38
+
39
+ <!-- The layout renders the same LoadScreen from the same shared state, so it carries on
40
+ from this one without restarting. -->
41
+ {#if Layout}
42
+ <Layout>{@render children()}</Layout>
43
+ {:else}
44
+ <div class="relative h-full w-full">
45
+ <LoadScreen>
46
+ {#snippet error()}
47
+ {#if failed}
48
+ <LoadError message="Couldn't load the map." onretry={load} />
49
+ {/if}
50
+ {/snippet}
51
+ </LoadScreen>
52
+ </div>
53
+ {/if}
@@ -0,0 +1,33 @@
1
+ <script lang="ts">
2
+ import type { Component } from "svelte";
3
+ import LoadError from "./LoadError.svelte";
4
+ import { loadMonitorDetailPanel } from "./lazy-chunks";
5
+
6
+ let Panel: Component | null = $state.raw(null);
7
+ let failed = $state(false);
8
+
9
+ function load() {
10
+ failed = false;
11
+ loadMonitorDetailPanel().then(
12
+ (module) => (Panel = module.default),
13
+ (error) => {
14
+ console.error(error);
15
+ failed = true;
16
+ }
17
+ );
18
+ }
19
+
20
+ load();
21
+ </script>
22
+
23
+ {#if Panel}
24
+ <Panel />
25
+ {:else}
26
+ <div class="flex h-full w-full items-center justify-center bg-white">
27
+ {#if failed}
28
+ <LoadError message="Couldn't load this monitor." onretry={load} />
29
+ {:else}
30
+ <p class="text-sm text-gray-500">Loading…</p>
31
+ {/if}
32
+ </div>
33
+ {/if}
@@ -0,0 +1,19 @@
1
+ <script lang="ts">
2
+ interface Props {
3
+ message: string;
4
+ onretry: () => void;
5
+ }
6
+
7
+ let { message, onretry }: Props = $props();
8
+ </script>
9
+
10
+ <div class="flex flex-col items-center gap-3 px-4 text-center" role="alert">
11
+ <p class="text-gray-700">{message}</p>
12
+ <button
13
+ type="button"
14
+ class="rounded-md bg-gray-800 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700"
15
+ onclick={onretry}
16
+ >
17
+ Try again
18
+ </button>
19
+ </div>
@@ -23,8 +23,16 @@
23
23
  </script>
24
24
 
25
25
  <script lang="ts">
26
+ import type { Snippet } from "svelte";
26
27
  import type { Attachment } from "svelte/attachments";
27
28
 
29
+ interface Props {
30
+ /** Rendered under the logo while the load screen is up, e.g. a load failure and a retry */
31
+ error?: Snippet;
32
+ }
33
+
34
+ let { error }: Props = $props();
35
+
28
36
  const animationTrigger: Attachment<HTMLDivElement> = () => {
29
37
  loadScreenState.entered = true;
30
38
  };
@@ -32,7 +40,7 @@
32
40
 
33
41
  {#if loadScreenState.enabled}
34
42
  <div
35
- class="absolute top-0 left-0 z-50 flex h-full w-full items-center justify-center overflow-hidden bg-white"
43
+ class="absolute top-0 left-0 z-50 flex h-full w-full flex-col items-center justify-center gap-6 overflow-hidden bg-white"
36
44
  {@attach animationTrigger}
37
45
  >
38
46
  <div class="flex justify-around gap-4">
@@ -64,5 +72,6 @@
64
72
  class:opacity-0={!loadScreenState.entered}
65
73
  />
66
74
  </div>
75
+ {@render error?.()}
67
76
  </div>
68
77
  {/if}
@@ -1,11 +1,14 @@
1
1
  <script lang="ts">
2
- import type { ComponentProps } from "svelte";
2
+ import { untrack, type ComponentProps } from "svelte";
3
3
  import type { SJVAirEntryLevel } from "@sjvair/sdk";
4
4
  import DataBox from "$lib/components/DataBox.svelte";
5
+ import LoadError from "$lib/LoadError.svelte";
5
6
  import MonitorWidgetModal from "$lib/monitors/components/MonitorWidgetModal.svelte";
6
7
  import MonitorSubscriptionManager from "$lib/monitors/components/MonitorSubscriptionManager.svelte";
7
8
  import DataChart from "$lib/data-chart/DataChart.svelte";
8
9
  import { monitorsManager } from "$lib";
10
+ import { MonitorDetailsState } from "$lib/monitors/monitor-details.svelte";
11
+ import { choosePanelPollutant } from "$lib/monitors/panel-pollutant";
9
12
  import { useMonitorMapRouter } from "./router-context";
10
13
  import { fetchTempByCoords } from "$lib/weather";
11
14
  import { Colors, valueToColor } from "$lib/colors";
@@ -21,24 +24,75 @@
21
24
  }
22
25
  const { route, navigate, basePath } = useMonitorMapRouter();
23
26
 
24
- const monitor = $derived(monitorsManager.latest?.get(route.params.id ?? ""));
27
+ // The panel fetches its own monitor: the map's readings only hold monitors that have a
28
+ // current reading for the map's pollutant.
29
+ const details = new MonitorDetailsState();
30
+ const meta = $derived(monitorsManager.meta);
31
+ // Shown only once the metadata is in too; labels and levels come from it.
32
+ const monitor = $derived(details.status === "ready" && meta ? details.monitor : null);
33
+
34
+ // Through a $derived: the router replaces `route.params` on every navigation, including
35
+ // a search-only change or Back to the same monitor, which must not reload the panel.
36
+ const routeId = $derived(route.params.id);
37
+ $effect(() => {
38
+ const id = routeId;
39
+ if (id) untrack(() => details.load(id));
40
+ });
41
+
42
+ // Bumped whenever the map's readings are replaced, i.e. its 2-minute refresh ran. The data
43
+ // box follows it; the chart decides for itself whether its date range can have new entries.
44
+ let refreshTick = $state(0);
45
+ let seenLatest = untrack(() => monitorsManager.latest);
46
+
47
+ $effect(() => {
48
+ const latest = monitorsManager.latest;
49
+ // Null while a pollutant change is loading; wait for the readings that follow.
50
+ if (!latest || latest === seenLatest) return;
51
+ // The first readings of a cold load arrive alongside this panel's own request.
52
+ const first = seenLatest === null;
53
+ seenLatest = latest;
54
+ if (first) return;
55
+ untrack(() => {
56
+ details.refresh();
57
+ refreshTick++;
58
+ });
59
+ });
60
+
61
+ const panel = $derived(
62
+ monitor && meta ? choosePanelPollutant(monitorsManager.pollutant, monitor, meta) : null
63
+ );
64
+ const reading = $derived(
65
+ monitor && panel?.pollutant && panel.hasReading ? monitor.latest[panel.pollutant] : undefined
66
+ );
67
+ // Set when the panel shows a different pollutant than the map is coloring by.
68
+ const pollutantNote = $derived.by(() => {
69
+ const mapPollutant = monitorsManager.pollutant;
70
+ if (!meta || !panel?.pollutant || !panel.hasReading || !mapPollutant) return;
71
+ if (panel.pollutant === mapPollutant) return;
72
+ const shown = meta.entryType(panel.pollutant).label;
73
+ const label = meta.entryType(mapPollutant).label;
74
+ // Mid-sentence: lowercase a plain word ("Ozone"), keep a symbol ("PM2.5") as written.
75
+ const missing = /^[A-Z][a-z]+$/.test(label) ? label.toLowerCase() : label;
76
+ return `Showing ${shown}. This monitor has no ${missing} readings.`;
77
+ });
78
+
25
79
  let tempData: TempDataBox | undefined = $state();
26
80
  const entryData: ComponentProps<typeof DataBox> | undefined = $derived.by(() => {
27
- if (!monitor || !monitorsManager.meta) return;
81
+ if (!monitor || !meta || !panel?.pollutant || !reading) return;
28
82
 
29
- const entryType = monitorsManager.meta.entryType(monitor.latest.entry_type);
83
+ const entryType = meta.entryType(panel.pollutant);
30
84
  const level: SJVAirEntryLevel | undefined = getCurrentLevel(
31
- monitor.latest.value,
85
+ reading.value,
32
86
  entryType.asIter.levels!
33
87
  );
34
88
 
35
- const updateDuration = monitorsManager.meta.monitorType(monitor.type).expected_interval;
89
+ const updateDuration = meta.monitorType(monitor.type).expected_interval;
36
90
 
37
91
  return {
38
92
  color: level?.color ?? "",
39
93
  header: entryType.label,
40
94
  subheading: `(${updateDuration} avg)`,
41
- value: Math.round(parseInt(monitor.latest.value, 10)).toString()
95
+ value: Math.round(parseInt(reading.value, 10)).toString()
42
96
  };
43
97
  });
44
98
 
@@ -49,15 +103,26 @@
49
103
  { min: 95, color: Colors.red }
50
104
  ];
51
105
 
106
+ // Re-run for each refreshed copy of the monitor, as before, but never show one monitor's
107
+ // temperature on another: cleared when the id changes, and a late answer is dropped.
108
+ let tempMonitorId: string | undefined;
52
109
  $effect(() => {
53
- if (monitor && monitor.position) {
54
- fetchTempByCoords(monitor.position.coordinates).then((temperature) => {
110
+ const current = monitor;
111
+ const id = current?.id;
112
+ if (id !== tempMonitorId) {
113
+ tempMonitorId = id;
114
+ tempData = undefined;
115
+ }
116
+ if (!current?.position) return;
117
+ fetchTempByCoords(current.position.coordinates)
118
+ .then((temperature) => {
119
+ if (untrack(() => monitor?.id) !== id) return;
55
120
  tempData = {
56
121
  value: `${temperature}°F`,
57
122
  color: valueToColor(temperature, tempColors)
58
123
  };
59
- });
60
- }
124
+ })
125
+ .catch(console.error);
61
126
  });
62
127
  </script>
63
128
 
@@ -104,24 +169,37 @@
104
169
  {@render badge("location-on", monitor.county)}
105
170
  {@render badge("location-searching", monitor.location)}
106
171
  </div>
172
+ {#if pollutantNote}
173
+ <p class="text-center text-sm text-gray-600">{pollutantNote}</p>
174
+ {:else if panel && !panel.hasReading}
175
+ <p class="text-center text-sm text-gray-600">This monitor has no current readings.</p>
176
+ {/if}
107
177
  {/if}
108
178
  </div>
109
179
 
110
180
  <div class="tall:overflow-y-auto flex flex-col items-center gap-12 p-5 pt-10">
111
- {#if monitor}
112
- <div class="flex w-full justify-around">
113
- {#if tempData}
114
- <DataBox color={tempData.color} header="Temperature" value={tempData.value} />
115
- {/if}
116
- {#if entryData}
117
- <DataBox
118
- color={entryData.color}
119
- header={entryData.header}
120
- subheading={entryData.subheading}
121
- value={entryData.value}
122
- />
123
- {/if}
124
- </div>
181
+ {#if details.status === "not-found"}
182
+ <p class="text-gray-700" role="alert">Monitor not found.</p>
183
+ {:else if details.status === "failed"}
184
+ <LoadError message="Couldn't load this monitor." onretry={() => details.retry()} />
185
+ {:else if !monitor || !panel}
186
+ <p class="text-sm text-gray-500">Loading…</p>
187
+ {:else}
188
+ {#if tempData || entryData}
189
+ <div class="flex w-full justify-around">
190
+ {#if tempData}
191
+ <DataBox color={tempData.color} header="Temperature" value={tempData.value} />
192
+ {/if}
193
+ {#if entryData}
194
+ <DataBox
195
+ color={entryData.color}
196
+ header={entryData.header}
197
+ subheading={entryData.subheading}
198
+ value={entryData.value}
199
+ />
200
+ {/if}
201
+ </div>
202
+ {/if}
125
203
 
126
204
  <div class="tall:block hidden">
127
205
  <MonitorWidgetModal monitorId={monitor.id} />
@@ -131,9 +209,11 @@
131
209
  <MonitorSubscriptionManager monitorId={monitor.id} />
132
210
  {/if}
133
211
 
134
- <div class="w-full">
135
- <DataChart {monitor} />
136
- </div>
212
+ {#if panel.pollutant}
213
+ <div class="w-full">
214
+ <DataChart {monitor} pollutant={panel.pollutant} {refreshTick} />
215
+ </div>
216
+ {/if}
137
217
 
138
218
  <div class="w-full">
139
219
  <h3 class="font-semibold">Data Provided Courtesy Of:</h3>
@@ -14,6 +14,7 @@
14
14
  currentSelection,
15
15
  parsePollutantParam
16
16
  } from "$lib/options/pollutant-param";
17
+ import LoadError from "$lib/LoadError.svelte";
17
18
  import Legend from "$lib/legend/Legend.svelte";
18
19
  import {
19
20
  fireSmokeLegendSections,
@@ -61,7 +62,21 @@
61
62
  monitorsMapIntegration.enabled = false;
62
63
  }
63
64
 
64
- monitorsManager.init(route.search.pollutant);
65
+ // The load screen stays up until the monitors load, so a failure has to be shown on it.
66
+ let loadFailed = $state(false);
67
+
68
+ function loadMonitors() {
69
+ loadFailed = false;
70
+ // A rejected init() can be retried: a later call starts a fresh run.
71
+ monitorsManager.init(route.search.pollutant).catch((error) => {
72
+ console.error(error);
73
+ loadFailed = true;
74
+ });
75
+ }
76
+
77
+ loadMonitors();
78
+ // A previous instance of this layout stopped polling in onDestroy; init() is a no-op by now.
79
+ monitorsManager.resumeAutoUpdate();
65
80
  collocationSitesManager.init();
66
81
  hmsManager.init();
67
82
  monitorsMapIntegration.onMonitorClick = (id: string) => {
@@ -127,6 +142,11 @@
127
142
  knownRoutes={[`${basePath}/monitor/`]}
128
143
  {basePath}
129
144
  >
145
+ {#snippet loadError()}
146
+ {#if loadFailed}
147
+ <LoadError message="Couldn't load air monitors." onretry={loadMonitors} />
148
+ {/if}
149
+ {/snippet}
130
150
  {#snippet menu()}
131
151
  <OptionsGroup>
132
152
  <PollutantMenu />
@@ -1,6 +1,6 @@
1
1
  import { type Popup, type MapLayerEventType, type Map as MaptilerMap } from "@maptiler/sdk";
2
2
  import type { Geometry } from "geojson";
3
- import { mapManager } from "$lib/map/map.svelte";
3
+ import { mapManager } from "$lib/map/map-manager.svelte.js";
4
4
  import { MapIconLayerIntegration } from "$lib/map/integrations/map-geojson-integration.svelte";
5
5
  import { CollocationIconManager, getCollocationIconId } from "./collocations-icon-manager.svelte";
6
6
  import { TooltipManager } from "$lib/map/integrations/tooltip.svelte";
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Light entry (`@sjvair/monitor-map/core`): what a host app needs at startup, without the map.
3
+ *
4
+ * Nothing imported here may reach @maptiler/sdk, @maptiler/weather or uplot at runtime;
5
+ * `npm run test:bundle` enforces it. Everything is re-exported from the same modules the
6
+ * main entry uses, so `monitorsManager` and the router context stay single instances
7
+ * whichever entries a host mixes.
8
+ */
9
+ export {
10
+ ROUTER_KEY,
11
+ provideMonitorMapRouter,
12
+ useMonitorMapRouter,
13
+ type MonitorMapRouterApi
14
+ } from "./router-context";
15
+ export { monitorsManager } from "./monitors/monitors.svelte.js";
16
+ export type { MonitorsManager, MonitorsInitOptions } from "./monitors/monitors.svelte.js";
17
+ export type { MonitorsDataSource, MonitorsPollutant } from "./monitors/types";
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
+ import { untrack } from "svelte";
2
3
  import type { Attachment } from "svelte/attachments";
3
- import type { MonitorLatestType } from "@sjvair/sdk";
4
4
  import type { DateRange as CalendarRange } from "bits-ui";
5
5
  import { parseAbsolute } from "@internationalized/date";
6
6
  import { Maximize, SquareX } from "@lucide/svelte";
@@ -9,13 +9,23 @@
9
9
  import { getMonitorEntriesCSVUrl } from "@sjvair/sdk/monitors";
10
10
  import { format, parseISO } from "date-fns";
11
11
  import { RangeCalendar } from "$lib/components/ui/range-calendar";
12
+ import type { PanelPollutant } from "../monitors/panel-pollutant";
13
+ import { monitorsManager } from "../monitors/monitors.svelte";
12
14
  import { DataChartManager } from "./data-chart.svelte";
13
- import { createDateRange } from "./DateRange";
14
- import { fetchChartData } from "./requests";
15
+ import { createDateRange, extendToNow, includesToday } from "./DateRange";
16
+ import { fetchChartData, type ChartMonitor } from "./requests";
15
17
  import { getChartConfig } from "./chart-utils";
16
- import { monitorsManager } from "../monitors/monitors.svelte";
18
+ import { levelColors } from "./level-colors";
19
+
20
+ interface Props {
21
+ monitor: ChartMonitor;
22
+ /** The pollutant to chart. Independent of the pollutant the map is coloring by. */
23
+ pollutant: PanelPollutant;
24
+ /** Bumped by the owner whenever fresh data may exist; see the effect below. */
25
+ refreshTick?: number;
26
+ }
17
27
 
18
- let { monitor }: { monitor: MonitorLatestType<"pm25" | "o3"> } = $props();
28
+ let { monitor, pollutant, refreshTick = 0 }: Props = $props();
19
29
 
20
30
  const chartManager = new DataChartManager();
21
31
  let expanded = $state(false);
@@ -34,7 +44,8 @@
34
44
  format(parseISO(chartManager.dateRange.end), "MMM d, yyyy")
35
45
  );
36
46
 
37
- const noChartData = $derived(!chartManager.chartData.length);
47
+ // A request with no entries still resolves to [[], []], so test the x axis, not the tuple.
48
+ const noChartData = $derived(!chartManager.chartData[0]?.length);
38
49
  const message = $derived(
39
50
  chartManager.loading
40
51
  ? "Loading Data"
@@ -42,10 +53,38 @@
42
53
  ? "No Data Available For Selected Date Range"
43
54
  : ""
44
55
  );
45
- const pollutantLabel = $derived(monitorsManager.pollutant === "pm25" ? "PM 2.5" : "Ozone");
56
+ const pollutantLabel = $derived(pollutant === "pm25" ? "PM 2.5" : "Ozone");
57
+ // Level definitions are shared metadata, not the map's current selection.
58
+ const colors = $derived(
59
+ levelColors(monitorsManager.meta?.entryType(pollutant).asIter.levels ?? [])
60
+ );
46
61
 
62
+ // Reload for another monitor or pollutant, not for a new object describing the same one.
63
+ // Both go through a $derived: the owner recomputes what it passes on every refresh, and a
64
+ // prop read directly would re-run this effect even when the value is unchanged.
65
+ const monitorId = $derived(monitor.id);
66
+ const chartPollutant = $derived(pollutant);
47
67
  $effect(() => {
48
- if (monitor) loadChartData();
68
+ void monitorId;
69
+ void chartPollutant;
70
+ untrack(() => loadChartData());
71
+ });
72
+
73
+ // Whether the chosen range ended on the day it was chosen, so it should follow the present.
74
+ // Remembered, not re-tested per tick: a panel left open past midnight keeps following.
75
+ let live = includesToday(chartManager.dateRange);
76
+ let seenTick = untrack(() => refreshTick);
77
+
78
+ // A range that ended in the past cannot gain entries, so only a live one reloads.
79
+ $effect(() => {
80
+ const tick = refreshTick;
81
+ if (tick === seenTick) return;
82
+ seenTick = tick;
83
+ if (!live) return;
84
+ untrack(() => {
85
+ chartManager.dateRange = extendToNow(chartManager.dateRange);
86
+ loadChartData({ silent: true });
87
+ });
49
88
  });
50
89
 
51
90
  $effect(() => {
@@ -82,7 +121,7 @@
82
121
  instance?.destroy?.();
83
122
  el.innerHTML = "";
84
123
  const maxDiff = Math.max(...flatData) - Math.min(...flatData);
85
- const opts = getChartConfig(monitor.type, maxDiff, width, height);
124
+ const opts = getChartConfig(monitor.type, maxDiff, width, height, colors, pollutantLabel);
86
125
  instance = new uPlot(opts, chartManager.chartData, el);
87
126
  };
88
127
 
@@ -103,9 +142,19 @@
103
142
  };
104
143
  };
105
144
 
106
- async function loadChartData() {
107
- chartManager.loading = true;
108
- chartManager.chartData = await fetchChartData(monitor, chartManager.dateRange);
145
+ let chartRequest = 0;
146
+
147
+ // `silent` keeps the current chart on screen while a timed refresh loads.
148
+ async function loadChartData({ silent = false } = {}) {
149
+ const request = ++chartRequest;
150
+ if (!silent) chartManager.loading = true;
151
+ const data = await fetchChartData(monitor, chartManager.dateRange, pollutant);
152
+ // A newer request (another range, monitor, pollutant or refresh) owns the chart now.
153
+ if (request !== chartRequest) return;
154
+ // A failed request resolves to [] (an empty range is [[], []]); a background refresh
155
+ // that failed keeps the chart already on screen.
156
+ if (silent && data.length === 0) return;
157
+ chartManager.chartData = data;
109
158
  chartManager.loading = false;
110
159
  }
111
160
 
@@ -113,6 +162,7 @@
113
162
  if (calendarValue?.start && calendarValue?.end) {
114
163
  dataRange = calendarValue;
115
164
  chartManager.dateRange = createDateRange(dataRange.start, dataRange.end);
165
+ live = includesToday(chartManager.dateRange);
116
166
  calendarOpen = false;
117
167
  loadChartData();
118
168
  }
@@ -121,7 +171,7 @@
121
171
  function downloadCSV() {
122
172
  const url = getMonitorEntriesCSVUrl({
123
173
  monitorId: monitor.id,
124
- entryType: monitorsManager.pollutant ?? "pm25",
174
+ entryType: pollutant,
125
175
  timestampGte: chartManager.dateRange.start,
126
176
  timestampLte: chartManager.dateRange.end
127
177
  });
@@ -1,4 +1,4 @@
1
- import { subDays, startOfDay, endOfDay, isToday } from "date-fns";
1
+ import { subDays, startOfDay, endOfDay, isToday, parseISO } from "date-fns";
2
2
  import type { DateValue } from "@internationalized/date";
3
3
 
4
4
  export type DateRange = { start: string; end: string };
@@ -20,3 +20,13 @@ export function createDateRange(start?: DateValue, end?: DateValue): DateRange {
20
20
  end: endDate.toISOString()
21
21
  };
22
22
  }
23
+
24
+ /** Whether the range ends on the current calendar day, i.e. it can still gain entries. */
25
+ export function includesToday(range: DateRange): boolean {
26
+ return isToday(parseISO(range.end));
27
+ }
28
+
29
+ /** The same range with its end moved to the present. */
30
+ export function extendToNow(range: DateRange): DateRange {
31
+ return { start: range.start, end: new Date().toISOString() };
32
+ }