-
-
Notifications
You must be signed in to change notification settings - Fork 16.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Up and Down arrow functionality to chat messages (#3440)
* Add Up and Down arrow functionality to chat messages - Works like Linux shell - History limited to 10 messages * Fix linting errors * Update EmbedChat.jsx --------- Co-authored-by: Henry Heng <[email protected]>
- Loading branch information
1 parent
d64cb70
commit 835b151
Showing
2 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
export class ChatInputHistory { | ||
constructor(maxHistory = 10) { | ||
this.history = [] | ||
this.currentIndex = -1 | ||
this.tempInput = '' | ||
this.maxHistory = maxHistory | ||
this.loadHistory() | ||
} | ||
|
||
addToHistory(input) { | ||
if (!input.trim()) return | ||
if (this.history[0] !== input) { | ||
this.history.unshift(input) | ||
if (this.history.length > this.maxHistory) { | ||
this.history.pop() | ||
} | ||
} | ||
this.currentIndex = -1 | ||
this.saveHistory() | ||
} | ||
|
||
getPreviousInput(currentInput) { | ||
if (this.currentIndex === -1) { | ||
this.tempInput = currentInput | ||
} | ||
if (this.currentIndex < this.history.length - 1) { | ||
this.currentIndex++ | ||
return this.history[this.currentIndex] | ||
} | ||
return this.history[this.currentIndex] || this.tempInput | ||
} | ||
|
||
getNextInput() { | ||
if (this.currentIndex > -1) { | ||
this.currentIndex-- | ||
if (this.currentIndex === -1) { | ||
return this.tempInput | ||
} | ||
return this.history[this.currentIndex] | ||
} | ||
return this.tempInput | ||
} | ||
|
||
saveHistory() { | ||
try { | ||
localStorage.setItem('chatInputHistory', JSON.stringify(this.history)) | ||
} catch (error) { | ||
console.warn('Failed to save chat history to localStorage:', error) | ||
} | ||
} | ||
|
||
loadHistory() { | ||
try { | ||
const saved = localStorage.getItem('chatInputHistory') | ||
if (saved) { | ||
this.history = JSON.parse(saved) | ||
} | ||
} catch (error) { | ||
console.warn('Failed to load chat history from localStorage:', error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters