diff --git a/src/hooks/useDrawDistribution.js b/src/hooks/useDrawDistribution.js index fd8966a..ffe15fd 100644 --- a/src/hooks/useDrawDistribution.js +++ b/src/hooks/useDrawDistribution.js @@ -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() }