History

Fix large sidebar collapse performance (Issue #3962)

Julius Marminge committed 3 months ago

Commit b7789111 parent 5d34f9f3 files · +171 −12Browse files

Unified · Split

3 files
  1. apps/web/src/components/Sidebar.tsx +11 −12
  2. apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.test.tsx +90 −0
  3. apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.ts +70 −0
apps/web/src/components/Sidebar.tsx+11−12
@@ -224,6 +224,10 @@ import { · 223 unmodified lines
224224 type SidebarProjectSnapshot,
225225 } from "../sidebarProjectGrouping";
226226 import { SidebarProviderUpdatePill } from "./sidebar/SidebarProviderUpdatePill";
227+import {
228+ type SidebarThreadListAutoAnimateRef,
229+ useSidebarThreadListAutoAnimateRef,
230+} from "./sidebar/useSidebarThreadListAutoAnimate";
227231 const SIDEBAR_SORT_LABELS: Record<SidebarProjectSortOrder, string> = {
228232 updated_at: "Last user message",
229233 created_at: "Created at",
@@ -913,7 +917,7 @@ interface SidebarProjectThreadListProps { · 683 unmodified lines
913917 confirmingArchiveThreadKey: string | null;
914918 setConfirmingArchiveThreadKey: React.Dispatch<React.SetStateAction<string | null>>;
915919 confirmArchiveButtonRefs: React.RefObject<Map<string, HTMLButtonElement>>;
916− attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
920+ attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
917921 handleThreadClick: (
918922 event: React.MouseEvent,
919923 threadRef: ScopedThreadRef,
@@ -982,7 +986,9 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList( · 62 unmodified lines
982986
983987 return (
984988 <SidebarMenuSub
985− ref={attachThreadListAutoAnimateRef}
989+ ref={(node) => {
990+ attachThreadListAutoAnimateRef(node, renderedThreads.length);
991+ }}
986992 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"
987993 >
988994 {shouldShowThreadPanel && showEmptyThreadState ? (
@@ -1075,7 +1081,7 @@ interface SidebarProjectItemProps { · 86 unmodified lines
10751081 archiveThread: ReturnType<typeof useThreadActions>["archiveThread"];
10761082 deleteThread: ReturnType<typeof useThreadActions>["deleteThread"];
10771083 threadJumpLabelByKey: ReadonlyMap<string, string>;
1078− attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
1084+ attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
10791085 expandThreadListForProject: (projectKey: string) => void;
10801086 collapseThreadListForProject: (projectKey: string) => void;
10811087 dragInProgressRef: React.RefObject<boolean>;
@@ -2921,7 +2927,7 @@ interface SidebarProjectsContentProps { · 1839 unmodified lines
29212927 newThreadShortcutLabel: string | null;
29222928 commandPaletteShortcutLabel: string | null;
29232929 threadJumpLabelByKey: ReadonlyMap<string, string>;
2924− attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;
2930+ attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
29252931 expandThreadListForProject: (projectKey: string) => void;
29262932 collapseThreadListForProject: (projectKey: string) => void;
29272933 dragInProgressRef: React.RefObject<boolean>;
@@ -3419,14 +3425,7 @@ export default function Sidebar() { · 491 unmodified lines
34193425 animatedProjectListsRef.current.add(node);
34203426 }, []);
34213427
3422− const animatedThreadListsRef = useRef(new WeakSet<HTMLElement>());
3423− const attachThreadListAutoAnimateRef = useCallback((node: HTMLElement | null) => {
3424− if (!node || animatedThreadListsRef.current.has(node)) {
3425− return;
3426− }
3427− autoAnimate(node, SIDEBAR_LIST_ANIMATION_OPTIONS);
3428− animatedThreadListsRef.current.add(node);
3429− }, []);
3428+ const attachThreadListAutoAnimateRef = useSidebarThreadListAutoAnimateRef();
34303429
34313430 const visibleThreads = useMemo(
34323431 () => sidebarThreads.filter((thread) => thread.archivedAt === null),
apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.test.tsx+90−0
@@ -0,0 +1,90 @@
1+import type { AnimationController } from "@formkit/auto-animate";
2+import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3+
4+const testState = vi.hoisted(() => ({
5+ autoAnimate: vi.fn(),
6+}));
7+
8+vi.mock("react", async (importOriginal) => {
9+ const actual = await importOriginal<typeof import("react")>();
10+ return {
11+ ...actual,
12+ useCallback: <T,>(callback: T): T => callback,
13+ useRef: <T,>(initialValue: T): { current: T } => ({ current: initialValue }),
14+ };
15+});
16+
17+vi.mock("@formkit/auto-animate", () => ({
18+ autoAnimate: testState.autoAnimate,
19+}));
20+
21+import {
22+ MAX_ANIMATED_SIDEBAR_THREAD_ROWS,
23+ shouldAnimateSidebarThreadList,
24+ useSidebarThreadListAutoAnimateRef,
25+} from "./useSidebarThreadListAutoAnimate";
26+
27+function makeController(): AnimationController {
28+ return {
29+ parent: {} as Element,
30+ enable: vi.fn(),
31+ disable: vi.fn(),
32+ isEnabled: vi.fn(() => true),
33+ destroy: vi.fn(),
34+ };
35+}
36+
37+describe("useSidebarThreadListAutoAnimateRef", () => {
38+ beforeEach(() => {
39+ testState.autoAnimate.mockReset();
40+ });
41+
42+ it("keeps animation enabled for ordinary sidebar thread lists", () => {
43+ const controller = makeController();
44+ testState.autoAnimate.mockReturnValue(controller);
45+ const attach = useSidebarThreadListAutoAnimateRef();
46+ const node = {} as HTMLElement;
47+
48+ attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS);
49+
50+ expect(testState.autoAnimate).toHaveBeenCalledOnce();
51+ expect(controller.enable).toHaveBeenCalledOnce();
52+ expect(controller.disable).not.toHaveBeenCalled();
53+ });
54+
55+ it("bypasses animations when the list grows beyond the threshold", () => {
56+ const controller = makeController();
57+ testState.autoAnimate.mockReturnValue(controller);
58+ const attach = useSidebarThreadListAutoAnimateRef();
59+ const node = {} as HTMLElement;
60+
61+ attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS);
62+ attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1);
63+
64+ expect(testState.autoAnimate).toHaveBeenCalledOnce();
65+ expect(controller.disable).toHaveBeenCalledOnce();
66+ expect(controller.enable).toHaveBeenCalledOnce();
67+ });
68+
69+ it("keeps animation disabled through a bulk removal before restoring it", async () => {
70+ const controller = makeController();
71+ testState.autoAnimate.mockReturnValue(controller);
72+ const attach = useSidebarThreadListAutoAnimateRef();
73+ const node = {} as HTMLElement;
74+
75+ attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1);
76+ attach(node, MAX_ANIMATED_SIDEBAR_THREAD_ROWS - 1);
77+
78+ expect(controller.disable).toHaveBeenCalledTimes(2);
79+ expect(controller.enable).not.toHaveBeenCalled();
80+
81+ await Promise.resolve();
82+
83+ expect(controller.enable).toHaveBeenCalledOnce();
84+ });
85+
86+ it("uses the threshold as the exact animation boundary", () => {
87+ expect(shouldAnimateSidebarThreadList(MAX_ANIMATED_SIDEBAR_THREAD_ROWS)).toBe(true);
88+ expect(shouldAnimateSidebarThreadList(MAX_ANIMATED_SIDEBAR_THREAD_ROWS + 1)).toBe(false);
89+ });
90+});
apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.ts+70−0
@@ -0,0 +1,70 @@
1+import {
2+ autoAnimate,
3+ type AnimationController,
4+ type AutoAnimateOptions,
5+} from "@formkit/auto-animate";
6+import { useCallback, useRef } from "react";
7+
8+export const MAX_ANIMATED_SIDEBAR_THREAD_ROWS = 20;
9+
10+const SIDEBAR_THREAD_LIST_ANIMATION_OPTIONS = {
11+ duration: 180,
12+ easing: "ease-out",
13+} as const satisfies Partial<AutoAnimateOptions>;
14+
15+export function shouldAnimateSidebarThreadList(visibleRowCount: number): boolean {
16+ return visibleRowCount <= MAX_ANIMATED_SIDEBAR_THREAD_ROWS;
17+}
18+
19+export type SidebarThreadListAutoAnimateRef = (
20+ node: HTMLElement | null,
21+ visibleRowCount: number,
22+) => void;
23+
24+interface SidebarThreadListAnimationState {
25+ controller: AnimationController;
26+ visibleRowCount: number;
27+}
28+
29+export function useSidebarThreadListAutoAnimateRef(): SidebarThreadListAutoAnimateRef {
30+ const animationStateRef = useRef(new WeakMap<HTMLElement, SidebarThreadListAnimationState>());
31+
32+ return useCallback((node: HTMLElement | null, visibleRowCount: number) => {
33+ if (!node) return;
34+
35+ let animationState = animationStateRef.current.get(node);
36+ if (!animationState) {
37+ animationState = {
38+ controller: autoAnimate(node, SIDEBAR_THREAD_LIST_ANIMATION_OPTIONS),
39+ visibleRowCount,
40+ };
41+ animationStateRef.current.set(node, animationState);
42+ }
43+
44+ const previousRowCount = animationState.visibleRowCount;
45+ animationState.visibleRowCount = visibleRowCount;
46+ if (
47+ shouldAnimateSidebarThreadList(previousRowCount) &&
48+ shouldAnimateSidebarThreadList(visibleRowCount)
49+ ) {
50+ animationState.controller.enable();
51+ return;
52+ }
53+
54+ animationState.controller.disable();
55+ if (!shouldAnimateSidebarThreadList(visibleRowCount)) return;
56+
57+ // Keep the controller disabled until the MutationObserver has processed the bulk removal,
58+ // then restore normal animation behavior for later changes to the now-small list.
59+ const stateForEnable = animationState;
60+ queueMicrotask(() => {
61+ const currentState = animationStateRef.current.get(node);
62+ if (
63+ currentState === stateForEnable &&
64+ shouldAnimateSidebarThreadList(currentState.visibleRowCount)
65+ ) {
66+ currentState.controller.enable();
67+ }
68+ });
69+ }, []);
70+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t