Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/components/patient-safety-plan-mockup.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -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,
Expand DownExpand Up@@ -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({
Expand DownExpand Up@@ -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) }));
Expand Down
Loading