|
1 | 1 | "use client"; |
2 | 2 |
|
3 | 3 | import type { MapSelection } from "@/types/map"; |
4 | | -import { formatGeoPoint } from "@/lib/map/geogrid"; |
| 4 | +import { formatCoordinate, formatGeoPoint } from "@/lib/map/geogrid"; |
5 | 5 | import { ZARR_STORE } from "@/lib/constants/store"; |
6 | 6 | import { ZARR_TIME } from "@/lib/zarr/timeRange"; |
7 | 7 | import { TimeSeriesPlot } from "@/components/map/TimeSeriesPlot"; |
8 | 8 | import { TimeSeriesPlotLoading } from "@/components/map/TimeSeriesPlotLoading"; |
| 9 | +import { ProgressBar } from "@/components/ui/ProgressBar"; |
9 | 10 |
|
10 | | -const PANEL_CLASS = |
11 | | - "grid gap-3 rounded-editor-md border border-editor-border bg-editor-bg-secondary p-4 shadow-editor"; |
12 | | -const HINT_CLASS = "text-[13px] leading-[1.55] text-editor-fg-tertiary"; |
13 | | -const LABEL_CLASS = "text-[13px] font-medium text-editor-fg-secondary"; |
| 11 | +type SeriesProgress = { loaded: number; total: number }; |
14 | 12 |
|
15 | 13 | type MapReadoutProps = { |
16 | 14 | selection: MapSelection | null; |
17 | 15 | historyYears: number; |
18 | 16 | onHistoryYearsChange: (years: number) => void; |
19 | 17 | loadingSeries: boolean; |
| 18 | + seriesProgress: SeriesProgress | null; |
20 | 19 | seriesError: string | null; |
21 | 20 | seriesValues: Float32Array | null; |
22 | 21 | seriesUnits: string | null; |
23 | 22 | }; |
24 | 23 |
|
| 24 | +const SECTION_LABEL = "text-[13px] font-semibold text-editor-fg-primary"; |
| 25 | +const META = "font-mono text-[11px] text-editor-fg-tertiary"; |
| 26 | + |
25 | 27 | export function MapReadout({ |
26 | 28 | selection, |
27 | 29 | historyYears, |
28 | 30 | onHistoryYearsChange, |
29 | 31 | loadingSeries, |
| 32 | + seriesProgress, |
30 | 33 | seriesError, |
31 | 34 | seriesValues, |
32 | 35 | seriesUnits, |
33 | 36 | }: MapReadoutProps) { |
34 | 37 | const historyLabel = |
35 | 38 | historyYears === 1 ? "Last 1 year" : `Last ${historyYears} years`; |
36 | 39 |
|
| 40 | + if (!selection) { |
| 41 | + return ( |
| 42 | + <div className="flex flex-1 flex-col items-center justify-center gap-2 py-8 text-center"> |
| 43 | + <div |
| 44 | + className="grid h-9 w-9 place-items-center rounded-full border border-dashed border-editor-border-strong" |
| 45 | + aria-hidden="true" |
| 46 | + > |
| 47 | + <span className="block h-2 w-2 rounded-full bg-accent" /> |
| 48 | + </div> |
| 49 | + <p className="text-[13.5px] font-semibold text-editor-fg-secondary"> |
| 50 | + Click the map |
| 51 | + </p> |
| 52 | + <p className="text-[12.5px] text-editor-fg-tertiary"> |
| 53 | + Pick a point to load its record |
| 54 | + </p> |
| 55 | + </div> |
| 56 | + ); |
| 57 | + } |
| 58 | + |
37 | 59 | const patchCells = ZARR_STORE.nativeChunks.lon; |
38 | 60 | const patchDeg = patchCells * ZARR_STORE.spatialResolutionDeg; |
39 | 61 |
|
| 62 | + const historyControl = ( |
| 63 | + <section> |
| 64 | + <div className="mb-3 flex items-baseline justify-between gap-3"> |
| 65 | + <label className={SECTION_LABEL} htmlFor="history-years"> |
| 66 | + History window |
| 67 | + </label> |
| 68 | + <span className="font-mono text-[12.5px] font-semibold text-accent"> |
| 69 | + {historyLabel} |
| 70 | + </span> |
| 71 | + </div> |
| 72 | + <input |
| 73 | + id="history-years" |
| 74 | + className="w-full cursor-pointer [accent-color:var(--accent)]" |
| 75 | + type="range" |
| 76 | + min={ZARR_TIME.defaultHistoryYears} |
| 77 | + max={ZARR_TIME.maxHistoryYears} |
| 78 | + step={1} |
| 79 | + value={historyYears} |
| 80 | + onChange={(event) => |
| 81 | + onHistoryYearsChange(Number(event.currentTarget.value)) |
| 82 | + } |
| 83 | + /> |
| 84 | + </section> |
| 85 | + ); |
| 86 | + |
| 87 | + const chart = ( |
| 88 | + <section aria-live="polite"> |
| 89 | + <div className="mb-3 flex items-baseline justify-between gap-3"> |
| 90 | + <span className={SECTION_LABEL}>Daily mean</span> |
| 91 | + {!loadingSeries && !seriesError && seriesValues && seriesUnits ? ( |
| 92 | + <span className={META}>{seriesUnits}</span> |
| 93 | + ) : null} |
| 94 | + </div> |
| 95 | + |
| 96 | + {loadingSeries ? ( |
| 97 | + <div className="grid gap-3"> |
| 98 | + <SeriesLoader progress={seriesProgress} /> |
| 99 | + <TimeSeriesPlotLoading historyYears={historyYears} /> |
| 100 | + </div> |
| 101 | + ) : seriesError ? ( |
| 102 | + <p className="text-[13px] leading-[1.55] text-editor-fg-tertiary"> |
| 103 | + {seriesError} |
| 104 | + </p> |
| 105 | + ) : seriesValues ? ( |
| 106 | + <TimeSeriesPlot |
| 107 | + values={seriesValues} |
| 108 | + units={seriesUnits} |
| 109 | + hoursPerDay={ZARR_TIME.hoursPerDay} |
| 110 | + /> |
| 111 | + ) : null} |
| 112 | + </section> |
| 113 | + ); |
| 114 | + |
40 | 115 | return ( |
41 | | - <> |
42 | | - <section className={PANEL_CLASS}> |
43 | | - <header className="flex items-baseline justify-between gap-3"> |
44 | | - <h2 className="text-[15px] font-semibold tracking-[-0.02em] text-editor-fg-primary"> |
45 | | - Pixel location |
| 116 | + <div className="flex flex-col divide-y divide-editor-border"> |
| 117 | + <section className="pb-4"> |
| 118 | + <div className="flex items-baseline justify-between gap-3"> |
| 119 | + <h2 className="font-mono text-[20px] leading-none tracking-tight tabular-nums text-editor-fg-primary"> |
| 120 | + {formatCoordinate(selection.click.lat)} |
| 121 | + <span className="text-editor-fg-tertiary">, </span> |
| 122 | + {formatCoordinate(selection.click.lon)} |
46 | 123 | </h2> |
47 | | - <span className="flex-shrink-0 font-mono text-[11px] text-editor-fg-tertiary"> |
48 | | - {ZARR_STORE.kicker} |
49 | | - </span> |
50 | | - </header> |
51 | | - <p className={HINT_CLASS}> |
52 | | - Click the map to sample a {ZARR_STORE.spatialResolutionDeg}° cell. |
| 124 | + <span className={`flex-shrink-0 ${META}`}>{ZARR_STORE.kicker}</span> |
| 125 | + </div> |
| 126 | + <p className="mt-2 text-[12.5px] leading-[1.5] text-editor-fg-tertiary"> |
| 127 | + Snapped to the nearest {ZARR_STORE.spatialResolutionDeg}° cell · |
| 128 | + variable {ZARR_STORE.defaultVariable} |
53 | 129 | </p> |
54 | | - <p className={HINT_CLASS}> |
| 130 | + <p className="mt-2 text-[12.5px] leading-[1.5] text-editor-fg-tertiary"> |
55 | 131 | Each click downloads a {patchCells}×{patchCells} patch ({patchDeg}° ×{" "} |
56 | 132 | {patchDeg}°), drawn as the dashed box. Toggle it with the patch button |
57 | 133 | in the top bar. |
58 | 134 | </p> |
59 | 135 | </section> |
60 | 136 |
|
61 | | - <section className={PANEL_CLASS}> |
62 | | - <div className="flex items-baseline justify-between gap-3"> |
63 | | - <label className={LABEL_CLASS} htmlFor="history-years"> |
64 | | - History window |
65 | | - </label> |
66 | | - <span className="font-mono text-[13px] font-semibold text-accent"> |
67 | | - {historyLabel} |
68 | | - </span> |
69 | | - </div> |
70 | | - <input |
71 | | - id="history-years" |
72 | | - className="w-full cursor-pointer [accent-color:var(--accent)]" |
73 | | - type="range" |
74 | | - min={ZARR_TIME.defaultHistoryYears} |
75 | | - max={ZARR_TIME.maxHistoryYears} |
76 | | - step={1} |
77 | | - value={historyYears} |
78 | | - onChange={(event) => |
79 | | - onHistoryYearsChange(Number(event.currentTarget.value)) |
80 | | - } |
81 | | - /> |
82 | | - </section> |
| 137 | + {/* History drives the chart, so they sit together with no divider. */} |
| 138 | + <div className="grid gap-4 py-4"> |
| 139 | + {historyControl} |
| 140 | + {chart} |
| 141 | + </div> |
83 | 142 |
|
84 | | - <section className={`${PANEL_CLASS} flex-1`} aria-live="polite"> |
85 | | - {selection ? ( |
86 | | - <> |
87 | | - <dl className="grid grid-cols-2 gap-x-4 gap-y-3 [&_dd]:font-mono [&_dd]:text-[13px] [&_dd]:font-medium [&_dd]:leading-[1.45] [&_dd]:text-editor-fg-secondary [&_div]:grid [&_div]:min-w-0 [&_div]:gap-1 [&_dt]:text-[12px] [&_dt]:text-editor-fg-tertiary"> |
88 | | - <div> |
89 | | - <dt>Click</dt> |
90 | | - <dd>{formatGeoPoint(selection.click)}</dd> |
91 | | - </div> |
92 | | - <div> |
93 | | - <dt>Grid cell</dt> |
94 | | - <dd>{formatGeoPoint(selection.grid)}</dd> |
95 | | - </div> |
96 | | - <div> |
97 | | - <dt>Indices</dt> |
98 | | - <dd> |
99 | | - lon {selection.grid.lonIndex}, lat {selection.grid.latIndex} |
100 | | - </dd> |
101 | | - </div> |
102 | | - <div> |
103 | | - <dt>Variable</dt> |
104 | | - <dd>{ZARR_STORE.defaultVariable}</dd> |
105 | | - </div> |
106 | | - </dl> |
107 | | - |
108 | | - <section |
109 | | - className="grid min-w-0 gap-3 pt-4" |
110 | | - aria-label="Time series" |
111 | | - > |
112 | | - <p className={LABEL_CLASS}>Time series</p> |
113 | | - {loadingSeries && ( |
114 | | - <TimeSeriesPlotLoading historyYears={historyYears} /> |
115 | | - )} |
116 | | - {!loadingSeries && seriesError && ( |
117 | | - <p className={HINT_CLASS}>{seriesError}</p> |
118 | | - )} |
119 | | - {!loadingSeries && !seriesError && seriesValues && ( |
120 | | - <TimeSeriesPlot |
121 | | - values={seriesValues} |
122 | | - units={seriesUnits} |
123 | | - hoursPerDay={ZARR_TIME.hoursPerDay} |
124 | | - /> |
125 | | - )} |
126 | | - </section> |
127 | | - </> |
128 | | - ) : ( |
129 | | - <p className={HINT_CLASS}>Pick a point on the map to begin.</p> |
130 | | - )} |
| 143 | + <section className="pt-4"> |
| 144 | + <dl className="grid grid-cols-2 gap-x-4 gap-y-3"> |
| 145 | + <Fact label="Grid cell" value={formatGeoPoint(selection.grid)} /> |
| 146 | + <Fact |
| 147 | + label="Array index" |
| 148 | + value={`lon ${selection.grid.lonIndex} · lat ${selection.grid.latIndex}`} |
| 149 | + /> |
| 150 | + </dl> |
131 | 151 | </section> |
132 | | - </> |
| 152 | + </div> |
| 153 | + ); |
| 154 | +} |
| 155 | + |
| 156 | +function Fact({ label, value }: { label: string; value: string }) { |
| 157 | + return ( |
| 158 | + <div className="grid min-w-0 gap-0.5"> |
| 159 | + <dt className="text-[11.5px] text-editor-fg-tertiary">{label}</dt> |
| 160 | + <dd className="font-mono text-[13px] tabular-nums text-editor-fg-secondary"> |
| 161 | + {value} |
| 162 | + </dd> |
| 163 | + </div> |
| 164 | + ); |
| 165 | +} |
| 166 | + |
| 167 | +function formatBytes(bytes: number): string { |
| 168 | + if (bytes >= 1_048_576) return `${(bytes / 1_048_576).toFixed(1)} MB`; |
| 169 | + if (bytes >= 1024) return `${Math.round(bytes / 1024)} KB`; |
| 170 | + return `${bytes} B`; |
| 171 | +} |
| 172 | + |
| 173 | +function SeriesLoader({ progress }: { progress: SeriesProgress | null }) { |
| 174 | + const hasBytes = progress !== null && progress.total > 0; |
| 175 | + const value = hasBytes ? progress.loaded / progress.total : undefined; |
| 176 | + const pct = value === undefined ? null : Math.min(100, Math.round(value * 100)); |
| 177 | + |
| 178 | + return ( |
| 179 | + <div className="grid gap-2"> |
| 180 | + <div className="flex items-baseline justify-between gap-3"> |
| 181 | + <span className="text-[12.5px] text-editor-fg-secondary"> |
| 182 | + Loading time series |
| 183 | + </span> |
| 184 | + <span className="font-mono text-[12.5px] font-semibold tabular-nums text-accent"> |
| 185 | + {pct === null ? "…" : `${pct}%`} |
| 186 | + </span> |
| 187 | + </div> |
| 188 | + <ProgressBar value={value} label="Fetching time series" /> |
| 189 | + {hasBytes ? ( |
| 190 | + <p className={META}> |
| 191 | + {formatBytes(progress.loaded)} / {formatBytes(progress.total)} |
| 192 | + </p> |
| 193 | + ) : null} |
| 194 | + </div> |
133 | 195 | ); |
134 | 196 | } |
0 commit comments