Skip to content
Open
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
22 changes: 9 additions & 13 deletions src/components/MentalHealthChatDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,13 @@ How are you feeling today? I'm here to listen and help.`,
.map((m) => m.mentalHealthAnalysis!)
}, [messages])

// ⚑ Bolt: Memoize filtered analyzed user messages to prevent O(N) re-filtering on every render
const analyzedUserMessages = useMemo(() => {
return messages.filter(
(m) => m.role === 'user' && m.mentalHealthAnalysis && !m.isProcessing,
)
}, [messages])
Comment on lines +386 to +390
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMemo only helps if messages changes by reference when updated. If messages is ever mutated in-place (e.g., messages.push(...) followed by a state set with the same array), this memo can go stale and the UI will fail to update correctly.

Suggestion

Ensure all messages updates are immutable (create a new array each time). For example:

setMessages((prev) => [...prev, nextMessage])

Reply with "@CharlieHelps yes please" if you'd like me to add a commit that audits the messages update sites in this component to guarantee immutable updates.


// Enhanced analysis for component compatibility
const enhancedAnalysisHistory = useMemo(() => {
const analysisHistory = getAnalysisHistory()
Expand Down Expand Up @@ -1130,13 +1137,7 @@ It sounds like you're dealing with some challenges. What's been the most difficu
</p>
</div>

{messages
.filter(
(m) =>
m.role === 'user' &&
m.mentalHealthAnalysis &&
!m.isProcessing,
)
{analyzedUserMessages
.slice(-2)
.map((m) => (
<div key={`analysis_${m.id}`}>
Expand All @@ -1153,12 +1154,7 @@ It sounds like you're dealing with some challenges. What's been the most difficu
</div>
))}

{!messages.some(
(m) =>
m.role === 'user' &&
m.mentalHealthAnalysis &&
!m.isProcessing,
) && (
{analyzedUserMessages.length === 0 && (
<Card className='bg-slate-50 w-full shadow-sm'>
<CardContent className='p-6'>
<div className='text-center'>
Expand Down
Loading