Merge pull request #4216 from janhq/fix/react-markdown-crash-on-code-highlighting

fix: react-markdown crash on code highlighting - threads switching do not take effect sometime
This commit is contained in:
Louis 2024-12-04 16:55:48 +07:00 committed by GitHub
commit 38ff118276
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 5 deletions

View File

@ -38,7 +38,7 @@
"react-icons": "^4.12.0", "react-icons": "^4.12.0",
"react-markdown": "^9.0.1", "react-markdown": "^9.0.1",
"react-toastify": "^9.1.3", "react-toastify": "^9.1.3",
"rehype-highlight": "^6.0.0", "rehype-highlight": "^7.0.1",
"rehype-highlight-code-lines": "^1.0.4", "rehype-highlight-code-lines": "^1.0.4",
"rehype-katex": "^7.0.1", "rehype-katex": "^7.0.1",
"rehype-raw": "^7.0.0", "rehype-raw": "^7.0.0",

View File

@ -14,9 +14,11 @@ import LoadModelError from '../LoadModelError'
import EmptyThread from './EmptyThread' import EmptyThread from './EmptyThread'
import { getCurrentChatMessagesAtom } from '@/helpers/atoms/ChatMessage.atom' import { getCurrentChatMessagesAtom } from '@/helpers/atoms/ChatMessage.atom'
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'
const ChatConfigurator = memo(() => { const ChatConfigurator = memo(() => {
const messages = useAtomValue(getCurrentChatMessagesAtom) const messages = useAtomValue(getCurrentChatMessagesAtom)
const currentThread = useAtomValue(activeThreadAtom)
const [current, setCurrent] = useState<ThreadMessage[]>([]) const [current, setCurrent] = useState<ThreadMessage[]>([])
const loadModelError = useAtomValue(loadModelErrorAtom) const loadModelError = useAtomValue(loadModelErrorAtom)
@ -31,12 +33,12 @@ const ChatConfigurator = memo(() => {
useEffect(() => { useEffect(() => {
if ( if (
messages?.length !== current?.length || !isMessagesIdentificial(messages, current) ||
!isMessagesIdentificial(messages, current) messages.some((e) => e.thread_id !== currentThread?.id)
) { ) {
setCurrent(messages) setCurrent(messages)
} }
}, [messages, current, loadModelError]) }, [messages, current, loadModelError, currentThread])
if (!messages.length) return <EmptyThread /> if (!messages.length) return <EmptyThread />
return ( return (
@ -119,7 +121,7 @@ const ChatBody = memo(
> >
{items.map((virtualRow) => ( {items.map((virtualRow) => (
<div <div
key={virtualRow.key} key={messages[virtualRow.index]?.id}
data-index={virtualRow.index} data-index={virtualRow.index}
ref={virtualizer.measureElement} ref={virtualizer.measureElement}
> >