diff --git a/apps/web/src/components/Sidebar.drag.ts b/apps/web/src/components/Sidebar.drag.ts index 76f2c9bf3a3ec..8c70d8c39ed35 100644 --- a/apps/web/src/components/Sidebar.drag.ts +++ b/apps/web/src/components/Sidebar.drag.ts @@ -46,7 +46,7 @@ export function createSidebarCollisionDetection( const source = items?.find((item) => item.kind === "thread" && item.key === args.active.id); const boundary = args.droppableContainers .find((container) => container.id === sidebarMarkerId("pinned-divider")) - ?.node.current?.querySelector(".sidebar-drag-boundary-label") + ?.node.current?.querySelector("[data-sidebar-drag-boundary]") ?.getBoundingClientRect(); if (items && boundary && source?.kind === "thread" && pointer) { boundarySection ??= source.section === "pinned" ? "pinned" : "active"; @@ -201,7 +201,7 @@ export function createSidebarSortingStrategy(input: { const moved = item.kind === "thread" && item.key === active.key; return item.kind === "marker" && (item.marker === "pinned-header" || item.marker === "pinned-divider") - ? labelHeight + ? Math.max(labelHeight, rect?.height ?? 0) : item.kind === "marker" && item.marker.endsWith("placeholder") ? slimHeight : moved diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index a17427fcdb54e..9cd3d9878aaf7 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -26,6 +26,8 @@ import { shouldRecedeSidebarThread, sortInboxThreadsByReturn, resolveSidebarDropTarget, + sidebarMarkerId, + withCollapsedSectionOrders, planSidebarThreadDrop, resolveSidebarReorderableThreadKeys, sortPinnedThreadsForSidebar, @@ -1205,6 +1207,65 @@ describe("Working shelf (beta)", () => { expect(resolveSidebarDropVerb("active", "working")).toBeNull(); }); + it("keeps a collapsed section's hidden rows in order around the drop", () => { + // Pinned is collapsed to the route row p2; Recent is collapsed to nothing. + const collapsed: readonly SidebarListItem[] = [ + marker("pinned-header"), + row("p2", "pinned"), + marker("pinned-divider"), + marker("active-placeholder"), + row("a9", "active"), + marker("settled-header"), + ]; + const full = { pinned: ["p1", "p2", "p3"], active: ["a1", "a9", "a2"] }; + const drop = (activeKey: string, overId: string) => + withCollapsedSectionOrders( + resolveSidebarDropTarget(collapsed, activeKey, overId)!, + activeKey, + full, + ); + expect(drop("p2", "p2")).toEqual({ + section: "pinned", + pinnedOrder: full.pinned, + activeOrder: full.active, + }); + expect(drop("a9", "a9")).toEqual({ + section: "active", + pinnedOrder: full.pinned, + activeOrder: full.active, + }); + expect(drop("a9", "p2")).toEqual({ + section: "pinned", + pinnedOrder: ["p1", "a9", "p2", "p3"], + activeOrder: ["a1", "a2"], + }); + expect(drop("p2", "a9")).toEqual({ + section: "active", + pinnedOrder: ["p1", "p3"], + activeOrder: ["a1", "a9", "p2", "a2"], + }); + expect( + withCollapsedSectionOrders( + resolveSidebarDropTarget( + collapsed.filter((item) => item.kind !== "thread" || item.key !== "a9"), + "p2", + sidebarMarkerId("active-placeholder"), + )!, + "p2", + full, + ), + ).toEqual({ + section: "active", + pinnedOrder: ["p1", "p3"], + activeOrder: ["a1", "a9", "a2", "p2"], + }); + // An expanded section passes through unchanged. + const target = resolveSidebarDropTarget(items, "p1", "a2")!; + expect( + withCollapsedSectionOrders(target, "p1", { pinned: ["p1"], active: ["a1", "a2"] }), + ).toEqual(target); + }); + it.each([ ["pinned", "p1"], ["active", "a2"], diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index 0771ea2e33d5d..39343b7ed6d0f 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -283,6 +283,32 @@ export function resolveSidebarDropTarget( return { section, pinnedOrder, activeOrder }; } +// A collapsed section lists only some rows; the drop keeps the hidden rows' order and lands the moved row beside its visible neighbor. +export function withCollapsedSectionOrders( + target: SidebarDropTarget, + movedKey: string, + full: { readonly pinned: readonly string[]; readonly active: readonly string[] }, +): SidebarDropTarget { + const restore = (visible: readonly string[], fullOrder: readonly string[]) => { + const visibleKeys = new Set(visible); + if (fullOrder.every((key) => key === movedKey || visibleKeys.has(key))) return visible; + if (visible.length === 1 && visible[0] === movedKey && fullOrder.includes(movedKey)) + return fullOrder; + const order = fullOrder.filter((key) => key !== movedKey); + const movedIndex = visible.indexOf(movedKey); + if (movedIndex === -1) return order; + const before = order.indexOf(visible[movedIndex - 1] ?? ""); + const after = order.indexOf(visible[movedIndex + 1] ?? ""); + order.splice(before !== -1 ? before + 1 : after !== -1 ? after : order.length, 0, movedKey); + return order; + }; + return { + section: target.section, + pinnedOrder: restore(target.pinnedOrder, full.pinned), + activeOrder: restore(target.activeOrder, full.active), + }; +} + type SidebarThreadDropPlan = | { readonly kind: "none" } | { diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 59fcb56bfce9d..5c10151775818 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -195,6 +195,7 @@ import { isTrailingDoubleClick, orderItemsByPreferredIds, planSidebarThreadDrop, + withCollapsedSectionOrders, reduceSidebarProjectScopeMenuState, resolveAdjacentThreadId, resolveSidebarSweepKeys, @@ -347,6 +348,8 @@ const SETTLED_TAIL_PAGE_COUNT = 25; const SETTLED_SHELF_EXPANDED_KEY = "t3code:sidebar:settled-expanded"; const SNOOZED_SHELF_EXPANDED_KEY = "t3code:sidebar:snoozed-expanded"; const WORKING_SHELF_EXPANDED_KEY = "t3code:sidebar:working-expanded"; +const PINNED_SECTION_EXPANDED_KEY = "t3code:sidebar:pinned-expanded"; +const RECENT_SECTION_EXPANDED_KEY = "t3code:sidebar:recent-expanded"; const inboxReturns = createInboxReturnTracker(); @@ -482,6 +485,16 @@ function useSidebarThreads(input: { false, Schema.Boolean, ); + const [pinnedSectionExpandedSetting, setPinnedSectionExpanded] = useLocalStorage( + PINNED_SECTION_EXPANDED_KEY, + true, + Schema.Boolean, + ); + const [recentSectionExpandedSetting, setRecentSectionExpanded] = useLocalStorage( + RECENT_SECTION_EXPANDED_KEY, + true, + Schema.Boolean, + ); // Shared with the old per-project sidebar, so folders keep their open state. const projectExpandedById = useUiStateStore((store) => store.projectExpandedById); const setProjectExpanded = useUiStateStore((store) => store.setProjectExpanded); @@ -509,6 +522,16 @@ function useSidebarThreads(input: { const { pinnedThreads, activeThreads, workingThreads, snoozedThreads, settledThreads } = partition; + // Pinned and Recent only get headers, and so can only collapse, while something is pinned. + const showPinnedSections = pinnedThreads.length > 0; + const pinnedSectionExpanded = !showPinnedSections || pinnedSectionExpandedSetting; + const recentSectionExpanded = !showPinnedSections || recentSectionExpandedSetting; + const visiblePinnedThreads = pinnedSectionExpanded + ? pinnedThreads + : routeThreadOnly(pinnedThreads, routeRowThreadKey); + const visibleActiveThreads = recentSectionExpanded + ? activeThreads + : routeThreadOnly(activeThreads, routeRowThreadKey); // Older settled threads load in pages; the shelf lists them only as far as they are loaded. const olderSettledShelf = useOlderSettledShelf({ scopes: olderSettledScopes([...serverConfigs.keys()], scopedProjectKeys), @@ -544,7 +567,11 @@ function useSidebarThreads(input: { const projectFolders = folderGroups === null ? null - : groupSidebarThreadsByProject(folderGroups, activeThreads, keepEmpty).map((folder) => { + : groupSidebarThreadsByProject( + folderGroups, + visibleActiveThreads, + recentSectionExpanded ? keepEmpty : () => false, + ).map((folder) => { const collapsed = !resolveProjectExpanded( projectExpandedById, sidebarFolderExpansionKeys(folder.group), @@ -562,9 +589,9 @@ function useSidebarThreads(input: { }; }); const orderedThreads = [ - ...pinnedThreads, + ...visiblePinnedThreads, ...(projectFolders === null - ? activeThreads + ? visibleActiveThreads : projectFolders.flatMap((folder) => folder.threads)), ...visibleWorkingThreads, ...visibleSnoozedThreads, @@ -606,6 +633,13 @@ function useSidebarThreads(input: { toggleSnoozedShelf: () => setSnoozedShelfExpanded((value) => !value), workingShelfExpanded, toggleWorkingShelf: () => setWorkingShelfExpanded((value) => !value), + showPinnedSections, + pinnedSectionExpanded, + togglePinnedSection: () => setPinnedSectionExpanded((value) => !value), + recentSectionExpanded, + toggleRecentSection: () => setRecentSectionExpanded((value) => !value), + visiblePinnedThreads, + visibleActiveThreads, projectFolders, toggleProjectFolder: (group: SidebarProjectSnapshot) => { const keys = sidebarFolderExpansionKeys(group); @@ -1347,6 +1381,13 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve toggleSnoozedShelf, workingShelfExpanded, toggleWorkingShelf, + showPinnedSections, + pinnedSectionExpanded, + togglePinnedSection, + recentSectionExpanded, + toggleRecentSection, + visiblePinnedThreads, + visibleActiveThreads, projectFolders, toggleProjectFolder, renderedSettledThreads, @@ -1966,8 +2007,11 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve if (list && header) { const listRect = list.getBoundingClientRect(); const scale = list.offsetWidth > 0 ? listRect.width / list.offsetWidth : 1; + const headerRect = header.getBoundingClientRect(); dragLabelOffsetRef.current = - header.getBoundingClientRect().top - listRect.top + SIDEBAR_DRAG_LABEL_HEIGHT * scale; + headerRect.top - + listRect.top + + Math.max(headerRect.height, SIDEBAR_DRAG_LABEL_HEIGHT * scale); } else { dragLabelOffsetRef.current = 0; } @@ -2004,10 +2048,10 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve return []; } const items: SidebarListItem[] = [{ kind: "marker", marker: "pinned-header" }]; - const pinnedRows = rowsOf(pinnedThreads, "pinned"); + const pinnedRows = rowsOf(visiblePinnedThreads, "pinned"); items.push(...pinnedRows); items.push({ kind: "marker", marker: "pinned-divider" }); - const activeRows = rowsOf(activeThreads, "active"); + const activeRows = rowsOf(visibleActiveThreads, "active"); items.push({ kind: "marker", marker: "active-placeholder" }); items.push(...activeRows); if (workingThreads.length > 0) { @@ -2059,9 +2103,13 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve dragState === null && sidebarListItems.length + visibleDraftSessionCount > 0; // Cache on both fields so layout-key changes trigger the effect even when animate is unchanged. const listMotionPass = { layoutKey: listMotionLayoutKey, animate: listMotionAnimate }; - const shelfExpansionKey = [workingShelfExpanded, snoozedShelfExpanded, settledShelfExpanded].join( - ":", - ); + const shelfExpansionKey = [ + pinnedSectionExpanded, + recentSectionExpanded, + workingShelfExpanded, + snoozedShelfExpanded, + settledShelfExpanded, + ].join(":"); const previousShelfExpansionKeyRef = useRef(shelfExpansionKey); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- records moved rows and updates list motion after each layout-affecting commit useLayoutEffect(() => { @@ -2071,6 +2119,12 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve previousShelfExpansionKeyRef.current = shelfExpansionKey; listMotionRef.current?.update(listMotionPass.animate && shelvesUnchanged); }, [listMotionPass, shelfExpansionKey]); + const resolveDropTarget = (activeKey: string, overId: string) => { + const target = resolveSidebarDropTarget(sidebarListItems, activeKey, overId); + return target === null + ? null + : withCollapsedSectionOrders(target, activeKey, { pinned: pinnedKeys, active: activeKeys }); + }; const handleThreadDragOver = (event: DragOverEvent) => { const target = event.over ? resolveSidebarDropTarget(sidebarListItems, String(event.active.id), String(event.over.id)) @@ -2114,7 +2168,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve if (source === undefined) return createSidebarCollisionDetection(() => false); return createSidebarCollisionDetection( (id) => { - const target = resolveSidebarDropTarget(sidebarListItems, draggedThreadKey, id); + const target = resolveDropTarget(draggedThreadKey, id); if (target === null) return false; return ( planSidebarThreadDrop({ @@ -2144,10 +2198,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve if (projectFolders !== null) return; const activeKey = String(event.active.id); const activeSection = sectionByThreadKey.get(activeKey); - const target = - event.over === null - ? null - : resolveSidebarDropTarget(sidebarListItems, activeKey, String(event.over.id)); + const target = event.over === null ? null : resolveDropTarget(activeKey, String(event.over.id)); const activeThread = threadByKey.get(activeKey); if (activeSection === undefined || target === null || activeThread === undefined) return; const threadRef = scopeThreadRef(activeThread.environmentId, activeThread.id); @@ -3525,7 +3576,9 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve section === "snoozed" || section === "settled" ? `shelf:${section}` - : undefined) + : showPinnedSections + ? `shelf:${section === "pinned" ? "pinned" : "recent"}` + : undefined) } variantAction={ section === "snoozed" @@ -3609,6 +3662,8 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve ); }; const from = isContextDrag ? null : (dragState?.activeSection ?? null); + const pinnedSectionLabel = gt("Pinned", { $context: "sidebar section" }); + const recentSectionLabel = gt("Recent", { $context: "sidebar section" }); const items: ReactNode[] = [ , + showPinnedSections ? ( + + ) : ( + + ), ); break; case "pinned-divider": items.push( - , + showPinnedSections ? ( + + ) : ( + + ), ); break; case "active-placeholder": @@ -3683,12 +3768,12 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve {props.visible ? ( -
+
void }; }) { - const shelf = - props.marker === "working-header" - ? "working" - : props.marker === "snoozed-header" - ? "snoozed" - : "settled"; + const shelf = SECTION_BY_HEADER[props.marker]; return (