From 12242c40c3efe236750d51ac8004fb248140d827 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 31 Mar 2026 22:05:26 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20objective=20stat?= =?UTF-8?q?e=20updates=20in=20TreatmentPlanManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com> --- .../therapy/TreatmentPlanManager.tsx | 118 +++++++++++------- 1 file changed, 71 insertions(+), 47 deletions(-) diff --git a/src/components/therapy/TreatmentPlanManager.tsx b/src/components/therapy/TreatmentPlanManager.tsx index 83c4d1f0a..66f538f14 100644 --- a/src/components/therapy/TreatmentPlanManager.tsx +++ b/src/components/therapy/TreatmentPlanManager.tsx @@ -118,9 +118,10 @@ const TreatmentPlanManager: FC = () => { const [error, setError] = useState(null) const [isCreateModalOpen, setIsCreateModalOpen] = useState(false) - const [newPlanData, setNewPlanData] = useState( - JSON.parse(JSON.stringify(initialNewPlanData) as unknown), - ) + const [newPlanData, setNewPlanData] = useState({ + ...initialNewPlanData, + goals: [...initialNewPlanData.goals], + } as FormNewPlanData) const [planToDelete, setPlanToDelete] = useState(null) const [isEditModalOpen, setIsEditModalOpen] = useState(false) @@ -257,6 +258,7 @@ const TreatmentPlanManager: FC = () => { // --- End Goal Management --- // --- Objective Management Functions --- + // ⚡ Bolt: Replace JSON.parse/stringify with shallow copy for performance const addObjective = (goalIndex: number, isEdit = false) => { const newObjective: ClientSideNewObjective = { description: '', @@ -265,27 +267,29 @@ const TreatmentPlanManager: FC = () => { } if (isEdit && editingPlanData) { - const updatedGoals = JSON.parse( - JSON.stringify(editingPlanData.goals || []), - ) as EditableGoal[] + const updatedGoals = [...(editingPlanData.goals || [])] if (updatedGoals[goalIndex]) { - updatedGoals[goalIndex].objectives = [ - ...(updatedGoals[goalIndex].objectives || []), - newObjective, - ] + updatedGoals[goalIndex] = { + ...updatedGoals[goalIndex], + objectives: [ + ...(updatedGoals[goalIndex].objectives || []), + newObjective, + ], + } setEditingPlanData((prev: FormUpdatePlanData | null) => prev ? { ...prev, goals: updatedGoals } : null, ) } } else { - const updatedGoals = JSON.parse( - JSON.stringify(newPlanData.goals), - ) as ClientSideNewGoal[] + const updatedGoals = [...newPlanData.goals] if (updatedGoals[goalIndex]) { - updatedGoals[goalIndex].objectives = [ - ...updatedGoals[goalIndex].objectives, - newObjective, - ] + updatedGoals[goalIndex] = { + ...updatedGoals[goalIndex], + objectives: [ + ...updatedGoals[goalIndex].objectives, + newObjective, + ], + } setNewPlanData((prev: FormNewPlanData) => ({ ...prev, goals: updatedGoals, @@ -302,33 +306,41 @@ const TreatmentPlanManager: FC = () => { isEdit = false, ) => { if (isEdit && editingPlanData) { - const updatedGoals = JSON.parse( - JSON.stringify(editingPlanData.goals || []), - ) as EditableGoal[] + const updatedGoals = [...(editingPlanData.goals || [])] if ( updatedGoals[goalIndex] && + updatedGoals[goalIndex].objectives && updatedGoals[goalIndex].objectives[objIndex] ) { - ;(updatedGoals[goalIndex].objectives[objIndex])[ - field as keyof EditableObjective - ] = value as never + const updatedObjectives = [...updatedGoals[goalIndex].objectives] + updatedObjectives[objIndex] = { + ...updatedObjectives[objIndex], + [field]: value, + } + updatedGoals[goalIndex] = { + ...updatedGoals[goalIndex], + objectives: updatedObjectives, + } setEditingPlanData((prev: FormUpdatePlanData | null) => prev ? { ...prev, goals: updatedGoals } : null, ) } } else { - const updatedNewGoals = JSON.parse( - JSON.stringify(newPlanData.goals), - ) as ClientSideNewGoal[] + const updatedNewGoals = [...newPlanData.goals] if ( updatedNewGoals[goalIndex] && + updatedNewGoals[goalIndex].objectives && updatedNewGoals[goalIndex].objectives[objIndex] ) { - ;( - updatedNewGoals[goalIndex].objectives[ - objIndex - ] - )[field] = value as never + const updatedObjectives = [...updatedNewGoals[goalIndex].objectives] + updatedObjectives[objIndex] = { + ...updatedObjectives[objIndex], + [field]: value, + } + updatedNewGoals[goalIndex] = { + ...updatedNewGoals[goalIndex], + objectives: updatedObjectives, + } setNewPlanData((prev: FormNewPlanData) => ({ ...prev, goals: updatedNewGoals, @@ -343,21 +355,27 @@ const TreatmentPlanManager: FC = () => { isEdit = false, ) => { if (isEdit && editingPlanData) { - const updatedGoals = JSON.parse( - JSON.stringify(editingPlanData.goals || []), - ) as EditableGoal[] + const updatedGoals = [...(editingPlanData.goals || [])] if (updatedGoals[goalIndex] && updatedGoals[goalIndex].objectives) { - updatedGoals[goalIndex].objectives.splice(objIndex, 1) + const updatedObjectives = [...updatedGoals[goalIndex].objectives] + updatedObjectives.splice(objIndex, 1) + updatedGoals[goalIndex] = { + ...updatedGoals[goalIndex], + objectives: updatedObjectives, + } setEditingPlanData((prev: FormUpdatePlanData | null) => prev ? { ...prev, goals: updatedGoals } : null, ) } } else { - const updatedNewGoals = JSON.parse( - JSON.stringify(newPlanData.goals), - ) as ClientSideNewGoal[] + const updatedNewGoals = [...newPlanData.goals] if (updatedNewGoals[goalIndex] && updatedNewGoals[goalIndex].objectives) { - updatedNewGoals[goalIndex].objectives.splice(objIndex, 1) + const updatedObjectives = [...updatedNewGoals[goalIndex].objectives] + updatedObjectives.splice(objIndex, 1) + updatedNewGoals[goalIndex] = { + ...updatedNewGoals[goalIndex], + objectives: updatedObjectives, + } setNewPlanData((prev: FormNewPlanData) => ({ ...prev, goals: updatedNewGoals, @@ -409,7 +427,10 @@ const TreatmentPlanManager: FC = () => { } await fetchPlans() setIsCreateModalOpen(false) - setNewPlanData(JSON.parse(JSON.stringify(initialNewPlanData) as unknown)) + setNewPlanData({ + ...initialNewPlanData, + goals: [...initialNewPlanData.goals], + } as FormNewPlanData) toast.success('Treatment plan created successfully!') } catch (err: unknown) { const errorMessage = @@ -501,18 +522,21 @@ const TreatmentPlanManager: FC = () => { ? new Date(plan.startDate).toISOString().split('T')[0] : '', goals: plan.goals - ? JSON.parse( - JSON.stringify( - plan.goals.map((g) => ({ ...g, objectives: g.objectives || [] })), - ), - ) - : [], // Deep copy goals, ensure objectives is array + ? plan.goals.map((g) => ({ + ...g, + objectives: g.objectives ? [...g.objectives] : [], + })) + : [], // Shallow copy goals, ensure objectives is array } as FormUpdatePlanData) // Cast to ensure type compatibility setIsEditModalOpen(true) } const openCreateModal = () => { - setNewPlanData(JSON.parse(JSON.stringify(initialNewPlanData) as unknown)) // Reset with deep copy + // ⚡ Bolt: Replace JSON.parse/stringify with shallow copy for performance + setNewPlanData({ + ...initialNewPlanData, + goals: [...initialNewPlanData.goals], + } as FormNewPlanData) // Reset with shallow copy setIsCreateModalOpen(true) }