Compare commits

...
Author SHA1 Message Date
Julius Marmingeandcodex b7789116f7 Fix large sidebar collapse performance (Issue #3962)
Co-authored-by: codex <[email protected]>
2026-07-20 17:27:42 +02:00
3 changed files with 171 additions and 12 deletions

No files matched your search

+11 -12
View File
@@ -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<SidebarProjectSortOrder, string> = {
updated_at: "Last user message",
created_at: "Created at",
@@ -913,7 +917,7 @@ interface SidebarProjectThreadListProps {
confirmingArchiveThreadKey: string | null;
setConfirmingArchiveThreadKey: React.Dispatch<React.SetStateAction<string | null>>;
confirmArchiveButtonRefs: React.RefObject<Map<string, HTMLButtonElement>>;
attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
handleThreadClick: (
event: React.MouseEvent,
threadRef: ScopedThreadRef,
@@ -982,7 +986,9 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList(
return (
<SidebarMenuSub
ref={attachThreadListAutoAnimateRef}
ref={(node) => {
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<typeof useThreadActions>["archiveThread"];
deleteThread: ReturnType<typeof useThreadActions>["deleteThread"];
threadJumpLabelByKey: ReadonlyMap<string, string>;
attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
expandThreadListForProject: (projectKey: string) => void;
collapseThreadListForProject: (projectKey: string) => void;
dragInProgressRef: React.RefObject<boolean>;
@@ -2921,7 +2927,7 @@ interface SidebarProjectsContentProps {
newThreadShortcutLabel: string | null;
commandPaletteShortcutLabel: string | null;
threadJumpLabelByKey: ReadonlyMap<string, string>;
attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
expandThreadListForProject: (projectKey: string) => void;
collapseThreadListForProject: (projectKey: string) => void;
dragInProgressRef: React.RefObject<boolean>;
@@ -3419,14 +3425,7 @@ export default function Sidebar() {
animatedProjectListsRef.current.add(node);
}, []);
const animatedThreadListsRef = useRef(new WeakSet<HTMLElement>());
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),
@@ -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<typeof import("react")>();
return {
...actual,
useCallback: <T,>(callback: T): T => callback,
useRef: <T,>(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);
});
});
@@ -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<AutoAnimateOptions>;
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<HTMLElement, SidebarThreadListAnimationState>());
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();
}
});
}, []);
}