forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convo component re-render every single token yield
- Loading branch information
Showing
2 changed files
with
90 additions
and
29 deletions.
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
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 |
---|---|---|
@@ -1,15 +1,42 @@ | ||
import React, { forwardRef } from 'react' | ||
import React, { forwardRef, useEffect, useState } from 'react' | ||
|
||
import { ThreadMessage } from '@janhq/core' | ||
import { | ||
events, | ||
MessageEvent, | ||
MessageStatus, | ||
ThreadContent, | ||
ThreadMessage, | ||
} from '@janhq/core' | ||
|
||
import SimpleTextMessage from '../SimpleTextMessage' | ||
|
||
type Ref = HTMLDivElement | ||
|
||
const ChatItem = forwardRef<Ref, ThreadMessage>((message, ref) => ( | ||
<div ref={ref} className="relative"> | ||
<SimpleTextMessage {...message} /> | ||
</div> | ||
)) | ||
const ChatItem = forwardRef<Ref, ThreadMessage>((message, ref) => { | ||
const [content, setContent] = useState<ThreadContent[]>(message.content) | ||
const [status, setStatus] = useState<MessageStatus>(message.status) | ||
|
||
function onMessageUpdate(data: ThreadMessage) { | ||
if (data.id === message.id) { | ||
setContent(data.content) | ||
if (data.status !== status) setStatus(data.status) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (message.status === MessageStatus.Pending) | ||
events.on(MessageEvent.OnMessageUpdate, onMessageUpdate) | ||
return () => { | ||
events.off(MessageEvent.OnMessageUpdate, onMessageUpdate) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
return ( | ||
<div ref={ref} className="relative"> | ||
<SimpleTextMessage {...message} content={content} status={status} /> | ||
</div> | ||
) | ||
}) | ||
|
||
export default ChatItem |