mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
refactor(coach): drawer prefill becomes a controlled prop
The v1.4.20 implementation reset the composer by mounting the
`<CoachDrawer>` with `key={prefill}`. That weaponised React's
key-reset to flush internal state every prefill change — Sr-HIGH-4
flagged it as a smell because the next refactor that touches the
drawer's mount point silently loses the reset semantics.
Replace with the controlled-prop pattern React documents at
https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes:
new `useResettableValue` hook reads the controlled prefill, mirrors
it via a sibling useState pair, and seeds the composer with the new
value when the prop changes. Render-phase setState — ESLint-clean
against `react-hooks/set-state-in-effect`. The user can still type
freely; the drawer only resets when the parent changes which
suggested-prompt chip is active.
`nextResettableValue` exposes the comparison contract as a pure
helper so the unit test can pin the (Object.is) semantics without
standing up a React renderer.
The `<CoachDrawer key={…}>` is gone from the insights page mount.
Tests +5: pure-function coverage of unchanged value (no reset),
prefill change (reset), null↔empty transition (reset), suggestion
chip → cleared prefill (reset), and NaN-to-NaN edge case (no reset
under Object.is).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
fa0774835e
commit
9413d29303
3 files changed
+129
-11
No files matched your search
@@ -1645,12 +1645,13 @@ export default function InsightsPage() {
|
||||
/>
|
||||
</section>
|
||||
|
||||
{/* v1.4.20 phase B2b — AI Coach drawer. The drawer reads its
|
||||
initial input value from `prefill`; we change the React `key`
|
||||
on every prefill transition so the lazy `useState` initialiser
|
||||
fires fresh and the composer surfaces the latest chip. */}
|
||||
{/* v1.4.20 phase B2b — AI Coach drawer.
|
||||
v1.4.23 H3 — `prefill` is a controlled prop now; the drawer
|
||||
watches it via an in-render ref and resets the composer
|
||||
when the parent changes which suggested-prompt chip is
|
||||
active. The old `key={prefill}` weaponise-React-keys pattern
|
||||
is gone (Sr-HIGH-4). */}
|
||||
<CoachDrawer
|
||||
key={coachPrefill ?? "blank"}
|
||||
open={coachOpen}
|
||||
onOpenChange={setCoachOpen}
|
||||
prefill={coachPrefill}
|
||||
|
||||
Reference in new issue
Block a user