Skip to content

Commit c8f5df9

Browse files
fix: lint
1 parent 0858aa4 commit c8f5df9

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

extensions/cli/src/ui/EditMessageSelector.tsx

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Box, Text, useInput } from "ink";
2+
import type { Key } from "ink/build/hooks/use-input.js";
23
import React, { useMemo, useState } from "react";
34

45
import type { ChatHistoryItem } from "../../../../core/index.js";
@@ -45,17 +46,23 @@ export function EditMessageSelector({
4546
}
4647
}, [selectedIndex, isEditing, userMessages, textBuffer]);
4748

48-
useInput((input, key) => {
49-
// If editing, handle text input
50-
if (isEditing) {
51-
// In edit mode, handle text input
49+
// Helper to get message content safely
50+
const getMessageContent = React.useCallback(
51+
(index: number): string => {
52+
return typeof userMessages[index]?.item.message.content === "string"
53+
? userMessages[index].item.message.content
54+
: "";
55+
},
56+
[userMessages],
57+
);
58+
59+
// Handle input when in editing mode
60+
const handleEditModeInput = React.useCallback(
61+
(input: string, key: Key) => {
5262
if (key.escape) {
5363
setIsEditing(false);
5464
// Reset text to original
55-
const content =
56-
typeof userMessages[selectedIndex]?.item.message.content === "string"
57-
? userMessages[selectedIndex].item.message.content
58-
: "";
65+
const content = getMessageContent(selectedIndex);
5966
textBuffer.setText(content);
6067
setEditText(content);
6168
setCursorPosition(content.length);
@@ -76,8 +83,22 @@ export function EditMessageSelector({
7683
setEditText(textBuffer.text);
7784
setCursorPosition(textBuffer.cursor);
7885
}
79-
} else {
80-
// If not editing, handle navigation
86+
},
87+
[
88+
getMessageContent,
89+
selectedIndex,
90+
textBuffer,
91+
userMessages,
92+
onEdit,
93+
setIsEditing,
94+
setEditText,
95+
setCursorPosition,
96+
],
97+
);
98+
99+
// Handle input when in navigation mode
100+
const handleNavigationInput = React.useCallback(
101+
(input: string, key: Key) => {
81102
if (key.upArrow || input === "k") {
82103
// Only navigate if there are messages
83104
if (userMessages.length > 0) {
@@ -96,18 +117,32 @@ export function EditMessageSelector({
96117
// Start editing the selected message - set cursor to end
97118
// Only allow editing if there are messages
98119
if (userMessages.length > 0) {
99-
const content =
100-
typeof userMessages[selectedIndex]?.item.message.content ===
101-
"string"
102-
? userMessages[selectedIndex].item.message.content
103-
: "";
120+
const content = getMessageContent(selectedIndex);
104121
textBuffer.setCursor(content.length);
105122
setCursorPosition(content.length);
106123
setIsEditing(true);
107124
}
108125
} else if (key.escape || (key.ctrl && input === "d")) {
109126
onExit();
110127
}
128+
},
129+
[
130+
userMessages.length,
131+
getMessageContent,
132+
selectedIndex,
133+
textBuffer,
134+
onExit,
135+
setSelectedIndex,
136+
setCursorPosition,
137+
setIsEditing,
138+
],
139+
);
140+
141+
useInput((input, key) => {
142+
if (isEditing) {
143+
handleEditModeInput(input, key);
144+
} else {
145+
handleNavigationInput(input, key);
111146
}
112147
});
113148

0 commit comments

Comments
 (0)