Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Up and Down arrow functionality to chat messages #3440

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions packages/ui/src/views/chatmessage/ChatInputHistory.js
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)
}
}
}
18 changes: 17 additions & 1 deletion packages/ui/src/views/chatmessage/ChatMessage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ import { isValidURL, removeDuplicateURL, setLocalStorageChatflow, getLocalStorag
import useNotifier from '@/utils/useNotifier'
import FollowUpPromptsCard from '@/ui-component/cards/FollowUpPromptsCard'

// History
import { ChatInputHistory } from './ChatInputHistory'

const messageImageStyle = {
width: '128px',
height: '128px',
Expand Down Expand Up @@ -185,6 +188,7 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
const [uploadedFiles, setUploadedFiles] = useState([])
const [imageUploadAllowedTypes, setImageUploadAllowedTypes] = useState('')
const [fileUploadAllowedTypes, setFileUploadAllowedTypes] = useState('')
const [inputHistory] = useState(new ChatInputHistory(10))

const inputRef = useRef(null)
const getChatmessageApi = useApi(chatmessageApi.getInternalChatmessageFromChatflow)
Expand Down Expand Up @@ -768,6 +772,10 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview

if (selectedInput !== undefined && selectedInput.trim() !== '') input = selectedInput

if (input.trim()) {
inputHistory.addToHistory(input)
}

setLoading(true)
let uploads = previews.map((item) => {
return {
Expand Down Expand Up @@ -934,7 +942,15 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
const handleEnter = (e) => {
// Check if IME composition is in progress
const isIMEComposition = e.isComposing || e.keyCode === 229
if (e.key === 'Enter' && userInput && !isIMEComposition) {
if (e.key === 'ArrowUp' && !isIMEComposition) {
e.preventDefault()
const previousInput = inputHistory.getPreviousInput(userInput)
setUserInput(previousInput)
} else if (e.key === 'ArrowDown' && !isIMEComposition) {
e.preventDefault()
const nextInput = inputHistory.getNextInput()
setUserInput(nextInput)
} else if (e.key === 'Enter' && userInput && !isIMEComposition) {
if (!e.shiftKey && userInput) {
handleSubmit(e)
}
Expand Down
Loading