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
224 type SidebarProjectSnapshot,224 type SidebarProjectSnapshot,
225} from "../sidebarProjectGrouping";225} from "../sidebarProjectGrouping";
226import { SidebarProviderUpdatePill } from "./sidebar/SidebarProviderUpdatePill";226import { SidebarProviderUpdatePill } from "./sidebar/SidebarProviderUpdatePill";
227import {
228 type SidebarThreadListAutoAnimateRef,
229 useSidebarThreadListAutoAnimateRef,
230} from "./sidebar/useSidebarThreadListAutoAnimate";
227const SIDEBAR_SORT_LABELS: Record<SidebarProjectSortOrder, string> = {231const SIDEBAR_SORT_LABELS: Record<SidebarProjectSortOrder, string> = {
228 updated_at: "Last user message",232 updated_at: "Last user message",
229 created_at: "Created at",233 created_at: "Created at",
@@ -913,7 +917,7 @@ interface SidebarProjectThreadListProps { · 683 unmodified lines
913 confirmingArchiveThreadKey: string | null;917 confirmingArchiveThreadKey: string | null;
914 setConfirmingArchiveThreadKey: React.Dispatch<React.SetStateAction<string | null>>;918 setConfirmingArchiveThreadKey: React.Dispatch<React.SetStateAction<string | null>>;
915 confirmArchiveButtonRefs: React.RefObject<Map<string, HTMLButtonElement>>;919 confirmArchiveButtonRefs: React.RefObject<Map<string, HTMLButtonElement>>;
916 attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;920 attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
917 handleThreadClick: (921 handleThreadClick: (
918 event: React.MouseEvent,922 event: React.MouseEvent,
919 threadRef: ScopedThreadRef,923 threadRef: ScopedThreadRef,
@@ -982,7 +986,9 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList( · 62 unmodified lines
982 986
983 return (987 return (
984 <SidebarMenuSub988 <SidebarMenuSub
985 ref={attachThreadListAutoAnimateRef}989 ref={(node) => {
990 attachThreadListAutoAnimateRef(node, renderedThreads.length);
991 }}
986 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"992 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"
987 >993 >
988 {shouldShowThreadPanel && showEmptyThreadState ? (994 {shouldShowThreadPanel && showEmptyThreadState ? (
@@ -1075,7 +1081,7 @@ interface SidebarProjectItemProps { · 86 unmodified lines
1075 archiveThread: ReturnType<typeof useThreadActions>["archiveThread"];1081 archiveThread: ReturnType<typeof useThreadActions>["archiveThread"];
1076 deleteThread: ReturnType<typeof useThreadActions>["deleteThread"];1082 deleteThread: ReturnType<typeof useThreadActions>["deleteThread"];
1077 threadJumpLabelByKey: ReadonlyMap<string, string>;1083 threadJumpLabelByKey: ReadonlyMap<string, string>;
1078 attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;1084 attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
1079 expandThreadListForProject: (projectKey: string) => void;1085 expandThreadListForProject: (projectKey: string) => void;
1080 collapseThreadListForProject: (projectKey: string) => void;1086 collapseThreadListForProject: (projectKey: string) => void;
1081 dragInProgressRef: React.RefObject<boolean>;1087 dragInProgressRef: React.RefObject<boolean>;
@@ -2921,7 +2927,7 @@ interface SidebarProjectsContentProps { · 1839 unmodified lines
2921 newThreadShortcutLabel: string | null;2927 newThreadShortcutLabel: string | null;
2922 commandPaletteShortcutLabel: string | null;2928 commandPaletteShortcutLabel: string | null;
2923 threadJumpLabelByKey: ReadonlyMap<string, string>;2929 threadJumpLabelByKey: ReadonlyMap<string, string>;
2924 attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void;2930 attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef;
2925 expandThreadListForProject: (projectKey: string) => void;2931 expandThreadListForProject: (projectKey: string) => void;
2926 collapseThreadListForProject: (projectKey: string) => void;2932 collapseThreadListForProject: (projectKey: string) => void;
2927 dragInProgressRef: React.RefObject<boolean>;2933 dragInProgressRef: React.RefObject<boolean>;
@@ -3419,14 +3425,7 @@ export default function Sidebar() { · 491 unmodified lines
3419 animatedProjectListsRef.current.add(node);3425 animatedProjectListsRef.current.add(node);
3420 }, []);3426 }, []);
3421 3427
3422 const animatedThreadListsRef = useRef(new WeakSet<HTMLElement>());3428 const attachThreadListAutoAnimateRef = useSidebarThreadListAutoAnimateRef();
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 }, []);
3430 3429
3431 const visibleThreads = useMemo(3430 const visibleThreads = useMemo(
3432 () => sidebarThreads.filter((thread) => thread.archivedAt === null),3431 () => sidebarThreads.filter((thread) => thread.archivedAt === null),
apps/web/src/components/sidebar/useSidebarThreadListAutoAnimate.test.tsx+90−0
@@ -0,0 +1,90 @@
1import type { AnimationController } from "@formkit/auto-animate";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4const testState = vi.hoisted(() => ({
5 autoAnimate: vi.fn(),
6}));
7
8vi.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
17vi.mock("@formkit/auto-animate", () => ({
18 autoAnimate: testState.autoAnimate,
19}));
20
21import {
22 MAX_ANIMATED_SIDEBAR_THREAD_ROWS,
23 shouldAnimateSidebarThreadList,
24 useSidebarThreadListAutoAnimateRef,
25} from "./useSidebarThreadListAutoAnimate";
26
27function 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
37describe("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 @@
1import {
2 autoAnimate,
3 type AnimationController,
4 type AutoAnimateOptions,
5} from "@formkit/auto-animate";
6import { useCallback, useRef } from "react";
7
8export const MAX_ANIMATED_SIDEBAR_THREAD_ROWS = 20;
9
10const SIDEBAR_THREAD_LIST_ANIMATION_OPTIONS = {
11 duration: 180,
12 easing: "ease-out",
13} as const satisfies Partial<AutoAnimateOptions>;
14
15export function shouldAnimateSidebarThreadList(visibleRowCount: number): boolean {
16 return visibleRowCount <= MAX_ANIMATED_SIDEBAR_THREAD_ROWS;
17}
18
19export type SidebarThreadListAutoAnimateRef = (
20 node: HTMLElement | null,
21 visibleRowCount: number,
22) => void;
23
24interface SidebarThreadListAnimationState {
25 controller: AnimationController;
26 visibleRowCount: number;
27}
28
29export 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