优化:缓存查询与智能分析页面,修复组织下钻和草稿保留
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { usePageCache } from './pageCache';
|
||||
import { FilterFieldSettings } from './FilterFieldSettings';
|
||||
import { isInternalDataField, publicJson } from './publicDataDisplay';
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
@@ -951,10 +952,12 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
const [renameTarget, setRenameTarget] = useState<Record<string, unknown> | null>(null);
|
||||
const [renameTitle, setRenameTitle] = useState('');
|
||||
const [renaming, setRenaming] = useState(false);
|
||||
const initialParams = new URLSearchParams(window.location.search);
|
||||
const { active, initialSearch, refreshKey, onLocationChange } = usePageCache();
|
||||
const activePage = useRef(active); activePage.current = active;
|
||||
const initialParams = new URLSearchParams(initialSearch ?? window.location.search);
|
||||
const initialSessionId = initialParams.get('sessionId') || '';
|
||||
const initialTurnId = initialParams.get('turnId') || '';
|
||||
const [initialLink] = useState(() => decodeAnalysisLinkContext(new URLSearchParams(window.location.search).get('context')));
|
||||
const [initialLink] = useState(() => decodeAnalysisLinkContext(initialParams.get('context')));
|
||||
const [query, setQuery] = useState('');
|
||||
const [sessionId, setSessionId] = useState(initialSessionId);
|
||||
const [sessionContext, setSessionContext] = useState<Record<string, unknown> | null>(null);
|
||||
@@ -979,14 +982,19 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
const conversation = useRef<HTMLDivElement>(null);
|
||||
const followOutput = useRef(true);
|
||||
const previousScrollTop = useRef(0);
|
||||
const readingPosition = useRef(0);
|
||||
useEffect(() => {
|
||||
const element = conversation.current;
|
||||
if (active && element) element.scrollTop = followOutput.current ? element.scrollHeight : readingPosition.current;
|
||||
}, [active]);
|
||||
useEffect(() => { followOutput.current = true; previousScrollTop.current = 0; }, [sessionId]);
|
||||
useEffect(() => {
|
||||
const element = conversation.current;
|
||||
if (element && followOutput.current) {
|
||||
if (active && element && followOutput.current) {
|
||||
element.scrollTop = element.scrollHeight;
|
||||
previousScrollTop.current = element.scrollTop;
|
||||
}
|
||||
}, [lastTurn, busy, turns, sessionId]);
|
||||
}, [lastTurn, busy, turns, sessionId, active]);
|
||||
|
||||
const loadSessions = async () => {
|
||||
try { const items = collectionItems(await api.analysisSessions()); if (mounted.current) setSessions(items); }
|
||||
@@ -1079,14 +1087,26 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
const writeAnalysisLocation = (selectedSessionId?: string, selectedTurnId?: string) => {
|
||||
if (!mounted.current) return;
|
||||
if (selectedSessionId) rememberAnalysisSession(user.id, selectedSessionId);
|
||||
if (!activePage.current) return;
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set('page', 'analysis');
|
||||
if (selectedSessionId) url.searchParams.set('sessionId', selectedSessionId); else url.searchParams.delete('sessionId');
|
||||
if (selectedTurnId) url.searchParams.set('turnId', selectedTurnId); else url.searchParams.delete('turnId');
|
||||
url.searchParams.delete('context');
|
||||
window.history.replaceState({}, '', `${url.pathname}${url.search}${url.hash}`);
|
||||
onLocationChange(url.search);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (active && !historyLoading) writeAnalysisLocation(sessionId || undefined, activeTurnId || undefined);
|
||||
}, [active, historyLoading, sessionId, activeTurnId]);
|
||||
const refreshSeen = useRef(refreshKey);
|
||||
useEffect(() => {
|
||||
if (!active || refreshSeen.current === refreshKey) return;
|
||||
refreshSeen.current = refreshKey; void loadSessions();
|
||||
if (sessionId && !busy) void openSession(sessionId);
|
||||
}, [active, refreshKey]);
|
||||
|
||||
const openSession = async (id: string, focusTurnId?: string): Promise<boolean> => {
|
||||
userSelectionMade.current = true;
|
||||
beginAnalysisSelection(user.id);
|
||||
@@ -1259,7 +1279,7 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
const settledTurn = settledAnalysisTurn(id, question, detail, streamedResult, terminalEventStatus);
|
||||
setLastTurn(settledTurn);
|
||||
if (ANALYSIS_FINAL_STATUSES.has(String(settledTurn.status || '').toUpperCase())) {
|
||||
setRetryQuestion(''); setQuery('');
|
||||
setRetryQuestion('');
|
||||
const parentSessionId = sessionOverride || sessionId;
|
||||
if (parentSessionId) {
|
||||
try {
|
||||
@@ -1438,10 +1458,12 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
<div className="analysis-conversation" ref={conversation} aria-live="polite" onWheel={event => {
|
||||
if (event.deltaY < 0 && event.currentTarget.scrollTop > 0) followOutput.current = false;
|
||||
}} onScroll={event => {
|
||||
if (!activePage.current) return;
|
||||
const element = event.currentTarget;
|
||||
followOutput.current = analysisScrollFollows(previousScrollTop.current, element.scrollTop,
|
||||
element.scrollHeight - element.clientHeight - element.scrollTop, followOutput.current);
|
||||
previousScrollTop.current = element.scrollTop;
|
||||
readingPosition.current = element.scrollTop;
|
||||
}}>
|
||||
{turns.map((turn, index) => <AnalysisTurn key={String(turn.id || index)} turn={turn} onRetry={(id) => void retryTurn(turns.find(item => String(item.id || '') === id) || turn)} onResume={id => { if (!historyLoading) void followTurn(id, String(turn.question || '')); }} onGenerateReport={() => void submit(generateReportPrompt(turn))} retryDisabled={busy || historyLoading} />)}
|
||||
{lastTurn && <AnalysisTurn turn={lastTurn} onRetry={() => void retryTurn(lastTurn)} onResume={id => { if (!historyLoading) void followTurn(id, String(lastTurn.question || '')); }} onGenerateReport={() => void submit(generateReportPrompt(lastTurn))} retryDisabled={busy || historyLoading} />}
|
||||
|
||||
Reference in New Issue
Block a user