Skip to content

Commit db4fc96

Browse files
fix: log scroll follow
1 parent daa0406 commit db4fc96

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/components/log-list.tsx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import {
1212
} from "@glideapps/glide-data-grid";
1313
import { format } from "date-fns";
1414
import { useSetAtom } from "jotai";
15-
import { useCallback, useEffect, useRef, useState } from "react";
15+
import {
16+
useCallback,
17+
useEffect,
18+
useLayoutEffect,
19+
useRef,
20+
useState,
21+
} from "react";
1622
import { useThemeStyle } from "@/lib/hooks/useThemeStyle";
1723
import { type LogEntry, LogLevel } from "@/lib/native/game-process";
1824
import type { RunningGame } from "@/store/running-games";
@@ -134,6 +140,8 @@ type Props = {
134140
};
135141

136142
export function LogList({ runningGame, levelFilter, classFilter }: Props) {
143+
"use no memo";
144+
137145
const isDark = useThemeStyle() === "dark";
138146
const setLogCallback = useSetAtom(runningGame.log.atomCallback);
139147

@@ -155,11 +163,6 @@ export function LogList({ runningGame, levelFilter, classFilter }: Props) {
155163
.then((log) => {
156164
setRowData(log);
157165
setRowCount(log.length);
158-
setTimeout(() => {
159-
if (isScrollFollowing.current) {
160-
dataGridRef.current?.scrollTo(0, log.length - 1);
161-
}
162-
}, 1);
163166
});
164167
}, [runningGame, levelFilter, classFilter]);
165168

@@ -173,9 +176,6 @@ export function LogList({ runningGame, levelFilter, classFilter }: Props) {
173176
}
174177
rowData.push(log);
175178
setRowCount(rowData.length);
176-
if (isScrollFollowing.current) {
177-
dataGridRef.current?.scrollTo(0, rowData.length - 1);
178-
}
179179
};
180180
setLogCallback((prev) => [...prev, c]);
181181
return () => {
@@ -189,6 +189,21 @@ export function LogList({ runningGame, levelFilter, classFilter }: Props) {
189189
};
190190
}, [rowData]);
191191

192+
useLayoutEffect(() => {
193+
if (isScrollFollowing.current) {
194+
dataGridRef.current?.scrollTo(
195+
0,
196+
rowCount - 1,
197+
"vertical",
198+
undefined,
199+
undefined,
200+
{
201+
vAlign: "end",
202+
},
203+
);
204+
}
205+
}, [rowCount]);
206+
192207
const getCellContent = useCallback(
193208
(cell: Item): GridCell => {
194209
const [col, row] = cell;
@@ -291,7 +306,7 @@ export function LogList({ runningGame, levelFilter, classFilter }: Props) {
291306
(range: Rectangle) => {
292307
if (isScrollFollowing.current) {
293308
const prev = prevVisibleRegion.current;
294-
if (prev && prev.y > range.y) {
309+
if (prev && prev.y + prev.height > range.y + range.height) {
295310
isScrollFollowing.current = false;
296311
}
297312
} else if (range.y + range.height >= rowCount) {

0 commit comments

Comments
 (0)