Skip to content

Commit

Permalink
Merge pull request #1834 from ashrafchowdury/refactor/chat-input-func…
Browse files Browse the repository at this point in the history
…tion-code-duplication

Refactor: removed code duplication from ChatInputs component
  • Loading branch information
mmabrouk authored Jul 9, 2024
2 parents f5b07dc + 182cd3f commit bef4230
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions agenta-web/src/components/ChatInputs/ChatInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,34 @@ const ChatInputs: React.FC<Props> = ({
disableEditRole = true
}

const handleRoleChange = (index: number, role: ChatRole) => {
const newMessages = [...messages]
newMessages[index].role = role
const updateMessages = (newMessages: ChatMessage[]) => {
setMessages(newMessages)
if (onChangeRef.current) {
onChangeRef.current(cloneDeep(newMessages))
}
}

const handleRoleChange = (index: number, role: ChatRole) => {
const newMessages = [...messages]
newMessages[index].role = role
updateMessages(newMessages)
}

const handleInputChange = (index: number, event: React.ChangeEvent<HTMLTextAreaElement>) => {
const {value} = event.target
const newMessages = [...messages]
newMessages[index].content = value
setMessages(newMessages)
if (onChangeRef.current) {
onChangeRef.current(cloneDeep(newMessages))
}
updateMessages(newMessages)
}

const handleDelete = (index: number) => {
const newMessages = messages.filter((_, i) => i !== index)
setMessages(newMessages)
if (onChangeRef.current) {
onChangeRef.current(cloneDeep(newMessages))
}
updateMessages(newMessages)
}

const handleAdd = () => {
const newMessages = messages.concat([getDefaultNewMessage()])
setMessages(newMessages)
if (onChangeRef.current) {
onChangeRef.current(cloneDeep(newMessages))
}
updateMessages(newMessages)
}

useEffect(() => {
Expand Down

0 comments on commit bef4230

Please sign in to comment.