Skip to content

Refactor MessageComponent to improve usage stats handling and readabi…#549

Merged
felix-schultz merged 1 commit intoalphafrom
dev
Apr 2, 2026
Merged

Refactor MessageComponent to improve usage stats handling and readabi…#549
felix-schultz merged 1 commit intoalphafrom
dev

Conversation

@felix-schultz
Copy link
Copy Markdown
Member

This pull request makes a small update to the MessageComponent in the chat UI to improve how usage statistics are handled and displayed for messages. The main change is to ensure that usage statistics are only passed to the UsageStats component when appropriate, which also simplifies the logic for determining when these stats should be shown.

Improvements to usage statistics handling:

@felix-schultz felix-schultz merged commit 96f1ebf into alpha Apr 2, 2026
12 of 16 checks passed
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the usage statistics logic in the MessageComponent to ensure a consistent array structure for the UsageStats component. The reviewer suggested memoizing the usageStats array to maintain a stable reference across renders, preventing potential performance issues in child components.

}, [message.ratingSettings]);

const hasUsageStats = !isUser && Boolean(message.usage_stats?.length);
const usageStats = !isUser ? (message.usage_stats ?? []) : [];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To avoid creating a new empty array on each render when message.usage_stats is not present, which can cause unnecessary re-renders or re-calculations in child components, consider memoizing the usageStats array using useMemo. This will ensure a stable array reference across renders.

Suggested change
const usageStats = !isUser ? (message.usage_stats ?? []) : [];
const usageStats = useMemo(() => !isUser ? (message.usage_stats ?? []) : [], [isUser, message.usage_stats]);

@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant