refactor(storage): use unstorage for session state

This commit is contained in:
2026-07-01 18:36:26 +08:00
parent 0251916a8a
commit ae6578923b
13 changed files with 398 additions and 246 deletions
+37 -23
View File
@@ -81,35 +81,49 @@ export function ChatArea({
if (restoredScrollRef.current || messages.length === 0) return;
const scrollNode = scrollRef.current;
const snapshot = consumeChatScrollSnapshot();
if (!scrollNode || snapshot === null) return;
if (!scrollNode) return;
restoredScrollRef.current = true;
let cancelled = false;
let frame: number | null = null;
let timer: number | null = null;
let observerTimer: number | null = null;
let lateRestoreTimer: number | null = null;
let resizeObserver: ResizeObserver | null = null;
const restoreScroll = () => {
scrollNode.scrollTop = getChatScrollRestoreTop(scrollNode, snapshot);
const restore = async () => {
const snapshot = await consumeChatScrollSnapshot();
if (cancelled || snapshot === null) return;
restoredScrollRef.current = true;
const restoreScroll = () => {
scrollNode.scrollTop = getChatScrollRestoreTop(scrollNode, snapshot);
};
restoreScroll();
frame = window.requestAnimationFrame(restoreScroll);
timer = window.setTimeout(restoreScroll, 120);
resizeObserver =
typeof ResizeObserver === "undefined"
? null
: new ResizeObserver(restoreScroll);
Array.from(scrollNode.children).forEach((child) => {
resizeObserver?.observe(child);
});
observerTimer = window.setTimeout(() => {
resizeObserver?.disconnect();
}, 800);
lateRestoreTimer = window.setTimeout(restoreScroll, 600);
};
restoreScroll();
const frame = window.requestAnimationFrame(restoreScroll);
const timer = window.setTimeout(restoreScroll, 120);
const resizeObserver =
typeof ResizeObserver === "undefined"
? null
: new ResizeObserver(restoreScroll);
Array.from(scrollNode.children).forEach((child) => {
resizeObserver?.observe(child);
});
const observerTimer = window.setTimeout(() => {
resizeObserver?.disconnect();
}, 800);
const lateRestoreTimer = window.setTimeout(restoreScroll, 600);
void restore();
return () => {
window.cancelAnimationFrame(frame);
window.clearTimeout(timer);
window.clearTimeout(observerTimer);
window.clearTimeout(lateRestoreTimer);
cancelled = true;
if (frame !== null) window.cancelAnimationFrame(frame);
if (timer !== null) window.clearTimeout(timer);
if (observerTimer !== null) window.clearTimeout(observerTimer);
if (lateRestoreTimer !== null) window.clearTimeout(lateRestoreTimer);
resizeObserver?.disconnect();
};
}, [messages.length]);