|
1 | 1 | import { TextAttributes } from '@opentui/core' |
2 | 2 | import { pluralize } from '@codebuff/common/util/string' |
3 | | -import React, { memo, useCallback, type ReactNode } from 'react' |
| 3 | +import React, { memo, useCallback, useMemo, type ReactNode } from 'react' |
4 | 4 |
|
5 | 5 | import { AgentBranchItem } from './agent-branch-item' |
6 | 6 | import { ElapsedTimer } from './elapsed-timer' |
7 | 7 | import { FeedbackIconButton } from './feedback-icon-button' |
8 | 8 | import { useTheme } from '../hooks/use-theme' |
9 | 9 | import { useWhyDidYouUpdateById } from '../hooks/use-why-did-you-update' |
10 | 10 | import { isTextBlock, isToolBlock } from '../types/chat' |
11 | | -import { logger } from '../utils/logger' |
12 | 11 | import { type MarkdownPalette } from '../utils/markdown-renderer' |
13 | 12 | import { |
14 | 13 | useFeedbackStore, |
@@ -86,22 +85,37 @@ export const MessageBlock = memo((props: MessageBlockProps): ReactNode => { |
86 | 85 | }) |
87 | 86 |
|
88 | 87 | const theme = useTheme() |
89 | | - const isFeedbackOpen = useFeedbackStore(selectIsFeedbackOpenForMessage(messageId)) |
90 | | - const hasSubmittedFeedback = useFeedbackStore(selectHasSubmittedFeedback(messageId)) |
91 | | - const selectedFeedbackCategory = useFeedbackStore(selectMessageFeedbackCategory(messageId)) |
| 88 | + |
| 89 | + // Memoize selectors to prevent new function references on every render |
| 90 | + const selectIsFeedbackOpenMemo = useMemo( |
| 91 | + () => selectIsFeedbackOpenForMessage(messageId), |
| 92 | + [messageId], |
| 93 | + ) |
| 94 | + const selectHasSubmittedFeedbackMemo = useMemo( |
| 95 | + () => selectHasSubmittedFeedback(messageId), |
| 96 | + [messageId], |
| 97 | + ) |
| 98 | + const selectMessageFeedbackCategoryMemo = useMemo( |
| 99 | + () => selectMessageFeedbackCategory(messageId), |
| 100 | + [messageId], |
| 101 | + ) |
| 102 | + |
| 103 | + const isFeedbackOpen = useFeedbackStore(selectIsFeedbackOpenMemo) |
| 104 | + const hasSubmittedFeedback = useFeedbackStore(selectHasSubmittedFeedbackMemo) |
| 105 | + const selectedFeedbackCategory = useFeedbackStore(selectMessageFeedbackCategoryMemo) |
92 | 106 |
|
93 | 107 | const resolvedTextColor = textColor ?? theme.foreground |
94 | 108 | const shouldShowLoadingTimer = isAi && isLoading && !isComplete |
95 | 109 | const shouldShowCompletionFooter = isAi && isComplete |
96 | | - const canRequestFeedback = |
97 | | - shouldShowCompletionFooter && !hasSubmittedFeedback |
| 110 | + const canRequestFeedback = shouldShowCompletionFooter && !hasSubmittedFeedback |
98 | 111 | const isGoodOrBadSelection = |
99 | 112 | selectedFeedbackCategory === 'good_result' || |
100 | 113 | selectedFeedbackCategory === 'bad_result' |
101 | 114 | const shouldShowSubmittedFeedbackState = |
102 | 115 | shouldShowCompletionFooter && hasSubmittedFeedback && isGoodOrBadSelection |
103 | 116 | const shouldRenderFeedbackButton = |
104 | | - Boolean(onFeedback) && (canRequestFeedback || shouldShowSubmittedFeedbackState) |
| 117 | + Boolean(onFeedback) && |
| 118 | + (canRequestFeedback || shouldShowSubmittedFeedbackState) |
105 | 119 |
|
106 | 120 | const handleFeedbackOpen = useCallback(() => { |
107 | 121 | if (!canRequestFeedback || !onFeedback) return |
|
0 commit comments