feat: only autoscroll Continue console when focused on last element

This commit is contained in:
Fred Bricon 2025-05-16 14:24:50 +02:00
parent 03d921ec13
commit 40390f583b

View file

@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useEffect, useRef, useState } from "react";
import { LLMInteraction } from "../../hooks/useLLMLog";
import useLLMSummary from "../../hooks/useLLMSummary";
import End from "./End";
@ -50,6 +50,18 @@ export default function Details({ interaction }: DetailsProps) {
const scrollTop = useRef<HTMLDivElement>(null);
const lastResult = useRef<any>(null);
const summary = useLLMSummary(interaction);
const [isAtBottom, setIsAtBottom] = useState(true);
// Handler to check if user is at the bottom
function handleScroll() {
const elt = scrollTop.current;
if (!elt) {
return;
}
// Allow a small threshold for floating point errors
const threshold = 5;
setIsAtBottom((elt.scrollHeight - elt.scrollTop - elt.clientHeight) < threshold);
}
useEffect(() => {
if (interaction.end) {
@ -59,15 +71,17 @@ export default function Details({ interaction }: DetailsProps) {
const last = interaction.results[interaction.results.length - 1];
if (last != lastResult.current) {
lastResult.current = last;
const lastChild = scrollTop.current?.lastChild;
if (lastChild) {
(lastChild as HTMLElement).scrollIntoView({
behavior: "auto",
block: "end",
});
if (isAtBottom) {
const lastChild = scrollTop.current?.lastChild;
if (lastChild) {
(lastChild as HTMLElement).scrollIntoView({
behavior: "auto",
block: "end",
});
}
}
}
});
}, [interaction, isAtBottom]);
return (
<div className="m-0 flex min-w-0 flex-1 shrink grow flex-col">
@ -102,7 +116,11 @@ export default function Details({ interaction }: DetailsProps) {
></Cell>
</div>
</div>
<div ref={scrollTop} className="grow overflow-auto">
<div
ref={scrollTop}
className="grow overflow-auto"
onScroll={handleScroll}
>
{interaction.start ? <Start item={interaction.start}></Start> : ""}
<div className="whitespace-pre-wrap p-2">
{interaction.results.map((group, i) => {