From 5317406cff3620bcf5333696eb04637e18f5e0f2 Mon Sep 17 00:00:00 2001
From: vincentlu <t1341870251@gmail.com>
Date: 星期四, 19 三月 2026 14:39:26 +0800
Subject: [PATCH] #

---
 zy-acs-flow/src/map/NewsLogDialog.jsx |   75 ++++++++++++++++++++++++++++++++-----
 1 files changed, 64 insertions(+), 11 deletions(-)

diff --git a/zy-acs-flow/src/map/NewsLogDialog.jsx b/zy-acs-flow/src/map/NewsLogDialog.jsx
index 97a768d..a636920 100644
--- a/zy-acs-flow/src/map/NewsLogDialog.jsx
+++ b/zy-acs-flow/src/map/NewsLogDialog.jsx
@@ -30,7 +30,8 @@
 const MIN_HEIGHT = 240;
 const EDGE_MARGIN = 16;
 
-const MAX_VISIBLE_LOGS = 300;
+const MAX_VISIBLE_LOGS = 200;
+const MAX_BUFFER_LOGS = 600;
 
 const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
 
@@ -49,6 +50,9 @@
     const programmaticScrollRef = useRef(false);
     const initialScrollDoneRef = useRef(false);
     const userScrollRef = useRef(false);
+    const autoScrollRef = useRef(true);
+    const nearBottomRef = useRef(true);
+    const pendingLogsRef = useRef(null);
     const resizeRef = useRef({
         active: false,
         startX: 0,
@@ -75,17 +79,52 @@
         };
     }, [getSizeLimits]);
 
+    const applyLogs = useCallback((payload, { programmatic = false } = {}) => {
+        if (!payload) {
+            return;
+        }
+        programmaticScrollRef.current = programmatic;
+        pendingLogsRef.current = null;
+        setLogs(payload.logs);
+        setLastUpdated(payload.timestamp);
+    }, []);
+
+    const flushPendingLogs = useCallback((options = {}) => {
+        if (!pendingLogsRef.current) {
+            return false;
+        }
+        applyLogs(pendingLogsRef.current, options);
+        return true;
+    }, [applyLogs]);
+
+    const computeNearBottom = useCallback(() => {
+        const container = scrollRef.current;
+        if (!container) {
+            return true;
+        }
+        const { scrollTop, scrollHeight, clientHeight } = container;
+        return scrollHeight - (scrollTop + clientHeight) < 32;
+    }, []);
+
     const fetchLogs = useCallback(async () => {
         const data = await Http.fetchNewsLogs();
         if (Array.isArray(data)) {
-            const trimmedLogs = data.length > MAX_VISIBLE_LOGS
-                ? data.slice(data.length - MAX_VISIBLE_LOGS)
-                : data;
-            programmaticScrollRef.current = true;
-            setLogs(trimmedLogs);
-            setLastUpdated(new Date());
+            let nextLogs = data;
+            if (data.length > MAX_VISIBLE_LOGS) {
+                const shouldStickToBottom = autoScrollRef.current && nearBottomRef.current;
+                const limit = shouldStickToBottom
+                    ? MAX_VISIBLE_LOGS
+                    : Math.min(MAX_BUFFER_LOGS, data.length);
+                nextLogs = data.slice(data.length - limit);
+            }
+            const payload = { logs: nextLogs, timestamp: new Date() };
+            if (autoScrollRef.current && nearBottomRef.current) {
+                applyLogs(payload, { programmatic: true });
+            } else {
+                pendingLogsRef.current = payload;
+            }
         }
-    }, []);
+    }, [applyLogs]);
 
     useEffect(() => {
         if (!open) {
@@ -96,6 +135,8 @@
             setPosition(null);
             setSize(null);
             initialScrollDoneRef.current = false;
+            nearBottomRef.current = true;
+            pendingLogsRef.current = null;
             return;
         }
         setSize(buildInitialSize());
@@ -103,6 +144,8 @@
         userScrollRef.current = false;
         programmaticScrollRef.current = true;
         initialScrollDoneRef.current = false;
+        nearBottomRef.current = true;
+        pendingLogsRef.current = null;
         fetchLogs();
         pollingRef.current = setInterval(fetchLogs, POLLING_INTERVAL);
         return () => {
@@ -116,6 +159,7 @@
             return;
         }
         programmaticScrollRef.current = true;
+        nearBottomRef.current = true;
         const step = (remaining) => {
             if (!scrollRef.current) {
                 return;
@@ -144,19 +188,25 @@
         initialScrollDoneRef.current = true;
     }, [logs, autoScroll, scrollToBottom]);
 
+    useEffect(() => {
+        autoScrollRef.current = autoScroll;
+    }, [autoScroll]);
+
     const handleScroll = useCallback(() => {
         if (!scrollRef.current) {
             return;
         }
         if (programmaticScrollRef.current) {
             programmaticScrollRef.current = false;
+            nearBottomRef.current = computeNearBottom();
             return;
         }
+        const isNearBottom = computeNearBottom();
+        nearBottomRef.current = isNearBottom;
         if (!autoScroll) {
+            userScrollRef.current = !isNearBottom;
             return;
         }
-        const { scrollTop, scrollHeight, clientHeight } = scrollRef.current;
-        const isNearBottom = scrollHeight - (scrollTop + clientHeight) < 32;
         if (isNearBottom) {
             if (userScrollRef.current) {
                 userScrollRef.current = false;
@@ -164,9 +214,10 @@
         } else {
             userScrollRef.current = true;
         }
-    }, [autoScroll]);
+    }, [autoScroll, computeNearBottom]);
 
     const handleJumpLatest = () => {
+        flushPendingLogs({ programmatic: true });
         userScrollRef.current = false;
         setAutoScroll(true);
         scrollToBottom('smooth', 2);
@@ -181,8 +232,10 @@
         const { checked } = event.target;
         setAutoScroll(checked);
         if (checked) {
+            flushPendingLogs({ programmatic: true });
             userScrollRef.current = false;
             initialScrollDoneRef.current = false;
+            nearBottomRef.current = true;
             scrollToBottom('auto', 5);
         } else {
             userScrollRef.current = true;

--
Gitblit v1.9.1