fix(chat): preserve agent UI across close

This commit is contained in:
2026-07-10 14:38:25 +08:00
parent adb53d9a13
commit c6e6e24aab
2 changed files with 148 additions and 24 deletions
+24 -24
View File
@@ -42,7 +42,7 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
const isNearBottomRef = useRef(true);
const streamingScrollFrameRef = useRef<number | null>(null);
const composerRef = useRef<AgentComposerHandle | null>(null);
const hasResetForOpenRef = useRef(false);
const initializedProjectIdRef = useRef<string | null | undefined>(undefined);
const theme = useTheme();
const { open: openNotification } = useNotification();
const currentProjectId = useProjectStore((state) => state.currentProjectId);
@@ -154,6 +154,17 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
isNearBottomRef.current = isNearBottom;
}, []);
const resetConversationView = useCallback(() => {
composerRef.current?.clear();
setIsHistoryOpen(false);
window.setTimeout(() => {
composerRef.current?.focus();
isNearBottomRef.current = true;
cancelStreamingScroll();
scrollToBottom("auto");
}, 0);
}, [cancelStreamingScroll, scrollToBottom]);
useEffect(() => {
if (isStreaming) {
if (!isNearBottomRef.current) return;
@@ -178,24 +189,18 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
);
useEffect(() => {
if (!open) {
hasResetForOpenRef.current = false;
if (
!open ||
isHydrating ||
initializedProjectIdRef.current === currentProjectId
) {
return;
}
if (hasResetForOpenRef.current || isHydrating) return;
hasResetForOpenRef.current = true;
const timer = window.setTimeout(() => {
createSession();
composerRef.current?.clear();
setIsHistoryOpen(false);
composerRef.current?.focus();
isNearBottomRef.current = true;
cancelStreamingScroll();
scrollToBottom("auto");
}, 0);
return () => window.clearTimeout(timer);
}, [cancelStreamingScroll, createSession, isHydrating, open, scrollToBottom]);
initializedProjectIdRef.current = currentProjectId;
createSession();
resetConversationView();
}, [createSession, currentProjectId, isHydrating, open, resetConversationView]);
const handleSend = useCallback(async (prompt: string) => {
if (isStreaming || isCheckingAuth) return;
@@ -230,14 +235,8 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
handleStopSpeech();
stopListening();
createSession();
composerRef.current?.clear();
window.setTimeout(() => {
composerRef.current?.focus();
isNearBottomRef.current = true;
cancelStreamingScroll();
scrollToBottom("auto");
}, 0);
}, [cancelStreamingScroll, createSession, handleStopSpeech, scrollToBottom, stopListening]);
resetConversationView();
}, [createSession, handleStopSpeech, resetConversationView, stopListening]);
const handleHistoryToggle = useCallback(() => {
setIsHistoryOpen((prev) => !prev);
@@ -311,6 +310,7 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
hideBackdrop
disableScrollLock
disableEnforceFocus
ModalProps={{ keepMounted: true }}
sx={{
zIndex: (muiTheme) => muiTheme.zIndex.modal + 100,
pointerEvents: "none",