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

fix: Preserve chatbot feedback after data change #2442 #2443

Merged
merged 3 commits into from
Jan 7, 2025
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
23 changes: 23 additions & 0 deletions ui/src/chatbot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,29 @@ describe('XChatbot', () => {
expect(emitMock).toHaveBeenCalledTimes(1)
})

it('Preserve feedback on data change', () => {
const { container, getByRole, getByTestId, rerender } = render(<XChatbot {...{ ...model, data, events: ['feedback'] }} />)
const likeButton = container.querySelector("i[data-icon-name='Like']") as HTMLLIElement
expect(likeButton).toBeInTheDocument()

fireEvent.click(likeButton)
const likeSolidButton = container.querySelector("i[data-icon-name='LikeSolid']") as HTMLLIElement
expect(likeSolidButton).toBeInTheDocument()

const input = getByRole('textbox')
fireEvent.change(input, { target: { value: 'Woohoo' } })
fireEvent.click(getByTestId(`${name}-submit`))

// TODO: "rerender" does not append new message
rerender(<XChatbot {...{
...model, data: [...data,
{ content: 'I like it!', from_user: false }
], events: ['feedback']
}} />)

expect(container.querySelector("i[data-icon-name='LikeSolid']") as HTMLLIElement).toBeInTheDocument()
})

it('Renders suggestions when specified', () => {
const { getByText } = render(<XChatbot {...{ ...model, suggestions }} />)

Expand Down
4 changes: 3 additions & 1 deletion ui/src/chatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ export const XChatbot = (props: Chatbot) => {
setIsInfiniteLoading(true)
}
}, [props.events, props.name]),
onDataChange = React.useCallback(() => { if (props.data) setMsgs(processData(props.data)) }, [props.data]),
onDataChange = React.useCallback(() => {
if (props.data) setMsgs(prevMsgs => processData(props.data).map((msg, idx) => ({ ...msg, positive: prevMsgs?.[idx]?.positive })))
}, [props.data]),
handlePositive = (id: I) => {
setMsgs(messages => {
if (messages[id]?.positive) return messages
Expand Down
Loading