Unified · Split
| @@ -224,6 +224,10 @@ import { · 223 unmodified lines | ||
| 224 | 224 | type SidebarProjectSnapshot, |
| 225 | 225 | } from "../sidebarProjectGrouping"; |
| 226 | 226 | import { SidebarProviderUpdatePill } from "./sidebar/SidebarProviderUpdatePill"; |
| 227 | +import { | |
| 228 | + type SidebarThreadListAutoAnimateRef, | |
| 229 | + useSidebarThreadListAutoAnimateRef, | |
| 230 | +} from "./sidebar/useSidebarThreadListAutoAnimate"; | |
| 227 | 231 | const SIDEBAR_SORT_LABELS: Record<SidebarProjectSortOrder, string> = { |
| 228 | 232 | updated_at: "Last user message", |
| 229 | 233 | created_at: "Created at", |
| @@ -913,7 +917,7 @@ interface SidebarProjectThreadListProps { · 683 unmodified lines | ||
| 913 | 917 | confirmingArchiveThreadKey: string | null; |
| 914 | 918 | setConfirmingArchiveThreadKey: React.Dispatch<React.SetStateAction<string | null>>; |
| 915 | 919 | confirmArchiveButtonRefs: React.RefObject<Map<string, HTMLButtonElement>>; |
| 916 | − attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void; | |
| 920 | + attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef; | |
| 917 | 921 | handleThreadClick: ( |
| 918 | 922 | event: React.MouseEvent, |
| 919 | 923 | threadRef: ScopedThreadRef, |
| @@ -982,7 +986,9 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList( · 62 unmodified lines | ||
| 982 | 986 | |
| 983 | 987 | return ( |
| 984 | 988 | <SidebarMenuSub |
| 985 | − ref={attachThreadListAutoAnimateRef} | |
| 989 | + ref={(node) => { | |
| 990 | + attachThreadListAutoAnimateRef(node, renderedThreads.length); | |
| 991 | + }} | |
| 986 | 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 | 994 | {shouldShowThreadPanel && showEmptyThreadState ? ( |
| @@ -1075,7 +1081,7 @@ interface SidebarProjectItemProps { · 86 unmodified lines | ||
| 1075 | 1081 | archiveThread: ReturnType<typeof useThreadActions>["archiveThread"]; |
| 1076 | 1082 | deleteThread: ReturnType<typeof useThreadActions>["deleteThread"]; |
| 1077 | 1083 | threadJumpLabelByKey: ReadonlyMap<string, string>; |
| 1078 | − attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void; | |
| 1084 | + attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef; | |
| 1079 | 1085 | expandThreadListForProject: (projectKey: string) => void; |
| 1080 | 1086 | collapseThreadListForProject: (projectKey: string) => void; |
| 1081 | 1087 | dragInProgressRef: React.RefObject<boolean>; |
| @@ -2921,7 +2927,7 @@ interface SidebarProjectsContentProps { · 1839 unmodified lines | ||
| 2921 | 2927 | newThreadShortcutLabel: string | null; |
| 2922 | 2928 | commandPaletteShortcutLabel: string | null; |
| 2923 | 2929 | threadJumpLabelByKey: ReadonlyMap<string, string>; |
| 2924 | − attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void; | |
| 2930 | + attachThreadListAutoAnimateRef: SidebarThreadListAutoAnimateRef; | |
| 2925 | 2931 | expandThreadListForProject: (projectKey: string) => void; |
| 2926 | 2932 | collapseThreadListForProject: (projectKey: string) => void; |
| 2927 | 2933 | dragInProgressRef: React.RefObject<boolean>; |
| @@ -3419,14 +3425,7 @@ export default function Sidebar() { · 491 unmodified lines | ||
| 3419 | 3425 | animatedProjectListsRef.current.add(node); |
| 3420 | 3426 | }, []); |
| 3421 | 3427 | |
| 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(); | |
| 3430 | 3429 | |
| 3431 | 3430 | const visibleThreads = useMemo( |
| 3432 | 3431 | () => sidebarThreads.filter((thread) => thread.archivedAt === null), |
| @@ -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 | +}); | |
| @@ -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 | +} | |