137 lines
4.8 KiB
JavaScript
137 lines
4.8 KiB
JavaScript
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||
|
||
/**
|
||
* useChatScroll —— 在 wrapper 内查找 Semi Chat 的滚动容器,跟踪是否在底部。
|
||
*
|
||
* 为什么不用 ScrollableContainer?
|
||
* Semi Chat 自带的滚动容器是组件内部 DOM,我们没法把它包到 ScrollableContainer 里。
|
||
* 只能拿外层 wrapperRef,向下 querySelector 找到那个 scrollable 的 inner 元素,再挂监听。
|
||
*
|
||
* 返回:
|
||
* isAtBottom (boolean) — 用户当前是否贴在底部(含 80px 容差,避免抖动)
|
||
* scrollToBottom() — 平滑滚到底部
|
||
*/
|
||
const NEAR_BOTTOM_PX = 80;
|
||
|
||
const findScrollable = (rootEl) => {
|
||
if (!rootEl) return null;
|
||
try {
|
||
const direct = rootEl.querySelector('[class*="ui-chat-container"]');
|
||
if (direct && direct.scrollHeight > direct.clientHeight) return direct;
|
||
if (direct) {
|
||
const inner = Array.from(direct.querySelectorAll('*')).find((el) => {
|
||
const cs = window.getComputedStyle(el);
|
||
return (
|
||
(cs.overflowY === 'auto' || cs.overflowY === 'scroll') &&
|
||
el.scrollHeight > el.clientHeight
|
||
);
|
||
});
|
||
if (inner) return inner;
|
||
}
|
||
} catch (_) {
|
||
// ignore
|
||
}
|
||
return null;
|
||
};
|
||
|
||
export const useChatScroll = (wrapperRef, deps = []) => {
|
||
const [isAtBottom, setIsAtBottom] = useState(true);
|
||
const containerRef = useRef(null);
|
||
const observerRef = useRef(null);
|
||
// 缓存当前的 deps 字符串,方便重定位 scroll 容器 / 重检 isAtBottom
|
||
const depsKey = deps
|
||
.map((d) => (typeof d === 'object' ? '' : String(d)))
|
||
.join('|');
|
||
|
||
const checkPosition = useCallback(() => {
|
||
const el = containerRef.current;
|
||
if (!el) return;
|
||
const fromBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
|
||
setIsAtBottom(fromBottom <= NEAR_BOTTOM_PX);
|
||
}, []);
|
||
|
||
// 找滚动容器 + 装监听;容器可能在 mount 后才出现(Semi Chat 异步渲染),用 MutationObserver 兜底
|
||
useEffect(() => {
|
||
const root = wrapperRef.current;
|
||
if (!root) return undefined;
|
||
|
||
const attach = () => {
|
||
const el = findScrollable(root);
|
||
if (!el || el === containerRef.current) return;
|
||
// 旧的卸载
|
||
if (containerRef.current) {
|
||
containerRef.current.removeEventListener('scroll', checkPosition);
|
||
}
|
||
containerRef.current = el;
|
||
el.addEventListener('scroll', checkPosition, { passive: true });
|
||
checkPosition();
|
||
};
|
||
|
||
attach();
|
||
|
||
if (!observerRef.current) {
|
||
observerRef.current = new MutationObserver(() => {
|
||
// Chat 组件改 DOM 时只尝试重附加 listener;不在这里调 checkPosition ——
|
||
// 流式响应下子树每秒会有几十次 mutation,每次都读 scrollHeight/scrollTop
|
||
// 会触发 layout reflow,慢设备明显卡。scroll 事件 + deps 变化的 effect
|
||
// 已经足以维护 isAtBottom 的正确性。
|
||
attach();
|
||
});
|
||
observerRef.current.observe(root, { childList: true, subtree: true });
|
||
}
|
||
|
||
return () => {
|
||
if (containerRef.current) {
|
||
containerRef.current.removeEventListener('scroll', checkPosition);
|
||
}
|
||
if (observerRef.current) {
|
||
observerRef.current.disconnect();
|
||
observerRef.current = null;
|
||
}
|
||
containerRef.current = null;
|
||
};
|
||
// wrapperRef 是 ref,不放进 deps;其它依赖通过 depsKey 收敛
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
}, [depsKey]);
|
||
|
||
// deps 改变时(消息列表变了/会话切了)也重测位置
|
||
useEffect(() => {
|
||
// 等下一帧,让 Chat 把新内容渲染完再判断
|
||
const id = requestAnimationFrame(checkPosition);
|
||
return () => cancelAnimationFrame(id);
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
}, [depsKey]);
|
||
|
||
const scrollToBottom = useCallback(
|
||
(opts = { behavior: 'smooth' }) => {
|
||
const el = containerRef.current || findScrollable(wrapperRef.current);
|
||
if (!el) return;
|
||
el.scrollTo({ top: el.scrollHeight, ...opts });
|
||
},
|
||
[wrapperRef],
|
||
);
|
||
|
||
return { isAtBottom, scrollToBottom };
|
||
};
|
||
|
||
export default useChatScroll;
|