From 2eb41ab4027b99f6578ca4bf99c4c6e316ee28fa Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Mon, 28 Sep 2026 16:09:07 -0300 Subject: [PATCH 1/2] feat(web): collapse the sidebar to an icon rail Collapsing the sidebar hid it completely, losing sight of which company you were in and which threads needed you. - Collapsed on desktop and web, the sidebar is a rail: the profile avatar (with its menu and the "waiting elsewhere" dot), new thread, search, notes, automations, one icon per pinned or active thread in the current profile with a static status dot and a title tooltip, then usage and settings. With all profiles shown, each thread carries its profile's color on its edge. Mobile keeps its sheet. - The rail starts below the titlebar row, so macOS traffic lights and the sidebar toggle sit on one continuous bar; page headers inset only past what extends beyond the rail. - The expanded header is symmetric: traffic lights, the lights' own left padding, the T3 Code mark, and the toggle at the far right with the same padding. Without traffic lights both insets fall back to the usual controls inset. Co-Authored-By: Claude Opus 5.5 (1M context) --- apps/web/src/components/AppSidebarLayout.tsx | 3 +- .../profileScope/CollapsedThreadSidebar.tsx | 256 ++++++++++++++++++ .../src/profileScope/ProfileAwareSidebar.tsx | 11 +- apps/web/src/profileScope/ProfileSwitcher.tsx | 53 +++- apps/web/src/profileScope/profileSidebar.css | 64 +++++ docs/user/thread-sidebar.md | 4 + 6 files changed, 386 insertions(+), 5 deletions(-) create mode 100644 apps/web/src/profileScope/CollapsedThreadSidebar.tsx create mode 100644 apps/web/src/profileScope/profileSidebar.css diff --git a/apps/web/src/components/AppSidebarLayout.tsx b/apps/web/src/components/AppSidebarLayout.tsx index c3f11b0d43558..469c367196c8d 100644 --- a/apps/web/src/components/AppSidebarLayout.tsx +++ b/apps/web/src/components/AppSidebarLayout.tsx @@ -303,7 +303,8 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) { void; + children: ReactNode; +}) { + return ( + + {edgeColor ? ( + + ) : null} + + + } + > + {children} + + {label} + + + ); +} + +const RailThreadButton = memo(function RailThreadButton({ + thread, + isActive, + profileColor, +}: { + thread: EnvironmentThreadShell; + isActive: boolean; + /** Set while showing every profile, so each thread still reads as its company's. */ + profileColor: string | null; +}) { + const navigate = useNavigate(); + const threadRef = scopeThreadRef(thread.environmentId, thread.id); + const lastVisitedAt = useUiStateStore( + (state) => state.threadLastVisitedAtById[scopedThreadKey(threadRef)], + ); + const project = useProjects().find( + (candidate) => + candidate.environmentId === thread.environmentId && candidate.id === thread.projectId, + ); + const status = resolveThreadStatusPill({ thread: { ...thread, lastVisitedAt } }); + return ( + + void navigate({ + to: "/$environmentId/$threadId", + params: buildThreadRouteParams(threadRef), + }) + } + label={ + + {thread.title} + + {project?.title ?? "Unknown project"} + {status ? ` · ${status.label}` : ""} + + + } + > + + {project ? ( + + ) : ( + + )} + {/* Static on purpose: a rail of pulsing dots would repaint every frame. */} + {status ? ( + + ) : null} + + + ); +}); + +/** + * The sidebar as an icon rail: navigation, the profile avatar, and one icon per pinned or + * active thread in the current profile, marked with its status. Snoozed and settled threads + * stay in the expanded sidebar. + */ +export function CollapsedThreadSidebar() { + const navigate = useNavigate(); + const pathname = useLocation({ select: (location) => location.pathname }); + const activeThreadRef = useParams({ + strict: false, + select: (params) => resolveThreadRouteRef(params), + }); + const activeThreadKey = activeThreadRef ? scopedThreadKey(activeThreadRef) : null; + const threads = useThreadShells(); + const scopedProjectKeys = useProfileScopedProjectKeys(); + const { scope, profiles, projectProfiles } = useProfileScopeState(); + const colorByProfileId = useMemo( + () => new Map(profiles.map((profile) => [profile.id, profile.color] as const)), + [profiles], + ); + const nowMinute = useNowMinute(); + + const { pinned, active } = useMemo(() => { + const pinnedThreads: EnvironmentThreadShell[] = []; + const activeThreads: EnvironmentThreadShell[] = []; + for (const thread of threads) { + if (thread.archivedAt !== null || thread.settledOverride === "settled") continue; + if (effectiveSnoozed(thread, { now: nowMinute })) continue; + if ( + scopedProjectKeys !== null && + !scopedProjectKeys.has(projectScopeKey(thread.environmentId, thread.projectId)) + ) { + continue; + } + (thread.pinnedAt != null ? pinnedThreads : activeThreads).push(thread); + } + return { + pinned: sortPinnedThreadsForSidebar(pinnedThreads), + active: sortThreadsForSidebar(activeThreads), + }; + }, [nowMinute, scopedProjectKeys, threads]); + + const renderThreads = (list: ReadonlyArray) => + list.map((thread) => { + const key = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); + const profileId = + scope === "all" + ? projectProfiles.get(projectScopeKey(thread.environmentId, thread.projectId)) + : null; + return ( + + ); + }); + + return ( +
+
+ + + openCommandPalette({ open: "new-thread-in" })} + > + + + openCommandPalette()}> + + + void navigate({ to: "/notes" })} + > + + + void navigate({ to: "/automations" })} + > + + + +
+
+
+ + {renderThreads(pinned)} + {pinned.length > 0 && active.length > 0 ? ( +
  • + ) : null} + {renderThreads(active)} + +
  • + + void navigate({ to: "/usage" })} + > + + + void navigate({ to: "/settings" })} + > + + + +
    + ); +} diff --git a/apps/web/src/profileScope/ProfileAwareSidebar.tsx b/apps/web/src/profileScope/ProfileAwareSidebar.tsx index 27af2f594fb78..fac58184870be 100644 --- a/apps/web/src/profileScope/ProfileAwareSidebar.tsx +++ b/apps/web/src/profileScope/ProfileAwareSidebar.tsx @@ -7,10 +7,13 @@ import { import { useParams } from "@tanstack/react-router"; import { useEffect, useRef, type ReactNode } from "react"; +import { useSidebar } from "../components/ui/sidebar"; import { useHandleNewThread } from "../hooks/useHandleNewThread"; import { resolveThreadRouteTarget } from "../threadRoutes"; +import { CollapsedThreadSidebar } from "./CollapsedThreadSidebar"; import { ProfileScopeShortcuts } from "./ProfileSwitcher"; import { useProfileScopeState } from "./useProfileScope"; +import "./profileSidebar.css"; /** * The sidebar shows the profile of the thread you open. Opening one from a notification, search @@ -50,13 +53,17 @@ function ProfileFollowsActiveProject() { return null; } -/** Wraps the app sidebar's content with the profile shortcuts and the follow-the-project rule. */ +/** + * The app sidebar's content: the expanded sidebar it wraps, or the icon rail while collapsed on + * desktop. Mobile keeps its sheet, which only ever shows the expanded sidebar. + */ export function ProfileAwareSidebar({ children }: { children: ReactNode }) { + const { state, isMobile } = useSidebar(); return ( <> - {children} + {state === "collapsed" && !isMobile ? : children} ); } diff --git a/apps/web/src/profileScope/ProfileSwitcher.tsx b/apps/web/src/profileScope/ProfileSwitcher.tsx index 3055d6e0371c3..363ec15501cf6 100644 --- a/apps/web/src/profileScope/ProfileSwitcher.tsx +++ b/apps/web/src/profileScope/ProfileSwitcher.tsx @@ -193,13 +193,62 @@ function ProfileScopeMenuPopup({ ); } -/** Picks the company the sidebar shows: a row above the thread list. */ -export function ProfileSwitcher() { +/** + * Picks the company the sidebar shows. Expanded, it is a full row above the thread list; + * collapsed, it is the profile avatar at the top of the icon rail. + */ +export function ProfileSwitcher({ collapsed = false }: { collapsed?: boolean }) { const { options, current, setScope, profiles, attentionElsewhere } = useScopeOptions(); if (profiles.length === 0) return null; const elsewhereLabel = attentionElsewhere > 0 ? `${attentionElsewhere} waiting in other profiles` : null; + if (collapsed) { + return ( + <> + + + + } + /> + } + > + + + {attentionElsewhere > 0 ? ( + + ) : null} + + + + {current.label} + {elsewhereLabel ? ` · ${elsewhereLabel}` : ""} + + + + +
    + + ); + } + return ( <> diff --git a/apps/web/src/profileScope/profileSidebar.css b/apps/web/src/profileScope/profileSidebar.css new file mode 100644 index 0000000000000..1e9814c63da3e --- /dev/null +++ b/apps/web/src/profileScope/profileSidebar.css @@ -0,0 +1,64 @@ +/* + * Collapsed to icons, the rail starts below the titlebar row. That row then spans the whole + * window, like the hidden sidebar's: the macOS traffic lights (wider than the rail) and the fixed + * sidebar toggle sit on one continuous bar instead of straddling the rail's edge. + */ +[data-slot="sidebar"][data-collapsible="icon"] [data-slot="sidebar-container"] { + top: var(--workspace-topbar-height); + height: calc(100svh - var(--workspace-topbar-height)); +} + +/* + * Headers inset past the toggle when the sidebar collapses (see workspaceTitlebar.ts), measured + * from the window edge. The rail already moved them right by its width, so only the rest of the + * window controls and toggle needs clearing. + */ +[data-slot="sidebar-wrapper"]:has(> [data-slot="sidebar"][data-collapsible="icon"]) + > :not([data-slot="sidebar"]) { + --workspace-titlebar-content-left: max( + var(--workspace-gutter-start), + calc( + var(--workspace-controls-left) + var(--workspace-titlebar-control-size) + + var(--workspace-titlebar-control-gap) - var(--sidebar-width-icon) + ) + ); +} + +/* + * Expanded sidebar header: traffic lights, the brand, and the sidebar toggle at the far right, + * with the lights' own left padding repeated between the lights and the brand and after the + * toggle. The macOS app reserves --desktop-window-controls-inset (90px at 100% zoom) for lights + * drawn 16px from the edge and 52px wide; the ratios keep that geometry under zoom. Without + * lights (web, Windows, Linux, macOS fullscreen) both insets fall back to + * --workspace-controls-left. + */ +[data-slot="sidebar-wrapper"] { + --sidebar-header-edge: min( + var(--workspace-controls-left), + calc(var(--desktop-window-controls-inset, 90px) * 16 / 90) + ); + --sidebar-header-brand-left: max( + var(--sidebar-header-edge), + calc(var(--workspace-controls-left) - var(--desktop-window-controls-inset, 90px) * 6 / 90) + ); +} + +@media (min-width: 48rem) { + [data-slot="sidebar-wrapper"] > [data-slot="sidebar"] { + --workspace-titlebar-content-left: var(--sidebar-header-brand-left); + } + + [data-slot="sidebar-wrapper"][data-sidebar-state="expanded"] > [data-sidebar-control] { + /* 2px: the toggle's own ml-px and the sidebar's 1px right border. */ + left: calc( + var(--sidebar-width) - var(--sidebar-header-edge) - var(--workspace-titlebar-control-size) - + 2px + ); + } + + @media (prefers-reduced-motion: no-preference) { + [data-panel-animations="true"] > [data-sidebar-control] { + transition: left var(--panel-animation-duration) ease-out; + } + } +} diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index 4390b712e85e3..128d8b95604f6 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -139,6 +139,10 @@ The sidebar follows the thread you open. Opening a thread from another profile, notification, search, or link, switches to that profile, and so does picking another project for a new thread. Choosing a profile yourself while a thread is open keeps your choice. +Collapsing the sidebar leaves a strip of icons: the profile, new thread, search, notes, +automations, and one icon per pinned or active thread, marked with its status. Hover an icon for the +thread's title. With **All profiles**, a colored edge shows each thread's profile. + ## Find and reference work On web and desktop, open the command palette with `Cmd/Ctrl+K` to search threads From 5245f93f6f39047928d80130af37957cf4f84ec0 Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Mon, 28 Sep 2026 16:34:39 -0300 Subject: [PATCH 2/2] fix(web): keep shortcuts, snoozes and status dots right in the icon rail Review fixes for the collapsed sidebar: - Collapsing unmounted the expanded sidebar, and with it the thread jump and previous/next shortcuts that `Sidebar` and `LegacySidebar` register. It now stays mounted but hidden while the rail shows, as it stayed mounted off-canvas before; hidden rows also drop their live subscriptions. A regression test covers collapse and expand. - The rail compared snooze times against the zone-less minute string, which `Date.parse` reads as local time, so snoozed threads woke early or late outside UTC. It now compares full UTC timestamps and, like the expanded sidebar, re-reads the clock exactly when the earliest snooze ends. Tests run in America/Sao_Paulo. - The menu button truncates its last span, which clipped the icon wrapper and its status dot. A screen-reader title now comes last instead. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../profileScope/CollapsedThreadSidebar.tsx | 49 ++++------ .../src/profileScope/ProfileAwareSidebar.tsx | 12 ++- .../profileScope/SidebarContentSlot.test.tsx | 50 ++++++++++ .../src/profileScope/SidebarContentSlot.tsx | 26 ++++++ .../collapsedThreadSidebar.logic.test.ts | 91 +++++++++++++++++++ .../collapsedThreadSidebar.logic.ts | 63 +++++++++++++ 6 files changed, 259 insertions(+), 32 deletions(-) create mode 100644 apps/web/src/profileScope/SidebarContentSlot.test.tsx create mode 100644 apps/web/src/profileScope/SidebarContentSlot.tsx create mode 100644 apps/web/src/profileScope/collapsedThreadSidebar.logic.test.ts create mode 100644 apps/web/src/profileScope/collapsedThreadSidebar.logic.ts diff --git a/apps/web/src/profileScope/CollapsedThreadSidebar.tsx b/apps/web/src/profileScope/CollapsedThreadSidebar.tsx index 4b196991a276f..3dc1293c795ff 100644 --- a/apps/web/src/profileScope/CollapsedThreadSidebar.tsx +++ b/apps/web/src/profileScope/CollapsedThreadSidebar.tsx @@ -1,6 +1,5 @@ import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models"; -import { effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled"; import { useLocation, useNavigate, useParams } from "@tanstack/react-router"; import { ChartNoAxesColumnIcon, @@ -11,23 +10,19 @@ import { SquarePenIcon, ZapIcon, } from "lucide-react"; -import { memo, useMemo, type ReactNode } from "react"; +import { memo, useEffect, useMemo, useState, type ReactNode } from "react"; import { openCommandPalette } from "../commandPaletteBus"; import { ProjectFavicon } from "../components/ProjectFavicon"; -import { - resolveThreadStatusPill, - sortPinnedThreadsForSidebar, - sortThreadsForSidebar, -} from "../components/Sidebar.logic"; +import { resolveThreadStatusPill } from "../components/Sidebar.logic"; import { SidebarMenu, SidebarMenuButton, SidebarMenuItem } from "../components/ui/sidebar"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../components/ui/tooltip"; -import { useNowMinute } from "../hooks/useNowMinute"; import { cn } from "../lib/utils"; import { useProjects, useThreadShells } from "../state/entities"; import { buildThreadRouteParams, resolveThreadRouteRef } from "../threadRoutes"; import { useUiStateStore } from "../uiStateStore"; import { profileScopeProjectKey as projectScopeKey } from "@t3tools/client-runtime/state/profile-scope"; +import { partitionRailThreads } from "./collapsedThreadSidebar.logic"; import { ProfileSwitcher } from "./ProfileSwitcher"; import { useProfileScopedProjectKeys, useProfileScopeState } from "./useProfileScope"; @@ -132,6 +127,9 @@ const RailThreadButton = memo(function RailThreadButton({ /> ) : null} + {/* The menu button truncates (and so clips) its last span; keeping the title last spares + the icon wrapper, whose status dot sits outside its box. */} + {thread.title} ); }); @@ -156,27 +154,20 @@ export function CollapsedThreadSidebar() { () => new Map(profiles.map((profile) => [profile.id, profile.color] as const)), [profiles], ); - const nowMinute = useNowMinute(); - - const { pinned, active } = useMemo(() => { - const pinnedThreads: EnvironmentThreadShell[] = []; - const activeThreads: EnvironmentThreadShell[] = []; - for (const thread of threads) { - if (thread.archivedAt !== null || thread.settledOverride === "settled") continue; - if (effectiveSnoozed(thread, { now: nowMinute })) continue; - if ( - scopedProjectKeys !== null && - !scopedProjectKeys.has(projectScopeKey(thread.environmentId, thread.projectId)) - ) { - continue; - } - (thread.pinnedAt != null ? pinnedThreads : activeThreads).push(thread); - } - return { - pinned: sortPinnedThreadsForSidebar(pinnedThreads), - active: sortThreadsForSidebar(activeThreads), - }; - }, [nowMinute, scopedProjectKeys, threads]); + // Snooze wakes are second-precise, so like the expanded sidebar the rail re-reads the clock + // exactly when the earliest snooze ends rather than on a minute tick. + const [now, setNow] = useState(() => new Date()); + const { pinned, active, nextWakeAtMs } = useMemo( + () => partitionRailThreads({ threads, scopedProjectKeys, now }), + [now, scopedProjectKeys, threads], + ); + useEffect(() => { + if (nextWakeAtMs === null) return; + // setTimeout delays are signed 32-bit; clamp so a far-future wake re-arms instead of firing. + const delayMs = Math.min(Math.max(0, nextWakeAtMs - Date.now()) + 50, 2_147_483_647); + const id = window.setTimeout(() => setNow(new Date()), delayMs); + return () => window.clearTimeout(id); + }, [nextWakeAtMs]); const renderThreads = (list: ReadonlyArray) => list.map((thread) => { diff --git a/apps/web/src/profileScope/ProfileAwareSidebar.tsx b/apps/web/src/profileScope/ProfileAwareSidebar.tsx index fac58184870be..23136a766f665 100644 --- a/apps/web/src/profileScope/ProfileAwareSidebar.tsx +++ b/apps/web/src/profileScope/ProfileAwareSidebar.tsx @@ -12,6 +12,7 @@ import { useHandleNewThread } from "../hooks/useHandleNewThread"; import { resolveThreadRouteTarget } from "../threadRoutes"; import { CollapsedThreadSidebar } from "./CollapsedThreadSidebar"; import { ProfileScopeShortcuts } from "./ProfileSwitcher"; +import { SidebarContentSlot } from "./SidebarContentSlot"; import { useProfileScopeState } from "./useProfileScope"; import "./profileSidebar.css"; @@ -54,8 +55,8 @@ function ProfileFollowsActiveProject() { } /** - * The app sidebar's content: the expanded sidebar it wraps, or the icon rail while collapsed on - * desktop. Mobile keeps its sheet, which only ever shows the expanded sidebar. + * The app sidebar's content: the expanded sidebar it wraps, and the icon rail in its place while + * collapsed on desktop. Mobile keeps its sheet, which only ever shows the expanded sidebar. */ export function ProfileAwareSidebar({ children }: { children: ReactNode }) { const { state, isMobile } = useSidebar(); @@ -63,7 +64,12 @@ export function ProfileAwareSidebar({ children }: { children: ReactNode }) { <> - {state === "collapsed" && !isMobile ? : children} + } + > + {children} + ); } diff --git a/apps/web/src/profileScope/SidebarContentSlot.test.tsx b/apps/web/src/profileScope/SidebarContentSlot.test.tsx new file mode 100644 index 0000000000000..c9392164c4a4b --- /dev/null +++ b/apps/web/src/profileScope/SidebarContentSlot.test.tsx @@ -0,0 +1,50 @@ +import { act, createElement, useEffect } from "react"; +import { create, type ReactTestRenderer } from "react-test-renderer"; +import { describe, expect, it, vi } from "vite-plus/test"; + +import { SidebarContentSlot } from "./SidebarContentSlot"; + +describe("SidebarContentSlot", () => { + it("keeps the expanded sidebar's shortcuts registered across collapse and expand", () => { + const onShortcut = vi.fn(); + const lifecycle: string[] = []; + const shortcutHandlers = new Set<() => void>(); + // Stands in for the expanded sidebar: registers a shortcut handler for as long as it is mounted. + function ExpandedSidebar() { + useEffect(() => { + lifecycle.push("mount"); + shortcutHandlers.add(onShortcut); + return () => { + lifecycle.push("unmount"); + shortcutHandlers.delete(onShortcut); + }; + }, []); + return createElement("nav", null, "threads"); + } + const render = (showRail: boolean) => + createElement( + SidebarContentSlot, + { showRail, rail: createElement("aside", null, "rail") }, + createElement(ExpandedSidebar), + ); + const pressShortcut = () => { + for (const handler of shortcutHandlers) handler(); + }; + + let renderer!: ReactTestRenderer; + act(() => { + renderer = create(render(false)); + }); + act(() => renderer.update(render(true))); + pressShortcut(); + act(() => renderer.update(render(false))); + pressShortcut(); + + expect(lifecycle).toEqual(["mount"]); + expect(onShortcut).toHaveBeenCalledTimes(2); + expect(renderer.root.findAllByType("aside")).toHaveLength(0); + act(() => renderer.update(render(true))); + expect(renderer.root.findAllByType("aside")).toHaveLength(1); + act(() => renderer.unmount()); + }); +}); diff --git a/apps/web/src/profileScope/SidebarContentSlot.tsx b/apps/web/src/profileScope/SidebarContentSlot.tsx new file mode 100644 index 0000000000000..69b93e73c6bca --- /dev/null +++ b/apps/web/src/profileScope/SidebarContentSlot.tsx @@ -0,0 +1,26 @@ +import type { ReactNode } from "react"; + +/** + * Shows the rail while collapsed without unmounting the expanded sidebar. Thread jump and + * previous/next shortcuts live in the expanded sidebars (`Sidebar`, `LegacySidebar`), so they + * keep working collapsed, as they did when collapsing only slid the sidebar off-canvas. + * `display: contents` keeps the expanded content a direct flex child of the sidebar. + */ +export function SidebarContentSlot({ + showRail, + rail, + children, +}: { + showRail: boolean; + rail: ReactNode; + children?: ReactNode; +}) { + return ( + <> +
    + {children} +
    + {showRail ? rail : null} + + ); +} diff --git a/apps/web/src/profileScope/collapsedThreadSidebar.logic.test.ts b/apps/web/src/profileScope/collapsedThreadSidebar.logic.test.ts new file mode 100644 index 0000000000000..5572c5959d451 --- /dev/null +++ b/apps/web/src/profileScope/collapsedThreadSidebar.logic.test.ts @@ -0,0 +1,91 @@ +import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts"; +import { afterAll, beforeAll, describe, expect, it } from "vite-plus/test"; + +import { partitionRailThreads, type RailThread } from "./collapsedThreadSidebar.logic"; + +const environmentId = EnvironmentId.make("env-1"); +const projectId = ProjectId.make("project-1"); + +function thread(id: string, overrides: Partial = {}): RailThread { + return { + id: ThreadId.make(id), + environmentId, + projectId, + createdAt: "2026-09-01T08:00:00.000Z", + archivedAt: null, + settledOverride: null, + pinnedAt: null, + pinOrderKey: null, + activeOrderKey: null, + unsettledAt: null, + snoozedUntil: null, + snoozedAt: null, + hasPendingApprovals: false, + hasPendingUserInput: false, + session: null, + latestTurn: null, + ...overrides, + }; +} + +describe("partitionRailThreads", () => { + // Snooze times are UTC; run where local time differs so a zone-less "now" would misread them. + let previousTimeZone: string | undefined; + beforeAll(() => { + previousTimeZone = process.env.TZ; + process.env.TZ = "America/Sao_Paulo"; + }); + afterAll(() => { + if (previousTimeZone === undefined) delete process.env.TZ; + else process.env.TZ = previousTimeZone; + }); + + const now = new Date("2026-09-01T12:00:00.000Z"); + + it("keeps a thread snoozed until its UTC wake time outside UTC", () => { + const result = partitionRailThreads({ + threads: [ + thread("still-snoozed", { + snoozedAt: "2026-09-01T11:00:00.000Z", + snoozedUntil: "2026-09-01T13:00:00.000Z", + }), + thread("woke", { + snoozedAt: "2026-09-01T10:00:00.000Z", + snoozedUntil: "2026-09-01T11:00:00.000Z", + }), + ], + scopedProjectKeys: null, + now, + }); + expect(result.active.map((entry) => entry.id)).toEqual(["woke"]); + expect(result.nextWakeAtMs).toBe(Date.parse("2026-09-01T13:00:00.000Z")); + }); + + it("reports the earliest wake among scoped threads and leaves out settled and archived ones", () => { + const result = partitionRailThreads({ + threads: [ + thread("later", { + snoozedAt: "2026-09-01T11:00:00.000Z", + snoozedUntil: "2026-09-01T18:00:00.000Z", + }), + thread("sooner", { + snoozedAt: "2026-09-01T11:00:00.000Z", + snoozedUntil: "2026-09-01T12:30:00.000Z", + }), + thread("other-profile", { + projectId: ProjectId.make("project-2"), + snoozedAt: "2026-09-01T11:00:00.000Z", + snoozedUntil: "2026-09-01T12:05:00.000Z", + }), + thread("settled", { settledOverride: "settled" }), + thread("archived", { archivedAt: "2026-09-01T09:00:00.000Z" }), + thread("pinned", { pinnedAt: "2026-09-01T09:00:00.000Z" }), + ], + scopedProjectKeys: new Set([`${environmentId}:${projectId}`]), + now, + }); + expect(result.pinned.map((entry) => entry.id)).toEqual(["pinned"]); + expect(result.active).toEqual([]); + expect(result.nextWakeAtMs).toBe(Date.parse("2026-09-01T12:30:00.000Z")); + }); +}); diff --git a/apps/web/src/profileScope/collapsedThreadSidebar.logic.ts b/apps/web/src/profileScope/collapsedThreadSidebar.logic.ts new file mode 100644 index 0000000000000..4f08ef5ec0ff2 --- /dev/null +++ b/apps/web/src/profileScope/collapsedThreadSidebar.logic.ts @@ -0,0 +1,63 @@ +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models"; +import { profileScopeProjectKey } from "@t3tools/client-runtime/state/profile-scope"; +import { effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled"; + +import { sortPinnedThreadsForSidebar, sortThreadsForSidebar } from "../components/Sidebar.logic"; + +/** The thread fields the rail sorts, filters and snoozes by. */ +export type RailThread = Pick< + EnvironmentThreadShell, + | "id" + | "environmentId" + | "projectId" + | "createdAt" + | "archivedAt" + | "settledOverride" + | "pinnedAt" + | "pinOrderKey" + | "activeOrderKey" + | "unsettledAt" + | "snoozedUntil" + | "snoozedAt" + | "hasPendingApprovals" + | "hasPendingUserInput" + | "session" + | "latestTurn" +>; + +/** + * The rail's pinned and active threads in sidebar order, leaving out archived, settled, snoozed + * and out-of-scope ones, plus when the next snooze ends so the rail can wake it on time. + * `now` is compared as a full UTC timestamp: snooze times are UTC, and a zone-less string + * would be read as local time. + */ +export function partitionRailThreads(input: { + readonly threads: ReadonlyArray; + readonly scopedProjectKeys: ReadonlySet | null; + readonly now: Date; +}): { readonly pinned: T[]; readonly active: T[]; readonly nextWakeAtMs: number | null } { + const now = input.now.toISOString(); + const pinned: T[] = []; + const active: T[] = []; + let nextWakeAtMs: number | null = null; + for (const thread of input.threads) { + if (thread.archivedAt !== null || thread.settledOverride === "settled") continue; + if ( + input.scopedProjectKeys !== null && + !input.scopedProjectKeys.has(profileScopeProjectKey(thread.environmentId, thread.projectId)) + ) { + continue; + } + if (effectiveSnoozed(thread, { now })) { + const wakeAtMs = Date.parse(thread.snoozedUntil ?? ""); + if (nextWakeAtMs === null || wakeAtMs < nextWakeAtMs) nextWakeAtMs = wakeAtMs; + continue; + } + (thread.pinnedAt != null ? pinned : active).push(thread); + } + return { + pinned: sortPinnedThreadsForSidebar(pinned), + active: sortThreadsForSidebar(active), + nextWakeAtMs, + }; +}