From 167d998e01686b546ad09cc76c0f672f8682f551 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 21 Jul 2026 02:30:06 +0000 Subject: [PATCH] refactor(mockup): scope safety-plan id counter per instance Replace the module-level mutable `seq` counter behind `uid()` with a per-instance `useRef` counter inside PatientSafetyPlanMockup. The old module-level counter kept incrementing across unmount/remount (route revisits); a per-instance ref resets with the component. Ids only need to be unique within a mounted plan, so behaviour is unchanged. `uid` is a stable useCallback and is added to `addEntry`'s dependency list. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_019g9m5emoPKCmDDPeGrchYd --- src/components/patient-safety-plan-mockup.tsx | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/components/patient-safety-plan-mockup.tsx b/src/components/patient-safety-plan-mockup.tsx index a2d52bee67..738fcbe0e4 100644 --- a/src/components/patient-safety-plan-mockup.tsx +++ b/src/components/patient-safety-plan-mockup.tsx @@ -21,7 +21,7 @@ import { X, type LucideIcon, } from "lucide-react"; -import { useCallback, useMemo, useState } from "react"; +import { useCallback, useMemo, useRef, useState } from "react"; import { cn, @@ -188,9 +188,6 @@ const SEED_REASONS: Entry[] = [ { id: "r4", primary: "Being there for my niece" }, ]; -let seq = 0; -const uid = (prefix: string) => `${prefix}-live-${seq++}`; - /* ---------- small building blocks ---------- */ function AddRow({ @@ -418,10 +415,18 @@ export function PatientSafetyPlanMockup() { const [copied, setCopied] = useState(false); const [finalised, setFinalised] = useState(false); - const addEntry = useCallback((key: StepKey, primary: string, secondary?: string) => { - setEntries((prev) => ({ ...prev, [key]: [...prev[key], { id: uid(key), primary, secondary }] })); - setFinalised(false); - }, []); + // Per-instance id counter — avoids a module-level mutable that would persist + // across remounts; ids only need to be unique within this mounted plan. + const uidRef = useRef(0); + const uid = useCallback((prefix: string) => `${prefix}-live-${uidRef.current++}`, []); + + const addEntry = useCallback( + (key: StepKey, primary: string, secondary?: string) => { + setEntries((prev) => ({ ...prev, [key]: [...prev[key], { id: uid(key), primary, secondary }] })); + setFinalised(false); + }, + [uid], + ); const removeEntry = useCallback((key: StepKey, id: string) => { setEntries((prev) => ({ ...prev, [key]: prev[key].filter((entry) => entry.id !== id) }));