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:
@@ -63,6 +63,14 @@ export function useDrawDistribution({ leaid, state, model, year, groups }) {
|
|||||||
if (!leaid || !state || !model || !year || !groups?.length) return
|
if (!leaid || !state || !model || !year || !groups?.length) return
|
||||||
let cancelled = false
|
let cancelled = false
|
||||||
setStatus('loading')
|
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() {
|
async function run() {
|
||||||
let conn
|
let conn
|
||||||
@@ -106,7 +114,14 @@ export function useDrawDistribution({ leaid, state, model, year, groups }) {
|
|||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('useDrawDistribution failed:', 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 {
|
} finally {
|
||||||
if (conn) await conn.close()
|
if (conn) await conn.close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user