From 5b8408d7da272f66ee7bce139d56d450158fc86b Mon Sep 17 00:00:00 2001 From: LIlGG <1103069291@qq.com> Date: Fri, 10 Oct 2025 12:23:42 +0800 Subject: [PATCH] fix: resolve the issue of frequent triggering of replaceState --- app/components/sidebar/date-binning.ts | 2 +- app/lib/hooks/useChatMessage.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/components/sidebar/date-binning.ts b/app/components/sidebar/date-binning.ts index f828154..dc60121 100644 --- a/app/components/sidebar/date-binning.ts +++ b/app/components/sidebar/date-binning.ts @@ -5,7 +5,7 @@ import type { ServerChatItem } from '~/lib/hooks/useChatEntries'; type Bin = { category: string; items: ServerChatItem[] }; export function binDates(_list: ServerChatItem[]) { - const list = _list.toSorted((a, b) => Date.parse(b.timestamp) - Date.parse(a.timestamp)); + const list = _list.slice().sort((a, b) => Date.parse(b.timestamp) - Date.parse(a.timestamp)); const binLookup: Record = {}; const bins: Array = []; diff --git a/app/lib/hooks/useChatMessage.ts b/app/lib/hooks/useChatMessage.ts index b39843c..547352f 100644 --- a/app/lib/hooks/useChatMessage.ts +++ b/app/lib/hooks/useChatMessage.ts @@ -1,4 +1,5 @@ import { useChat } from '@ai-sdk/react'; +import { useStore } from '@nanostores/react'; import { useSearchParams } from '@remix-run/react'; import { DefaultChatTransport, type FileUIPart } from 'ai'; import { animate } from 'framer-motion'; @@ -10,6 +11,7 @@ import { cubicEasingFn } from '~/utils/easings'; import { createScopedLogger } from '~/utils/logger'; import { pagesToArtifacts } from '~/utils/page'; import { + aiState, getChatStarted, setAborted, setChatId, @@ -36,6 +38,7 @@ export function useChatMessage({ const SAVE_PROJECT_DELAY_MS = 1000; const [searchParams] = useSearchParams(); + const { chatStarted } = useStore(aiState); const { saveProject } = useProject(); const { refreshUsageStats } = useChatUsage(); const { parsedMessages, parseMessages } = useMessageParser(); @@ -89,12 +92,17 @@ export function useChatMessage({ useEffect(() => { if (messages.length > 0) { parseMessages(messages, isLoading); + } + }, [messages, isLoading, parseMessages]); + + useEffect(() => { + if (currentChatId && chatStarted) { const url = new URL(window.location.href); url.pathname = `/chat/${currentChatId}`; window.history.replaceState({}, '', url); setChatId(currentChatId); } - }, [messages, isLoading, parseMessages]); + }, [currentChatId, chatStarted]); useEffect(() => { if (messages.length > 0) {