Skip to content

Commit 841d2a9

Browse files
committed
refactor: improve readability of feedback code
- Fix indentation in feedback-store.ts - Simplify symbol selection logic in feedback-icon-button.tsx - Extract message context building helper in feedback-container.tsx - Rename functions/variables for clarity in feedback-input-mode.tsx - Simplify keyboard handler logic
1 parent 76f00e9 commit 841d2a9

File tree

4 files changed

+57
-58
lines changed

4 files changed

+57
-58
lines changed

cli/src/components/feedback-container.tsx

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,44 +61,56 @@ export const FeedbackContainer: React.FC<FeedbackContainerProps> = ({
6161

6262
const previousFeedbackModeRef = useRef(feedbackMode)
6363

64+
const buildMessageContext = useCallback(
65+
(targetMessageId: string | null) => {
66+
const target = targetMessageId
67+
? messages.find((m: ChatMessage) => m.id === targetMessageId)
68+
: null
69+
70+
const targetIndex = target ? messages.indexOf(target) : messages.length - 1
71+
const startIndex = Math.max(0, targetIndex - 9)
72+
const recentMessages = messages
73+
.slice(startIndex, targetIndex + 1)
74+
.map((m: ChatMessage) => ({
75+
type: m.variant,
76+
id: m.id,
77+
...(m.completionTime && { completionTime: m.completionTime }),
78+
...(m.credits && { credits: m.credits }),
79+
}))
80+
81+
return { target, recentMessages }
82+
},
83+
[messages],
84+
)
85+
6486
const handleFeedbackSubmit = useCallback(() => {
6587
const text = feedbackText.trim()
6688
if (!text) {
6789
return
6890
}
6991

70-
const target = feedbackMessageId
71-
? messages.find((m: ChatMessage) => m.id === feedbackMessageId)
72-
: null
73-
74-
const targetIndex = target ? messages.indexOf(target) : messages.length - 1
75-
const startIndex = Math.max(0, targetIndex - 9)
76-
const recent = messages
77-
.slice(startIndex, targetIndex + 1)
78-
.map((m: ChatMessage) => ({
79-
type: m.variant,
80-
id: m.id,
81-
...(m.completionTime && { completionTime: m.completionTime }),
82-
...(m.credits && { credits: m.credits }),
83-
}))
84-
85-
logger.info({
86-
eventId: AnalyticsEvent.FEEDBACK_SUBMITTED,
87-
source: 'cli',
88-
messageId: target?.id || null,
89-
variant: target?.variant || null,
90-
completionTime: target?.completionTime || null,
91-
credits: target?.credits || null,
92-
agentMode,
93-
sessionCreditsUsed,
94-
recentMessages: recent,
95-
feedback: {
96-
text,
97-
category: feedbackCategory,
98-
type: feedbackMessageId ? 'message' : 'general',
92+
const { target, recentMessages } = buildMessageContext(feedbackMessageId)
93+
94+
logger.info(
95+
{
96+
eventId: AnalyticsEvent.FEEDBACK_SUBMITTED,
97+
source: 'cli',
98+
messageId: target?.id || null,
99+
variant: target?.variant || null,
100+
completionTime: target?.completionTime || null,
101+
credits: target?.credits || null,
102+
agentMode,
103+
sessionCreditsUsed,
104+
recentMessages,
105+
feedback: {
106+
text,
107+
category: feedbackCategory,
108+
type: feedbackMessageId ? 'message' : 'general',
109+
},
110+
runState,
99111
},
100-
runState,
101-
}, 'User submitted feedback')
112+
'User submitted feedback',
113+
)
102114

103115
if (feedbackMessageId) {
104116
markMessageFeedbackSubmitted(feedbackMessageId, feedbackCategory)
@@ -115,7 +127,7 @@ export const FeedbackContainer: React.FC<FeedbackContainerProps> = ({
115127
feedbackText,
116128
feedbackMessageId,
117129
feedbackCategory,
118-
messages,
130+
buildMessageContext,
119131
agentMode,
120132
sessionCreditsUsed,
121133
runState,

cli/src/components/feedback-icon-button.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,11 @@ export const FeedbackIconButton: React.FC<FeedbackIconButtonProps> = ({
5353
hover.scheduleClose()
5454
}
5555

56-
// Determine which symbol to show based on selected category
57-
const getSymbol = () => {
58-
if (selectedCategory === 'good_result') {
59-
return '▲▽' // Good selected - filled up, outlined down
60-
} else if (selectedCategory === 'bad_result') {
61-
return '△▼' // Bad selected - outlined up, filled down
62-
}
63-
return '△▽' // Default - both outlined
56+
const symbolsByCategory: Record<string, string> = {
57+
good_result: '▲▽', // Good selected - filled up, outlined down
58+
bad_result: '△▼', // Bad selected - outlined up, filled down
6459
}
65-
66-
const textCollapsed = `${getSymbol()}`
60+
const textCollapsed = symbolsByCategory[selectedCategory || ''] || '△▽'
6761
const textExpanded = '[how was this?]'
6862

6963
return (

cli/src/components/feedback-input-mode.tsx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ const FEEDBACK_CONTAINER_HORIZONTAL_INSET = 4 // border + padding on each side
5656
const CATEGORY_BUTTON_EXTRA_WIDTH = 6 // indicator + padding + border
5757
const CATEGORY_BUTTON_GAP_WIDTH = 1
5858

59-
const getCategoryRowWidth = (labels: readonly string[]): number =>
59+
const calculateCategoryRowWidth = (labels: readonly string[]): number =>
6060
labels.reduce((total, label, idx) => {
6161
const buttonWidth = label.length + CATEGORY_BUTTON_EXTRA_WIDTH
62-
const gap = idx === 0 ? 0 : CATEGORY_BUTTON_GAP_WIDTH
63-
return total + buttonWidth + gap
62+
const gapWidth = idx === 0 ? 0 : CATEGORY_BUTTON_GAP_WIDTH
63+
return total + buttonWidth + gapWidth
6464
}, 0)
6565

66-
const FULL_CATEGORY_ROW_WIDTH = getCategoryRowWidth(
67-
CATEGORY_OPTIONS.map((option) => option.label),
66+
const FULL_CATEGORY_ROW_WIDTH = calculateCategoryRowWidth(
67+
CATEGORY_OPTIONS.map((opt) => opt.label),
6868
)
6969

7070
interface FeedbackTextSectionProps {
@@ -162,20 +162,18 @@ export const FeedbackInputMode: React.FC<FeedbackInputModeProps> = ({
162162
const inputRef = externalInputRef || internalInputRef
163163
const canSubmit = value.trim().length > 0
164164
const [closeButtonHovered, setCloseButtonHovered] = useState(false)
165-
const availableCategoryWidth = Math.max(
165+
const availableWidth = Math.max(
166166
0,
167167
width - FEEDBACK_CONTAINER_HORIZONTAL_INSET,
168168
)
169-
const shouldUseShortLabels =
170-
FULL_CATEGORY_ROW_WIDTH > availableCategoryWidth
169+
const shouldUseShortLabels = FULL_CATEGORY_ROW_WIDTH > availableWidth
171170

172171
// Handle keyboard shortcuts
173172
useKeyboard(
174173
useCallback(
175174
(key) => {
176175
const isCtrlC = key.ctrl && key.name === 'c'
177176
const isEscape = key.name === 'escape'
178-
const isCtrlEnter = false // handled via onKeyIntercept
179177

180178
if (!isCtrlC && !isEscape) return
181179

@@ -189,13 +187,8 @@ export const FeedbackInputMode: React.FC<FeedbackInputModeProps> = ({
189187
if (isEscape) {
190188
onCancel()
191189
} else if (isCtrlC) {
192-
if (value.length === 0) {
193-
onCancel()
194-
} else {
195-
onClear()
196-
}
190+
value.length === 0 ? onCancel() : onClear()
197191
}
198-
// Ctrl+Enter handled via onKeyIntercept
199192
},
200193
[value, onCancel, onClear, onSubmit, canSubmit],
201194
),

cli/src/state/feedback-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const useFeedbackStore = create<FeedbackStore>()(
9191
markMessageFeedbackSubmitted: (messageId, category) =>
9292
set((state) => {
9393
state.messagesWithFeedback.add(messageId)
94-
state.messageFeedbackCategories.set(messageId, category)
94+
state.messageFeedbackCategories.set(messageId, category)
9595
}),
9696

9797
resetFeedbackForm: () =>

0 commit comments

Comments
 (0)