fix: clear stale drawsByGroup when a model switch starts or fails

useDrawDistribution never cleared drawsByGroup on a new fetch or on
error, only status. A chart that varies `model` across renders (Chart
3's dropdown) could switch from a model that had succeeded to one
that then failed, and keep rendering the *previous* model's real
posterior draws — keyed by the same RACE_SEX strings — under the
newly-selected model's summary stats, with ApproxNote visible
suggesting (wrongly) that the fallback approximation was in use.

Clear drawsByGroup to null both when a new fetch starts and in the
catch branch, so a failed model switch never mixes draws from two
different models.
This commit is contained in:
2026-08-11 09:43:09 -04:00
parent b5941a78d1
commit 20c08ae198
+16 -1
View File
@@ -63,6 +63,14 @@ export function useDrawDistribution({ leaid, state, model, year, groups }) {
if (!leaid || !state || !model || !year || !groups?.length) return
let cancelled = false
setStatus('loading')
// Clear any previous model/district's draws immediately, not just on
// success/failure below. Without this, a chart that varies `model`
// across renders (e.g. RateDensityRidgeline's dropdown) would keep
// rendering the *previous* model's real draws — keyed by the same
// RACE_SEX strings — under the newly-selected model's summary stats
// while this fetch is in flight or if it fails, silently mixing data
// from two different models.
setDrawsByGroup(null)
async function run() {
let conn
@@ -106,7 +114,14 @@ export function useDrawDistribution({ leaid, state, model, year, groups }) {
}
} catch (err) {
console.error('useDrawDistribution failed:', err)
if (!cancelled) setStatus('error')
if (!cancelled) {
// Belt-and-suspenders alongside the setDrawsByGroup(null) at the
// top of this effect: a failed fetch/query must never leave a
// *previous* model's real draws in place under the newly-selected
// model's status/summary stats.
setDrawsByGroup(null)
setStatus('error')
}
} finally {
if (conn) await conn.close()
}