Merge pull request #7 from LIlGG/fix/loop-call-replace-state

fix: resolve the issue of frequent triggering of replaceState
This commit is contained in:
Takagi
2025-10-10 14:49:08 +08:00
committed by GitHub
2 changed files with 10 additions and 2 deletions

View File

@@ -5,7 +5,7 @@ import type { ServerChatItem } from '~/lib/hooks/useChatEntries';
type Bin = { category: string; items: ServerChatItem[] }; type Bin = { category: string; items: ServerChatItem[] };
export function binDates(_list: 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<string, Bin> = {}; const binLookup: Record<string, Bin> = {};
const bins: Array<Bin> = []; const bins: Array<Bin> = [];

View File

@@ -1,4 +1,5 @@
import { useChat } from '@ai-sdk/react'; import { useChat } from '@ai-sdk/react';
import { useStore } from '@nanostores/react';
import { useSearchParams } from '@remix-run/react'; import { useSearchParams } from '@remix-run/react';
import { DefaultChatTransport, type FileUIPart } from 'ai'; import { DefaultChatTransport, type FileUIPart } from 'ai';
import { animate } from 'framer-motion'; import { animate } from 'framer-motion';
@@ -10,6 +11,7 @@ import { cubicEasingFn } from '~/utils/easings';
import { createScopedLogger } from '~/utils/logger'; import { createScopedLogger } from '~/utils/logger';
import { pagesToArtifacts } from '~/utils/page'; import { pagesToArtifacts } from '~/utils/page';
import { import {
aiState,
getChatStarted, getChatStarted,
setAborted, setAborted,
setChatId, setChatId,
@@ -36,6 +38,7 @@ export function useChatMessage({
const SAVE_PROJECT_DELAY_MS = 1000; const SAVE_PROJECT_DELAY_MS = 1000;
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const { chatStarted } = useStore(aiState);
const { saveProject } = useProject(); const { saveProject } = useProject();
const { refreshUsageStats } = useChatUsage(); const { refreshUsageStats } = useChatUsage();
const { parsedMessages, parseMessages } = useMessageParser(); const { parsedMessages, parseMessages } = useMessageParser();
@@ -89,12 +92,17 @@ export function useChatMessage({
useEffect(() => { useEffect(() => {
if (messages.length > 0) { if (messages.length > 0) {
parseMessages(messages, isLoading); parseMessages(messages, isLoading);
}
}, [messages, isLoading, parseMessages]);
useEffect(() => {
if (currentChatId && chatStarted) {
const url = new URL(window.location.href); const url = new URL(window.location.href);
url.pathname = `/chat/${currentChatId}`; url.pathname = `/chat/${currentChatId}`;
window.history.replaceState({}, '', url); window.history.replaceState({}, '', url);
setChatId(currentChatId); setChatId(currentChatId);
} }
}, [messages, isLoading, parseMessages]); }, [currentChatId, chatStarted]);
useEffect(() => { useEffect(() => {
if (messages.length > 0) { if (messages.length > 0) {