mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 09:21:24 +08:00
fix(core): prevent reload pinned chat infinitely (#13226)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved chat stability by centralizing and simplifying the logic for resetting chat content, reducing unnecessary reloads and preventing infinite loading cycles. * **Refactor** * Streamlined internal chat content management for more reliable session handling and smoother user experience. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -148,6 +148,15 @@ export const Component = () => {
|
|||||||
}
|
}
|
||||||
}, [client, createSession, currentSession, isTogglingPin, workspaceId]);
|
}, [client, createSession, currentSession, isTogglingPin, workspaceId]);
|
||||||
|
|
||||||
|
// remove the old content to trigger re-mount
|
||||||
|
// to avoid infinitely load and mount, should not make `chatContent` as dependency
|
||||||
|
const reMountChatContent = useCallback(() => {
|
||||||
|
setChatContent(prev => {
|
||||||
|
prev?.remove();
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
const onOpenSession = useCallback(
|
const onOpenSession = useCallback(
|
||||||
(sessionId: string) => {
|
(sessionId: string) => {
|
||||||
if (isOpeningSession) return;
|
if (isOpeningSession) return;
|
||||||
@@ -156,10 +165,7 @@ export const Component = () => {
|
|||||||
.getSession(workspaceId, sessionId)
|
.getSession(workspaceId, sessionId)
|
||||||
.then(session => {
|
.then(session => {
|
||||||
setCurrentSession(session);
|
setCurrentSession(session);
|
||||||
if (chatContent) {
|
reMountChatContent();
|
||||||
chatContent.remove();
|
|
||||||
setChatContent(null);
|
|
||||||
}
|
|
||||||
chatTool?.closeHistoryMenu();
|
chatTool?.closeHistoryMenu();
|
||||||
})
|
})
|
||||||
.catch(console.error)
|
.catch(console.error)
|
||||||
@@ -167,7 +173,7 @@ export const Component = () => {
|
|||||||
setIsOpeningSession(false);
|
setIsOpeningSession(false);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[chatContent, chatTool, client, isOpeningSession, workspaceId]
|
[chatTool, client, isOpeningSession, reMountChatContent, workspaceId]
|
||||||
);
|
);
|
||||||
|
|
||||||
const onContextChange = useCallback((context: Partial<ChatContextValue>) => {
|
const onContextChange = useCallback((context: Partial<ChatContextValue>) => {
|
||||||
@@ -246,7 +252,7 @@ export const Component = () => {
|
|||||||
|
|
||||||
// init or update header ai-chat-toolbar
|
// init or update header ai-chat-toolbar
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isHeaderProvided || !chatToolContainerRef.current || !chatContent) {
|
if (!isHeaderProvided || !chatToolContainerRef.current) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let tool = chatTool;
|
let tool = chatTool;
|
||||||
@@ -268,8 +274,7 @@ export const Component = () => {
|
|||||||
tool.onNewSession = () => {
|
tool.onNewSession = () => {
|
||||||
if (!currentSession) return;
|
if (!currentSession) return;
|
||||||
setCurrentSession(null);
|
setCurrentSession(null);
|
||||||
chatContent?.remove();
|
reMountChatContent();
|
||||||
setChatContent(null);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
tool.onTogglePin = async () => {
|
tool.onTogglePin = async () => {
|
||||||
@@ -291,7 +296,6 @@ export const Component = () => {
|
|||||||
setChatTool(tool);
|
setChatTool(tool);
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
chatContent,
|
|
||||||
chatTool,
|
chatTool,
|
||||||
currentSession,
|
currentSession,
|
||||||
docDisplayConfig,
|
docDisplayConfig,
|
||||||
@@ -302,6 +306,7 @@ export const Component = () => {
|
|||||||
confirmModal,
|
confirmModal,
|
||||||
framework,
|
framework,
|
||||||
status,
|
status,
|
||||||
|
reMountChatContent,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -322,8 +327,6 @@ export const Component = () => {
|
|||||||
|
|
||||||
// restore pinned session
|
// restore pinned session
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!chatContent) return;
|
|
||||||
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const signal = controller.signal;
|
const signal = controller.signal;
|
||||||
client
|
client
|
||||||
@@ -339,10 +342,7 @@ export const Component = () => {
|
|||||||
const session = sessions[0];
|
const session = sessions[0];
|
||||||
if (!session) return;
|
if (!session) return;
|
||||||
setCurrentSession(session);
|
setCurrentSession(session);
|
||||||
if (chatContent) {
|
reMountChatContent();
|
||||||
chatContent.remove();
|
|
||||||
setChatContent(null);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
|
|
||||||
@@ -350,7 +350,7 @@ export const Component = () => {
|
|||||||
return () => {
|
return () => {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
};
|
};
|
||||||
}, [chatContent, client, workspaceId]);
|
}, [client, reMountChatContent, workspaceId]);
|
||||||
|
|
||||||
const onChatContainerRef = useCallback((node: HTMLDivElement) => {
|
const onChatContainerRef = useCallback((node: HTMLDivElement) => {
|
||||||
if (node) {
|
if (node) {
|
||||||
|
|||||||
Reference in New Issue
Block a user