@@ -1,4 +1,4 @@ -import { useCallback, useEffect, useRef, useState } from 'react' +import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react' import { useSession } from 'next-auth/react' import { useAppDispatch } from '#/app/store/store' @@ -223,6 +223,7 @@ export function useChatModel( const { data } = useSession() const dispatch = useAppDispatch() const [messages, setMessages] = useState(null) + const [loadedChatUid, setLoadedChatUid] = useState(null) const [loading, setLoading] = useState(false) const [paginationLoading, setPaginationLoading] = useState(false) const [offset, setOffset] = useState(0) @@ -555,6 +556,10 @@ export function useChatModel( } }, []) + useLayoutEffect(() => { + setLoadedChatUid(null) + }, [currentChat]) + useEffect(() => { abortRef.current?.abort() abortRef.current = null @@ -564,6 +569,7 @@ export function useChatModel( if (!currentChat) { setMessages([]) + setLoadedChatUid(null) setOffset(0) reconnectAttemptedForChatRef.current = null return @@ -587,11 +593,13 @@ export function useChatModel( if (!Array.isArray(answer)) { showMessage('Ошибка загрузки чата') setMessages([]) + setLoadedChatUid(currentChat) return } const loadedMessages = answer.reverse() setMessages(loadedMessages) + setLoadedChatUid(currentChat) setOffset(answer.length) if (streamingRef.current && data?.access) { @@ -867,5 +875,7 @@ export function useChatModel( [currentChat, data?.access] ) - return { messages, sendMessage, loading, paginationLoading, getMessagesPagination, deleteMessage } + const displayMessages = loadedChatUid === currentChat ? messages : null + + return { messages: displayMessages, sendMessage, loading, paginationLoading, getMessagesPagination, deleteMessage } } @@ -11,7 +11,7 @@ import { setParams as setParametres } from '#/app/store/model-parametres-store' import { useAppSelector } from '#/app/store/store' import { IModel } from '#/entities/model-entity' import BotParamsMap from '#/features/bot-params/bot-params-map' -import { selectCurrentChat } from '#/features/chats/chats-slice' +import { selectCurrentChat, resetChatsState, selectChatsLoading } from '#/features/chats/chats-slice' import { usePredictPrice } from '#/features/predict-price/model/use-predict-price' import Title from '#/features/title/title' import { TutorialContext } from '#/features/tutorial-context/tutorial-context' @@ -52,7 +52,9 @@ const Page: NextPageWithLayout = () => { const router = useRouter() const { push } = router + const routeSlug = typeof router.query.slug === 'string' ? router.query.slug : '' const currentChat = useAppSelector(selectCurrentChat) + const chatsLoading = useAppSelector(selectChatsLoading) const { messages, sendMessage, loading, paginationLoading, getMessagesPagination, deleteMessage } = useChatModel( currentChat, @@ -70,31 +72,52 @@ const Page: NextPageWithLayout = () => { ? '75vh' : `calc(100dvh - ${(botParams?.tags ?? []).length === 0 ? '200px' : '285px'})` - const isMessagesLoading = messages === null + const isBotReady = botParams !== null && botParams.slug === routeSlug && modelType === routeSlug + const isContentLoading = !isBotReady || chatsLoading || messages === null + + React.useLayoutEffect(() => { + setBotParams(null) + setModelType('') + setVersion('') + setFile(null) + setInputContent('') + dispatch(resetChatsState()) + }, [routeSlug, dispatch]) React.useEffect(() => { - if (data?.access) { - model_api.getBotParams(router.asPath.split('/')[2], data.access).then((res) => { - if (!res.title) return push('/404') - setBotParams(res) - setModelType(res.slug) - if (res.versions.length !== 0) { - setVersion(res.versions[0].slug) - dispatch( - setParametres( - res.parameters.reduce( - (a, v) => (v.versions.includes(res.versions[0].slug) ? { ...a, [v.key]: v.values.default } : { ...a }), - {} - ) + if (!data?.access || !routeSlug) { + return + } + + let cancelled = false + + model_api.getBotParams(routeSlug, data.access).then((res) => { + if (cancelled || res.slug !== routeSlug) { + return + } + if (!res.title) return push('/404') + setBotParams(res) + setModelType(res.slug) + if (res.versions.length !== 0) { + setVersion(res.versions[0].slug) + dispatch( + setParametres( + res.parameters.reduce( + (a, v) => (v.versions.includes(res.versions[0].slug) ? { ...a, [v.key]: v.values.default } : { ...a }), + {} ) ) - } else { - setVersion('') - dispatch(setParametres(res.parameters.reduce((a, v) => ({ ...a, [v.key]: v.values.default }), {}))) - } - }) + ) + } else { + setVersion('') + dispatch(setParametres(res.parameters.reduce((a, v) => ({ ...a, [v.key]: v.values.default }), {}))) + } + }) + + return () => { + cancelled = true } - }, [data?.access, dispatch, push, router.asPath, router.query]) + }, [data?.access, dispatch, push, routeSlug]) const resetParams = () => { if (botParams) { @@ -269,7 +292,7 @@ const Page: NextPageWithLayout = () => { - {isMessagesLoading ? ( + {isContentLoading ? (