Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[Metabot] Better scrolling on historical convo load (#79734)

Sloan Spargercb5adad43bac
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.tsx
index b2a99b24fc9..9bab9b47c3d 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.tsx
@@ -56,7 +56,10 @@ export const MetabotChat = ({
const hasMessages = metabot.messages.length > 0;
- const { scrollContainerRef, fillerRef } = useScrollManager(hasMessages);
+ const { scrollContainerRef, fillerRef } = useScrollManager(
+ hasMessages,
+ metabot.conversationId,
+ );
const suggestedPromptsReq = useGetSuggestedMetabotPromptsQuery(
{
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/hooks.ts b/frontend/src/metabase/metabot/components/MetabotChat/hooks.ts
index abdf22c79e0..be0321e0052 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/hooks.ts
+++ b/frontend/src/metabase/metabot/components/MetabotChat/hooks.ts
@@ -1,4 +1,4 @@
-import { type RefObject, useCallback, useEffect, useRef } from "react";
+import { type RefObject, useCallback, useLayoutEffect, useRef } from "react";
export function calculateFillerHeight(
scrollContainerEl: HTMLElement,
@@ -47,29 +47,13 @@ function resizeFillerArea(
}
}
-export function useScrollManager(hasMessages: boolean) {
+export function useScrollManager(hasMessages: boolean, conversationId: string) {
const scrollContainerRef = useRef<HTMLDivElement>(null);
const fillerRef = useRef<HTMLDivElement>(null);
- const scrollToBottomRafRef = useRef<number>();
const resizeRafRef = useRef<number>();
const scrollToPromptRafRef = useRef<number>();
- // clean animations on clean up if needed
- useEffect(function cancelAnimations() {
- return () => {
- if (scrollToBottomRafRef.current) {
- cancelAnimationFrame(scrollToBottomRafRef.current);
- }
- if (resizeRafRef.current) {
- cancelAnimationFrame(resizeRafRef.current);
- }
- if (scrollToPromptRafRef.current) {
- cancelAnimationFrame(scrollToPromptRafRef.current);
- }
- };
- }, []);
-
const scheduleFillerResize = useCallback(() => {
// only schedule if nothing has been for this frame
if (!resizeRafRef.current) {
@@ -80,7 +64,7 @@ export function useScrollManager(hasMessages: boolean) {
}
}, []);
- useEffect(
+ useLayoutEffect(
function resizeFiller() {
const scrollContainerEl = scrollContainerRef.current;
const fillerEl = fillerRef.current;
@@ -88,15 +72,8 @@ export function useScrollManager(hasMessages: boolean) {
return;
}
- // resize filler + scroll to the absolute bottom on mount
- scheduleFillerResize();
- if (scrollContainerEl) {
- scrollToBottomRafRef.current = requestAnimationFrame(() => {
- // put in a RAF so it happens after filler resize
- scrollContainerEl.scrollTop = scrollContainerEl.scrollHeight;
- scrollToBottomRafRef.current = undefined;
- });
- }
+ resizeFillerArea(scrollContainerRef, fillerRef);
+ scrollContainerEl.scrollTop = scrollContainerEl.scrollHeight;
// react to content updates
const mutationObserver = new MutationObserver((mutations) => {
@@ -152,9 +129,17 @@ export function useScrollManager(hasMessages: boolean) {
return () => {
mutationObserver.disconnect();
resizeObserver.disconnect();
+ if (resizeRafRef.current !== undefined) {
+ cancelAnimationFrame(resizeRafRef.current);
+ resizeRafRef.current = undefined;
+ }
+ if (scrollToPromptRafRef.current !== undefined) {
+ cancelAnimationFrame(scrollToPromptRafRef.current);
+ scrollToPromptRafRef.current = undefined;
+ }
};
},
- [hasMessages, scheduleFillerResize],
+ [conversationId, hasMessages, scheduleFillerResize],
);
return {
diff --git a/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx b/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx
index 8558788bf79..3c37fab3e90 100644
--- a/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx
@@ -515,6 +515,10 @@ describe("metabot > ui", () => {
});
describe("conversation history", () => {
+ afterEach(() => {
+ jest.restoreAllMocks();
+ });
+
it("lists past conversations when opened, falling back to a placeholder for untitled ones", async () => {
setup({
conversations: [
@@ -591,14 +595,6 @@ describe("metabot > ui", () => {
const PAST_CONVERSATION_ID = "11111111-1111-1111-1111-111111111111";
const setupWithPastConversation = () => {
- setup({
- conversations: [
- createMockMetabotConversation({
- conversation_id: PAST_CONVERSATION_ID,
- title: "Orders by month",
- }),
- ],
- });
setupGetMetabotConversationEndpoint(
createMockMetabotConversationDetail({
conversation_id: PAST_CONVERSATION_ID,
@@ -619,6 +615,14 @@ describe("metabot > ui", () => {
],
}),
);
+ return setup({
+ conversations: [
+ createMockMetabotConversation({
+ conversation_id: PAST_CONVERSATION_ID,
+ title: "Orders by month",
+ }),
+ ],
+ });
};
const selectPastConversation = async () => {
@@ -651,6 +655,41 @@ describe("metabot > ui", () => {
});
});
+ it("positions a loaded conversation before the next frame", async () => {
+ jest.spyOn(window, "requestAnimationFrame").mockReturnValue(1);
+ jest
+ .spyOn(HTMLElement.prototype, "scrollHeight", "get")
+ .mockReturnValue(800);
+ const { store } = setupWithPastConversation();
+ act(() => {
+ store.dispatch(
+ metabotActions.setConversationSnapshot({
+ agentId: "omnibot",
+ conversationId: "current-conversation",
+ messages: [
+ {
+ id: "current-user",
+ role: "user",
+ type: "text",
+ message: "Current question",
+ },
+ ],
+ activeToolCalls: [],
+ }),
+ );
+ });
+ const previousMessages = await screen.findByTestId(
+ "metabot-chat-messages",
+ );
+ previousMessages.scrollTop = 100;
+
+ await selectPastConversation();
+ await screen.findByText("There are 42 orders.");
+
+ const messages = screen.getByTestId("metabot-chat-messages");
+ expect(messages.scrollTop).toBe(messages.scrollHeight);
+ });
+
it("continues the loaded conversation when a new message is submitted", async () => {
setupWithPastConversation();
const agentSpy = mockAgentEndpoint({