Skip to content

Commit b153548

Browse files
authored
Merge pull request #4732 from continuedev/nate/arrow-down-hotfix
Nate/arrow-down-hotfix
2 parents 12fb378 + ed5befe commit b153548

File tree

4 files changed

+37
-93
lines changed

4 files changed

+37
-93
lines changed

extensions/vscode/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gui/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gui/src/components/mainInput/TipTapEditor.tsx

+4-6
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,7 @@ function TipTapEditor(props: TipTapEditorProps) {
301301
}
302302
}
303303

304-
// Ensure we're using the correct history key - chat for main input, edit for edit mode
305-
const historyKey = props.historyKey === 'edit' ? 'edit' : 'chat';
306-
const { prevRef, nextRef, addRef } = useInputHistory(historyKey);
304+
const { prevRef, nextRef, addRef } = useInputHistory(props.historyKey);
307305

308306
const editor: Editor | null = useEditor({
309307
extensions: [
@@ -662,9 +660,9 @@ function TipTapEditor(props: TipTapEditorProps) {
662660
return;
663661
}
664662

665-
// Always add to input history regardless of input type
666-
// This ensures that both chat and edit modes maintain their own history
667-
addRef.current(json);
663+
if (props.isMainInput) {
664+
addRef.current(json);
665+
}
668666

669667
props.onEnter(json, modifiers, editor);
670668
},

gui/src/hooks/useInputHistory.ts

+29-83
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JSONContent } from "@tiptap/react";
2-
import { useEffect, useState } from "react";
2+
import { useState } from "react";
33
import { getLocalStorage, setLocalStorage } from "../util/localStorage";
44
import useUpdatingRef from "./useUpdatingRef";
55

@@ -10,112 +10,58 @@ const emptyJsonContent = () => ({
1010

1111
const MAX_HISTORY_LENGTH = 100;
1212

13-
// Completely isolated memory stores for different history types
14-
// This ensures no crossover between edit and chat histories
15-
const CHAT_HISTORY_STORE: JSONContent[] = [];
16-
const EDIT_HISTORY_STORE: JSONContent[] = [];
17-
18-
// Initialize the history stores from localStorage (only do this once)
19-
let storesInitialized = false;
20-
function initializeStores() {
21-
22-
if (!storesInitialized) {
23-
// Initialize chat history
24-
const chatHistory = getLocalStorage('inputHistory_chat');
25-
if (chatHistory) {
26-
CHAT_HISTORY_STORE.push(...chatHistory.slice(-MAX_HISTORY_LENGTH));
27-
}
28-
29-
// Initialize edit history
30-
const editHistory = getLocalStorage('inputHistory_edit');
31-
if (editHistory) {
32-
EDIT_HISTORY_STORE.push(...editHistory.slice(-MAX_HISTORY_LENGTH));
33-
}
34-
35-
storesInitialized = true;
36-
}
37-
}
38-
39-
40-
// Initialize stores immediately
41-
initializeStores();
42-
43-
/**
44-
* Custom hook for managing input history with complete separation between history types
45-
*/
4613
export function useInputHistory(historyKey: string) {
47-
// Determine which history store to use
48-
const historyStore = historyKey === 'edit' ? EDIT_HISTORY_STORE : CHAT_HISTORY_STORE;
49-
const storageKey = historyKey === 'edit' ? 'inputHistory_edit' as const : 'inputHistory_chat' as const;
50-
51-
// Use a reference to the appropriate history store
52-
const [inputHistory, setInputHistory] = useState<JSONContent[]>(historyStore);
53-
const [pendingInput, setPendingInput] = useState<JSONContent>(emptyJsonContent());
14+
const [inputHistory, setInputHistory] = useState<JSONContent[]>(
15+
getLocalStorage(`inputHistory_${historyKey}`)?.slice(-MAX_HISTORY_LENGTH) ??
16+
[],
17+
);
18+
const [pendingInput, setPendingInput] =
19+
useState<JSONContent>(emptyJsonContent());
5420
const [currentIndex, setCurrentIndex] = useState(inputHistory.length);
55-
56-
// Keep the view of history updated when the underlying store changes
57-
useEffect(() => {
58-
setInputHistory([...historyStore]);
59-
setCurrentIndex(historyStore.length);
60-
}, [historyStore.length]);
6121

6222
function prev(currentInput: JSONContent) {
63-
if (currentIndex === inputHistory.length) {
23+
let index = currentIndex;
24+
25+
if (index === inputHistory.length) {
6426
setPendingInput(currentInput);
6527
}
6628

67-
if (currentIndex > 0) {
68-
setCurrentIndex(currentIndex - 1);
69-
return inputHistory[currentIndex - 1];
29+
if (index > 0 && index <= inputHistory.length) {
30+
setCurrentIndex((prevState) => prevState - 1);
31+
return inputHistory[index - 1];
7032
}
71-
72-
// Stay at the first history item if we're already there
73-
if (currentIndex === 0 && inputHistory.length > 0) {
74-
return inputHistory[0];
75-
}
76-
77-
return currentInput;
7833
}
7934

8035
function next() {
81-
if (currentIndex < inputHistory.length) {
82-
setCurrentIndex(currentIndex + 1);
83-
84-
if (currentIndex === inputHistory.length - 1) {
36+
let index = currentIndex;
37+
if (index >= 0 && index < inputHistory.length) {
38+
setCurrentIndex((prevState) => prevState + 1);
39+
if (index === inputHistory.length - 1) {
8540
return pendingInput;
8641
}
87-
88-
return inputHistory[currentIndex + 1];
42+
return inputHistory[index + 1];
8943
}
90-
91-
// Stay at pending input if we're already at the end
92-
return pendingInput;
9344
}
9445

9546
function add(inputValue: JSONContent) {
9647
setPendingInput(emptyJsonContent());
9748

98-
// Don't add duplicates
9949
if (
100-
historyStore.length > 0 &&
101-
JSON.stringify(historyStore[historyStore.length - 1]) === JSON.stringify(inputValue)
50+
JSON.stringify(inputHistory[inputHistory.length - 1]) ===
51+
JSON.stringify(inputValue)
10252
) {
103-
setCurrentIndex(historyStore.length);
53+
setCurrentIndex(inputHistory.length);
10454
return;
10555
}
10656

107-
// Update the appropriate history store
108-
while (historyStore.length >= MAX_HISTORY_LENGTH) {
109-
historyStore.shift(); // Remove oldest items if we exceed the limit
110-
}
111-
historyStore.push(inputValue);
112-
113-
// Update the view of history
114-
setInputHistory([...historyStore]);
115-
setCurrentIndex(historyStore.length);
116-
117-
// Update localStorage
118-
setLocalStorage(storageKey, [...historyStore]);
57+
setCurrentIndex(inputHistory.length + 1);
58+
setInputHistory((prev) => {
59+
return [...prev, inputValue].slice(-MAX_HISTORY_LENGTH);
60+
});
61+
setLocalStorage(
62+
`inputHistory_${historyKey}`,
63+
[...inputHistory, inputValue].slice(-MAX_HISTORY_LENGTH),
64+
);
11965
}
12066

12167
const prevRef = useUpdatingRef(prev, [inputHistory]);

0 commit comments

Comments
 (0)