diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 4b4b1e52a9..390f5abafa 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -224,6 +224,10 @@ import { type SidebarProjectSnapshot, } from "../sidebarProjectGrouping"; import { SidebarProviderUpdatePill } from "./sidebar/SidebarProviderUpdatePill"; +import { + type SidebarThreadListAutoAnimateRef, + useSidebarThreadListAutoAnimateRef, +} from "./sidebar/useSidebarThreadListAutoAnimate"; const SIDEBAR_SORT_LABELS: Record = { updated_at: "Last user message", created_at: "Created at", @@ -913,7 +917,7 @@ interface SidebarProjectThreadListProps { confirmingArchiveThreadKey: string | null; setConfirmingArchiveThreadKey: React.Dispatch>; confirmArchiveButtonRefs: React.RefObject>; - attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void; + attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef; handleThreadClick: ( event: React.MouseEvent, threadRef: ScopedThreadRef, @@ -982,7 +986,9 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList( return ( { + attachThreadListAutoAnimateRef(node, renderedThreads.length); + }} className="mx-0.5 my-0 w-full translate-x-0 gap-0.5 overflow-hidden px-1 py-0 sm:mx-1 sm:px-1.5" > {shouldShowThreadPanel && showEmptyThreadState ? ( @@ -1075,7 +1081,7 @@ interface SidebarProjectItemProps { archiveThread: ReturnType["archiveThread"]; deleteThread: ReturnType["deleteThread"]; threadJumpLabelByKey: ReadonlyMap; - attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void; + attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef; expandThreadListForProject: (projectKey: string) => void; collapseThreadListForProject: (projectKey: string) => void; dragInProgressRef: React.RefObject; @@ -2921,7 +2927,7 @@ interface SidebarProjectsContentProps { newThreadShortcutLabel: string | null; commandPaletteShortcutLabel: string | null; threadJumpLabelByKey: ReadonlyMap; - attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void; + attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef; expandThreadListForProject: (projectKey: string) => void; collapseThreadListForProject: (projectKey: string) => void; dragInProgressRef: React.RefObject; @@ -3419,14 +3425,7 @@ export default function Sidebar() { animatedProjectListsRef.current.add(node); }, []); - const animatedThreadListsRef = useRef(new WeakSet()); - const attachThreadListAutoAnimateRef = useCallback((node: HTMLElement | null) => { - if (!node || animatedThreadListsRef.current.has(node)) { - return; - } - autoAnimate(node, SIDEBAR_LIST_ANIMATION_OPTIONS); - animatedThreadListsRef.current.add(node); - }, []); + const attachThreadListAutoAnimateRef = useSidebarThreadListAutoAnimateRef(); const visibleThreads = useMemo( () => sidebarThreads.filter((thread) => thread.archivedAt === null), diff --git a/apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.test.tsx b/apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.test.tsx new file mode 100644 index 0000000000..faed521f2d --- /dev/null +++ b/apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.test.tsx @@ -0,0 +1,90 @@ +import type { AnimationController } from "@formkit/auto-animate"; +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +const testState = vi.hoisted(() => ({ + autoAnimate: vi.fn(), +})); + +vi.mock("react", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + useCallback: (callback: T): T => callback, + useRef: (initialValue: T): { current: T } => ({ current: initialValue }), + }; +}); + +vi.mock("@formkit/auto-animate", () => ({ + autoAnimate: testState.autoAnimate, +})); + +import { + MAX_ANIMATED_SIDEBAR_THREAD_ROWS, + shouldAnimateSidebarThreadList, + useSidebarThreadListAutoAnimateRef, +} from "./useSidebarThreadListAutoAnimate"; + +function makeController(): AnimationController { + return { + parent: {} as Element, + enable: vi.fn(), + disable: vi.fn(), + isEnabled: vi.fn(() => true), + destroy: vi.fn(), + }; +} + +describe("useSidebarThreadListAutoAnimateRef", () => { + beforeEach(() => { + testState.autoAnimate.mockReset(); + }); + + it("keeps animation enabled for ordinary sidebar thread lists", () => { + const controller = makeController(); + testState.autoAnimate.mockReturnValue(controller); + const attach = useSidebarThreadListAutoAnimateRef(); + const node = {} as HTMLElement; + + attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS); + + expect(testState.autoAnimate).toHaveBeenCalledOnce(); + expect(controller.enable).toHaveBeenCalledOnce(); + expect(controller.disable).not.toHaveBeenCalled(); + }); + + it("bypasses animations when the list grows beyond the threshold", () => { + const controller = makeController(); + testState.autoAnimate.mockReturnValue(controller); + const attach = useSidebarThreadListAutoAnimateRef(); + const node = {} as HTMLElement; + + attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS); + attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1); + + expect(testState.autoAnimate).toHaveBeenCalledOnce(); + expect(controller.disable).toHaveBeenCalledOnce(); + expect(controller.enable).toHaveBeenCalledOnce(); + }); + + it("keeps animation disabled through a bulk removal before restoring it", async () => { + const controller = makeController(); + testState.autoAnimate.mockReturnValue(controller); + const attach = useSidebarThreadListAutoAnimateRef(); + const node = {} as HTMLElement; + + attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1); + attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS - 1); + + expect(controller.disable).toHaveBeenCalledTimes(2); + expect(controller.enable).not.toHaveBeenCalled(); + + await Promise.resolve(); + + expect(controller.enable).toHaveBeenCalledOnce(); + }); + + it("uses the threshold as the exact animation boundary", () => { + expect(shouldAnimateSidebarThreadList(MAX_ANIMATED_SIDEBAR_THREAD_ROWS)).toBe(true); + expect(shouldAnimateSidebarThreadList(MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1)).toBe(false); + }); +}); diff --git a/apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.ts b/apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.ts new file mode 100644 index 0000000000..c9731f2b4b --- /dev/null +++ b/apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.ts @@ -0,0 +1,70 @@ +import { + autoAnimate, + type AnimationController, + type AutoAnimateOptions, +} from "@formkit/auto-animate"; +import { useCallback, useRef } from "react"; + +export const MAX_ANIMATED_SIDEBAR_THREAD_ROWS = 20; + +const SIDEBAR_THREAD_LIST_ANIMATION_OPTIONS = { + duration: 180, + easing: "ease-out", +} as const satisfies Partial; + +export function shouldAnimateSidebarThreadList(visibleRowCount: number): boolean { + return visibleRowCount <= MAX_ANIMATED_SIDEBAR_THREAD_ROWS; +} + +export type SidebarThreadListAutoAnimateRef = ( + node: HTMLElement | null, + visibleRowCount: number, +) => void; + +interface SidebarThreadListAnimationState { + controller: AnimationController; + visibleRowCount: number; +} + +export function useSidebarThreadListAutoAnimateRef(): SidebarThreadListAutoAnimateRef { + const animationStateRef = useRef(new WeakMap()); + + return useCallback((node: HTMLElement | null, visibleRowCount: number) => { + if (!node) return; + + let animationState = animationStateRef.current.get(node); + if (!animationState) { + animationState = { + controller: autoAnimate(node, SIDEBAR_THREAD_LIST_ANIMATION_OPTIONS), + visibleRowCount, + }; + animationStateRef.current.set(node, animationState); + } + + const previousRowCount = animationState.visibleRowCount; + animationState.visibleRowCount = visibleRowCount; + if ( + shouldAnimateSidebarThreadList(previousRowCount) && + shouldAnimateSidebarThreadList(visibleRowCount) + ) { + animationState.controller.enable(); + return; + } + + animationState.controller.disable(); + if (!shouldAnimateSidebarThreadList(visibleRowCount)) return; + + // Keep the controller disabled until the MutationObserver has processed the bulk removal, + // then restore normal animation behavior for later changes to the now-small list. + const stateForEnable = animationState; + queueMicrotask(() => { + const currentState = animationStateRef.current.get(node); + if ( + currentState === stateForEnable && + shouldAnimateSidebarThreadList(currentState.visibleRowCount) + ) { + currentState.controller.enable(); + } + }); + }, []); +}