Skip to content
TypeScript

perf(web): the sidebar compiles with no manual memoization (#20396)

Julius Marminge2de98f035c3a
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.ts
index 54cabdce243..e664744b105 100644
--- a/apps/web/reactCompiledFiles.ts
+++ b/apps/web/reactCompiledFiles.ts
@@ -6,6 +6,7 @@
*/
export const REACT_COMPILED_FILES = [
"apps/web/src/components/PersistentThreadTerminals.tsx",
+ "apps/web/src/components/Sidebar.tsx",
"apps/web/src/components/chat/usePendingThreadRequests.ts",
"apps/web/src/components/chat/composerFocusRedirect.ts",
"apps/web/src/components/chat/useChatShortcuts.ts",
diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx
index 27550680a9f..03fcd8c0311 100644
--- a/apps/web/src/components/Sidebar.tsx
+++ b/apps/web/src/components/Sidebar.tsx
@@ -1,10 +1,7 @@
-import { type EnvironmentId, type ScopedProjectRef } from "@t3tools/contracts";
+import { type EnvironmentId, type ScopedProjectRef, type ServerConfig } from "@t3tools/contracts";
import { ThreadHoverCard, ThreadHoverCardPopup } from "./ThreadHoverCard";
import { CollapsibleSectionHeader } from "./ui/collapsible-section-header";
-import {
- setThreadChangeRequestSnapshot,
- ThreadTerminalStatusButton,
-} from "./ThreadStatusIndicators";
+import { ThreadTerminalStatusButton } from "./ThreadStatusIndicators";
import { ThreadContextDragGhost } from "./chat/ThreadContextDragGhost";
import {
dropThreadContext,
@@ -74,7 +71,6 @@ import {
CircleDashedIcon,
CircleDotDashedIcon,
ClockIcon,
- EyeIcon,
FolderIcon,
GitBranchIcon,
ListFilterIcon,
@@ -91,10 +87,8 @@ import {
} from "lucide-react";
import {
memo,
- useCallback,
useEffect,
useLayoutEffect,
- useMemo,
useReducer,
useRef,
useState,
@@ -246,12 +240,9 @@ import {
ThreadPullRequestBadgeControl,
ThreadPullRequestsMiniList,
ThreadWorktreeIndicator,
- nextThreadChangeRequestSnapshot,
prStatusIndicator,
resolveThreadPullRequestBadge,
terminalStatusFromRunningIds,
- threadChangeRequestSnapshotsAtom,
- type ThreadChangeRequestSnapshot,
type TerminalStatusIndicator,
useLinkedThreadPullRequest,
ThreadSearchPullRequestNumber,
@@ -309,6 +300,442 @@ const WORKING_SHELF_EXPANDED_KEY = "t3code:sidebar:working-expanded";
const inboxReturns = createInboxReturnTracker();
+type SidebarOptimisticDrop = {
+ readonly key: string;
+ readonly sourceSection: SidebarSection;
+ readonly section: "pinned" | "active" | "settled";
+ readonly occurredAt: string;
+ readonly clearsSnooze: boolean;
+ readonly order: readonly string[] | null;
+ readonly keysAtDrop: ReadonlyMap<string, string | null>;
+ readonly assignedKeys: ReadonlyMap<string, string>;
+};
+
+interface SidebarThreadPartitionInput {
+ readonly threads: readonly EnvironmentThreadShell[];
+ readonly scopedProjectKeys: ReadonlySet<string> | null;
+ readonly serverConfigs: ReadonlyMap<EnvironmentId, ServerConfig>;
+ readonly optimisticDrop: SidebarOptimisticDrop | null;
+ readonly workingShelfEnabled: boolean;
+ // Snooze wakes and the queued-turn grace depend on the clock, so the minute
+ // tick and the precise snooze-wake tick are inputs: a new key repartitions.
+ readonly clockKey: string;
+}
+
+function threadShellKey(thread: Pick<SidebarThreadSummary, "environmentId" | "id">): string {
+ return scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
+}
+
+const NO_DRAGGED_ORDER: readonly string[] = [];
+const NO_DRAGGED_ORDERS: {
+ readonly settledOrder: readonly string[];
+ readonly activeOrder: readonly string[] | undefined;
+} = { settledOrder: NO_DRAGGED_ORDER, activeOrder: undefined };
+
+// Where the dragged row would sort if dropped on the settled or the
+// time-ordered active shelf; the strategy previews those slots.
+function resolveDraggedOrders(input: {
+ readonly drag: { readonly activeKey: string; readonly occurredAt: string };
+ readonly threadByKey: ReadonlyMap<string, EnvironmentThreadShell>;
+ readonly settledThreads: readonly EnvironmentThreadShell[];
+ readonly activeThreads: readonly EnvironmentThreadShell[];
+ readonly workingShelfEnabled: boolean;
+}): typeof NO_DRAGGED_ORDERS {
+ const { drag, threadByKey, settledThreads, activeThreads, workingShelfEnabled } = input;
+ const thread = threadByKey.get(drag.activeKey);
+ if (thread === undefined) return NO_DRAGGED_ORDERS;
+ const others = (list: readonly EnvironmentThreadShell[]) =>
+ list.filter((candidate) => threadShellKey(candidate) !== drag.activeKey);
+ return {
+ settledOrder: sortSettledThreads([
+ ...others(settledThreads),
+ applySidebarThreadDrop(thread, "settled", drag.occurredAt),
+ ]).map(threadShellKey),
+ activeOrder: workingShelfEnabled
+ ? sortInboxThreadsByReturn(
+ [...others(activeThreads), applySidebarThreadDrop(thread, "active", drag.occurredAt)],
+ inboxReturns.returnedAt,
+ ).map(threadShellKey)
+ : undefined,
+ };
+}
+
+function routeThreadOnly(
+ threads: readonly EnvironmentThreadShell[],
+ routeThreadKey: string | null,
+): readonly EnvironmentThreadShell[] {
+ if (routeThreadKey === null) return EMPTY_THREADS;
+ const routeThread = threads.find((thread) => threadShellKey(thread) === routeThreadKey);
+ return routeThread === undefined ? EMPTY_THREADS : [routeThread];
+}
+
+// Everything Sidebar derives from thread shells. Computed inside one hook so
+// each value reaches the component frozen and cached on its real inputs: in
+// the component body, later hook calls made the compiler treat these maps as
+// possibly mutated and recompute them, and every dnd-kit input with them.
+function useSidebarThreads(input: {
+ readonly projectScopeKey: string | null;
+ readonly scopedProjectKeys: ReadonlySet<string> | null;
+ readonly serverConfigs: ReadonlyMap<EnvironmentId, ServerConfig>;
+ readonly optimisticDrop: SidebarOptimisticDrop | null;
+ readonly routeThreadKey: string | null;
+}) {
+ const { projectScopeKey, scopedProjectKeys, serverConfigs, optimisticDrop, routeThreadKey } =
+ input;
+ const threads = useThreadShells();
+ const workingShelfEnabled = useClientSettings((s) => s.sidebarWorkingShelfEnabled);
+ const nowMinute = useNowMinute();
+ // Snooze wake times are second-precise, so classifying with the quantized
+ // minute would hold a woken thread on the shelf for up to a minute.
+ const [snoozeWakeTick, bumpSnoozeWakeTick] = useState(0);
+ const [nextSnoozeWakeAt, setNextSnoozeWakeAt] = useState<string | null>(null);
+ useEffect(
+ () => scheduleSnoozeWake(nextSnoozeWakeAt, () => bumpSnoozeWakeTick((tick) => tick + 1)),
+ [nextSnoozeWakeAt],
+ );
+ const [settledShelfExpanded, setSettledShelfExpanded] = useLocalStorage(
+ SETTLED_SHELF_EXPANDED_KEY,
+ false,
+ Schema.Boolean,
+ );
+ const [snoozedShelfExpanded, setSnoozedShelfExpanded] = useLocalStorage(
+ SNOOZED_SHELF_EXPANDED_KEY,
+ false,
+ Schema.Boolean,
+ );
+ const [workingShelfExpanded, setWorkingShelfExpanded] = useLocalStorage(
+ WORKING_SHELF_EXPANDED_KEY,
+ false,
+ Schema.Boolean,
+ );
+ // The settled tail restarts whenever the project scope changes.
+ const [settledTail, setSettledTail] = useState({
+ scopeKey: projectScopeKey,
+ count: SETTLED_TAIL_INITIAL_COUNT,
+ });
+ if (settledTail.scopeKey !== projectScopeKey) {
+ setSettledTail({ scopeKey: projectScopeKey, count: SETTLED_TAIL_INITIAL_COUNT });
+ }
+ const settledVisibleCount =
+ settledTail.scopeKey === projectScopeKey ? settledTail.count : SETTLED_TAIL_INITIAL_COUNT;
+
+ const partition = partitionSidebarThreads({
+ threads,
+ scopedProjectKeys,
+ serverConfigs,
+ optimisticDrop,
+ workingShelfEnabled,
+ clockKey: `${nowMinute}:${snoozeWakeTick}`,
+ });
+ const partitionNextWakeAt = partition.snoozedThreads[0]?.snoozedUntil ?? null;
+ if (partitionNextWakeAt !== nextSnoozeWakeAt) setNextSnoozeWakeAt(partitionNextWakeAt);
+
+ const { pinnedThreads, activeThreads, workingThreads, snoozedThreads, settledThreads } =
+ partition;
+ const visibleSettledThreads = (() => {
+ if (settledThreads.length <= settledVisibleCount) return settledThreads;
+ const visible = settledThreads.slice(0, settledVisibleCount);
+ if (routeThreadKey !== null) {
+ const routeThread = settledThreads
+ .slice(settledVisibleCount)
+ .find((thread) => threadShellKey(thread) === routeThreadKey);
+ if (routeThread !== undefined) visible.push(routeThread);
+ }
+ return visible;
+ })();
+ const renderedSettledThreads = settledShelfExpanded
+ ? visibleSettledThreads
+ : routeThreadOnly(visibleSettledThreads, routeThreadKey);
+ const visibleSnoozedThreads = snoozedShelfExpanded
+ ? snoozedThreads
+ : routeThreadOnly(snoozedThreads, routeThreadKey);
+ const visibleWorkingThreads = workingShelfExpanded
+ ? workingThreads
+ : routeThreadOnly(workingThreads, routeThreadKey);
+ const orderedThreads = [
+ ...pinnedThreads,
+ ...activeThreads,
+ ...visibleWorkingThreads,
+ ...visibleSnoozedThreads,
+ ...renderedSettledThreads,
+ ];
+ const sectionByThreadKey = new Map<string, SidebarSection>();
+ for (const [list, section] of [
+ [pinnedThreads, "pinned"],
+ [activeThreads, "active"],
+ [workingThreads, "working"],
+ [snoozedThreads, "snoozed"],
+ [settledThreads, "settled"],
+ ] as const) {
+ for (const thread of list) sectionByThreadKey.set(threadShellKey(thread), section);
+ }
+ return {
+ ...partition,
+ threads,
+ workingShelfEnabled,
+ searchableThreads: [
+ ...pinnedThreads,
+ ...activeThreads,
+ ...workingThreads,
+ ...snoozedThreads,
+ ...settledThreads,
+ ],
+ settledVisibleCount,
+ hiddenSettledCount: settledThreads.length - visibleSettledThreads.length,
+ showMoreSettled: () =>
+ setSettledTail((current) => ({
+ scopeKey: current.scopeKey,
+ count: current.count + SETTLED_TAIL_PAGE_COUNT,
+ })),
+ settledShelfExpanded,
+ toggleSettledShelf: () => setSettledShelfExpanded((value) => !value),
+ snoozedShelfExpanded,
+ toggleSnoozedShelf: () => setSnoozedShelfExpanded((value) => !value),
+ workingShelfExpanded,
+ toggleWorkingShelf: () => setWorkingShelfExpanded((value) => !value),
+ renderedSettledThreads,
+ visibleSnoozedThreads,
+ visibleWorkingThreads,
+ orderedThreadKeys: orderedThreads.map(threadShellKey),
+ threadByKey: new Map(orderedThreads.map((thread) => [threadShellKey(thread), thread] as const)),
+ settledThreadKeys: new Set(settledThreads.map(threadShellKey)),
+ snoozedThreadKeys: new Set(snoozedThreads.map(threadShellKey)),
+ sectionByThreadKey,
+ pinnedKeys: pinnedThreads.map(threadShellKey),
+ activeKeys: activeThreads.map(threadShellKey),
+ pinnedKeysById: new Map(
+ threads.map((thread) => [threadShellKey(thread), thread.pinOrderKey ?? null] as const),
+ ),
+ activeKeysById: new Map(
+ threads.map((thread) => [threadShellKey(thread), thread.activeOrderKey ?? null] as const),
+ ),
+ };
+}
+
+function partitionSidebarThreads(input: SidebarThreadPartitionInput) {
+ const { threads, scopedProjectKeys, serverConfigs, optimisticDrop, workingShelfEnabled } = input;
+ const preciseNow = new Date().toISOString();
+ const visible = filterSidebarV2VisibleThreads(threads, scopedProjectKeys);
+ inboxReturns.observe(workingShelfEnabled ? threads : null);
+ const pinned: EnvironmentThreadShell[] = [];
+ const active: EnvironmentThreadShell[] = [];
+ const working: EnvironmentThreadShell[] = [];
+ const inbox = (thread: EnvironmentThreadShell) =>
+ workingShelfEnabled && isSidebarThreadWorking(thread) ? working : active;
+ const snoozed: EnvironmentThreadShell[] = [];
+ const settled: EnvironmentThreadShell[] = [];
+ const draggable = new Set<string>();
+ const activeReorderable = new Set<string>();
+ for (const thread of visible) {
+ const capabilities = serverConfigs.get(thread.environmentId)?.environment.capabilities;
+ const supportsSettlement = capabilities?.threadSettlement === true;
+ const supportsSnooze = capabilities?.threadSnooze === true;
+ const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
+ if (capabilities?.threadActiveReorder === true) activeReorderable.add(threadKey);
+ if (capabilities?.threadPinning === true && capabilities.threadPinReorder === true) {
+ draggable.add(threadKey);
+ }
+ if (optimisticDrop?.key === threadKey) {
+ const projected = applySidebarThreadDrop(
+ thread,
+ optimisticDrop.section,
+ optimisticDrop.occurredAt,
+ optimisticDrop.assignedKeys.get(threadKey),
+ );
+ (optimisticDrop.section === "pinned"
+ ? pinned
+ : optimisticDrop.section === "settled"
+ ? settled
+ : inbox(projected)
+ ).push(
+ optimisticDrop.clearsSnooze
+ ? projected
+ : { ...projected, snoozedAt: thread.snoozedAt, snoozedUntil: thread.snoozedUntil },
+ );
+ } else {
+ const section = resolveSidebarThreadSection({
+ snoozed: supportsSnooze && effectiveSnoozed(thread, { now: preciseNow }),
+ settled: supportsSettlement && thread.settledOverride === "settled",
+ pinned: thread.pinnedAt != null,
+ });
+ (section === "snoozed"
+ ? snoozed
+ : section === "settled"
+ ? settled
+ : section === "pinned"
+ ? pinned
+ : inbox(thread)
+ ).push(thread);
+ }
+ }
+ // Server capability only gates DRAGGING — it must not influence the
+ // sort, or mixed-version fleets would render different pinned orders on
+ // web and mobile from the same data.
+ const sortedPinned = sortPinnedThreadsForSidebar(pinned);
+ const sortedActive = workingShelfEnabled
+ ? sortInboxThreadsByReturn(active, inboxReturns.returnedAt)
+ : sortThreadsForSidebar(active);
+ return {
+ pinnedThreads:
+ optimisticDrop?.section !== "pinned" || optimisticDrop.order === null
+ ? sortedPinned
+ : orderItemsByPreferredIds({
+ items: sortedPinned,
+ preferredIds: optimisticDrop.order,
+ getId: (thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
+ }),
+ draggableThreadKeys: draggable,
+ activeReorderableThreadKeys: activeReorderable,
+ activeThreads:
+ optimisticDrop?.section !== "active" || optimisticDrop.order === null
+ ? sortedActive
+ : orderItemsByPreferredIds({
+ items: sortedActive,
+ preferredIds: optimisticDrop.order,
+ getId: (thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
+ }),
+ workingThreads: sortWorkingThreadsBySend(working),
+ snoozedThreads: snoozed.toSorted(
+ (left, right) =>
+ firstValidTimestampMs(left.snoozedUntil ?? null) -
+ firstValidTimestampMs(right.snoozedUntil ?? null),
+ ),
+ settledThreads: sortSettledThreads(settled),
+ snoozeNow: preciseNow,
+ };
+}
+
+function isOptimisticDropReleased(input: {
+ readonly optimisticDrop: SidebarOptimisticDrop;
+ readonly threads: readonly EnvironmentThreadShell[];
+ readonly pinnedKeys: readonly string[];
+ readonly activeKeys: readonly string[];
+ readonly now: string;
+}): boolean {
+ const { optimisticDrop, threads, pinnedKeys, activeKeys, now } = input;
+ const canonicalByKey = new Map(
+ threads.map((thread) => [
+ scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
+ thread,
+ ]),
+ );
+ const thread = canonicalByKey.get(optimisticDrop.key);
+ if (thread === undefined || thread.archivedAt !== null) {
+ return true;
+ }
+ const canonicalSection = effectiveSnoozed(thread, { now })
+ ? "snoozed"
+ : thread.settledOverride === "settled"
+ ? "settled"
+ : thread.pinnedAt != null
+ ? "pinned"
+ : "active";
+ if (
+ canonicalSection !== optimisticDrop.sourceSection &&
+ canonicalSection !== optimisticDrop.section
+ ) {
+ return true;
+ }
+ if (optimisticDrop.order === null) {
+ // Settle also emits unpin/unsnooze events. Wait for the entire move
+ // before releasing the projected fields and sort timestamps.
+ if (
+ canonicalSection === optimisticDrop.section &&
+ thread.pinnedAt == null &&
+ (!optimisticDrop.clearsSnooze || thread.snoozedUntil == null)
+ ) {
+ return true;
+ }
+ return false;
+ }
+ if (canonicalSection !== optimisticDrop.section) return false;
+ if (optimisticDrop.clearsSnooze && thread.snoozedUntil != null) return false;
+ const destinationKeys = optimisticDrop.section === "pinned" ? pinnedKeys : activeKeys;
+ const canonicalDestination = destinationKeys.flatMap((key) => {
+ const canonical = canonicalByKey.get(key);
+ return canonical === undefined ? [] : [canonical];
+ });
+ const keyByThread = new Map(
+ canonicalDestination.map((thread) => [
+ scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
+ (optimisticDrop.section === "pinned" ? thread.pinOrderKey : thread.activeOrderKey) ?? null,
+ ]),
+ );
+ const heldOrder = optimisticDrop.order;
+ const heldKeys = new Set(heldOrder);
+ const membershipChanged =
+ destinationKeys.length !== heldOrder.length ||
+ destinationKeys.some((key) => !heldKeys.has(key));
+ const foreignKeyLanded = destinationKeys.some((threadKey) => {
+ const currentKey = keyByThread.get(threadKey) ?? null;
+ if (currentKey === (optimisticDrop.keysAtDrop.get(threadKey) ?? null)) return false;
+ return currentKey !== optimisticDrop.assignedKeys.get(threadKey);
+ });
+ const allAssignmentsLanded = [...optimisticDrop.assignedKeys].every(
+ ([threadKey, orderKey]) => keyByThread.get(threadKey) === orderKey,
+ );
+ return membershipChanged || foreignKeyLanded || allAssignmentsLanded;
+}
+
+// Sidebar() is large enough that the compiler cannot prove its local maps stay
+// unmutated; built inside a hook they reach it frozen and cached.
+function useSidebarLookups() {
+ const projects = useProjects();
+ const environments = useEnvironmentIdentities();
+ const serverConfigs = useAtomValue(environmentServerConfigsAtom);
+ const projectGroups = useSidebarProjectGroups();
+ const environmentLabelById = new Map(
+ environments.map((environment) => [environment.environmentId, environment.label] as const),
+ );
+ // Threads on non-primary environments (T3 Connect, hosted) resolve their
+ // provider entry from their own environment's config: default instance ids
+ // are driver slugs, so a flat map would collide across environments.
+ const providerEntriesByEnvironment = deriveProviderEntriesByEnvironment(
+ [...serverConfigs].map(
+ ([environmentId, config]) => [environmentId, config.providers, config.settings] as const,
+ ),
+ );
+ const projectByKey = new Map(
+ projects.map((project) => [`${project.environmentId}:${project.id}`, project] as const),
+ );
+ const projectDisplayNameByKey = new Map(
+ projectGroups.flatMap((group) =>
+ group.memberProjects.map(
+ (project) => [`${project.environmentId}:${project.id}`, group.displayName] as const,
+ ),
+ ),
+ );
+ const projectGroupByScopeKey = new Map(
+ projectGroups.map((project) => [project.projectKey, project] as const),
+ );
+ const projectScopeItems = [
+ { value: "all", label: "All projects" },
+ ...projectGroups.map((project) => ({ value: project.projectKey, label: project.displayName })),
+ ];
+ return {
+ projects,
+ serverConfigs,
+ projectGroups,
+ environmentLabelById,
+ providerEntriesByEnvironment,
+ projectByKey,
+ projectDisplayNameByKey,
+ projectGroupByScopeKey,
+ projectScopeItems,
+ };
+}
+
+function scheduleSnoozeWake(wakeAt: string | null, onWake: () => void): (() => void) | undefined {
+ const wakeAtMs = wakeAt === null ? Number.NaN : Date.parse(wakeAt);
+ if (Number.isNaN(wakeAtMs)) return undefined;
+ // setTimeout delays are signed 32-bit: anything larger overflows and fires
+ // immediately, turning a far-future wake (event-condition snoozes synced
+ // from elsewhere) into a tight re-arm loop.
+ const delayMs = Math.min(Math.max(0, wakeAtMs - Date.now()) + 50, 2_147_483_647);
+ const id = window.setTimeout(onWake, delayMs);
+ return () => window.clearTimeout(id);
+}
+
function canOperateThreads(
threads: ReadonlyArray<Pick<SidebarThreadSummary, "environmentId">>,
): boolean {
@@ -358,14 +785,15 @@ function JumpHintBadge(props: { label: string }) {
// Self-ticking so only this span re-renders each second, not the whole row.
function WorkingDuration(props: { startedAt: string | null }) {
const startedMs = props.startedAt !== null ? Date.parse(props.startedAt) : Number.NaN;
- const [, setTick] = useState(0);
+ // Mount time is close enough for the first frame; the interval keeps it current.
+ const [nowMs, setNowMs] = useState(() => Date.now());
useEffect(() => {
if (Number.isNaN(startedMs)) return;
- const id = window.setInterval(() => setTick((tick) => tick + 1), 1_000);
+ const id = window.setInterval(() => setNowMs(Date.now()), 1_000);
return () => window.clearInterval(id);
}, [startedMs]);
if (Number.isNaN(startedMs)) return null;
- return <span className="tabular-nums">{formatWorkingDurationLabel(Date.now() - startedMs)}</span>;
+ return <span className="tabular-nums">{formatWorkingDurationLabel(nowMs - startedMs)}</span>;
}
function terminalProcessLabel(count: number): string {
@@ -566,10 +994,7 @@ function SnoozeMenuButton(props: {
timestampFormat: TimestampFormat;
}) {
const { open, onOpenChange, onSnooze, timestampFormat } = props;
- const presets = useMemo(
- () => (open ? resolveSnoozePresets(new Date(), timestampFormat) : []),
- [open, timestampFormat],
- );
+ const presets = open ? resolveSnoozePresets(new Date(), timestampFormat) : [];
return (
<Menu open={open} onOpenChange={onOpenChange}>
<Tooltip>
@@ -625,7 +1050,6 @@ type SortableThreadRowBag = Pick<
"listeners" | "setNodeRef" | "transform" | "transition" | "isDragging"
>;
-const NO_DRAGGED_ORDER: readonly string[] = [];
// useSortable memoizes on its `disabled` object, so a fresh one per render
// re-renders every row and marker on each sidebar update.
const SIDEBAR_SORTABLE_DRAG_DISABLED = { draggable: true } as const;
@@ -649,16 +1073,13 @@ function SortableThreadRow(props: {
});
// dnd-kit memoizes each field but not the bag, so the memoized row would
// rerender on every shell update without this.
- const bag = useMemo(
- () => ({
- listeners,
- setNodeRef,
- transform: props.contextDrag ? null : transform,
- transition: props.contextDrag && isDragging ? "transform 150ms ease-out" : transition,
- isDragging: isDragging && !props.contextDrag,
- }),
- [listeners, setNodeRef, transform, transition, isDragging, props.contextDrag],
- );
+ const bag = {
+ listeners,
+ setNodeRef,
+ transform: props.contextDrag ? null : transform,
+ transition: props.contextDrag && isDragging ? "transform 150ms ease-out" : transition,
+ isDragging: isDragging && !props.contextDrag,
+ };
return props.children(bag);
}
@@ -828,39 +1249,30 @@ const SidebarDraftRow = memo(function SidebarDraftRow(props: {
projectDisplayName: props.projectDisplayName,
isActive: props.isActive,
});
- const handleActivate = useCallback(() => onNavigate(draftId), [draftId, onNavigate]);
- const handleKeyDown = useCallback(
- (event: ReactKeyboardEvent) => {
- // Keys targeting the nested discard button belong to the button:
- // preventDefault here would swallow Space's synthesized click and
- // navigate instead of discarding.
- if ((event.target as HTMLElement).closest("button")) return;
- if (event.key === "ContextMenu" || (event.shiftKey && event.key === "F10")) {
- event.preventDefault();
- const rect = event.currentTarget.getBoundingClientRect();
- onContextMenu(draftId, { x: rect.left, y: rect.bottom });
- } else if (event.key === "Enter" || event.key === " ") {
- event.preventDefault();
- onNavigate(draftId);
- }
- },
- [draftId, onContextMenu, onNavigate],
- );
- const handleContextMenu = useCallback(
- (event: ReactMouseEvent) => {
+ const handleActivate = () => onNavigate(draftId);
+ const handleKeyDown = (event: ReactKeyboardEvent) => {
+ // Keys targeting the nested discard button belong to the button:
+ // preventDefault here would swallow Space's synthesized click and
+ // navigate instead of discarding.
+ if ((event.target as HTMLElement).closest("button")) return;
+ if (event.key === "ContextMenu" || (event.shiftKey && event.key === "F10")) {
event.preventDefault();
- onContextMenu(draftId, { x: event.clientX, y: event.clientY });
- },
- [draftId, onContextMenu],
- );
- const handleDiscard = useCallback(
- (event: ReactMouseEvent) => {
+ const rect = event.currentTarget.getBoundingClientRect();
+ onContextMenu(draftId, { x: rect.left, y: rect.bottom });
+ } else if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
- event.stopPropagation();
- onDiscard(draftId);
- },
- [draftId, onDiscard],
- );
+ onNavigate(draftId);
+ }
+ };
+ const handleContextMenu = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ onContextMenu(draftId, { x: event.clientX, y: event.clientY });
+ };
+ const handleDiscard = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ event.stopPropagation();
+ onDiscard(draftId);
+ };
return (
<li className="list-none py-0.5">
<div
@@ -938,9 +1350,8 @@ const SidebarDraftBlock = memo(function SidebarDraftBlock(props: {
let row: SidebarDraftRowData | null = null;
if (props.routeDraftId !== null) {
const draftId = DraftId.make(props.routeDraftId);
- const store = useComposerDraftStore.getState();
- const session = store.getDraftSession(draftId);
- const composer = store.getComposerDraft(draftId);
+ const session = draftThreadsByThreadKey[draftId];
+ const composer = draftsByThreadKey[draftId];
row =
session && session.promotedTo == null && composer && composerDraftHasUserContent(composer)
? { draftId, session, composer }
@@ -948,7 +1359,7 @@ const SidebarDraftBlock = memo(function SidebarDraftBlock(props: {
}
setFrozenActive({ routeDraftId: props.routeDraftId, row });
}
- const drafts = useMemo(() => {
+ const drafts = (() => {
const rows: SidebarDraftRowData[] = [];
for (const [draftKey, session] of Object.entries(draftThreadsByThreadKey)) {
if (session.promotedTo != null) {
@@ -974,13 +1385,7 @@ const SidebarDraftBlock = memo(function SidebarDraftBlock(props: {
}
rows.sort((left, right) => right.session.createdAt.localeCompare(left.session.createdAt));
return rows;
- }, [
- draftThreadsByThreadKey,
- draftsByThreadKey,
- frozenActive,
- props.routeDraftId,
- props.scopedProjectKeys,
- ]);
+ })();
if (drafts.length === 0) {
return null;
}
@@ -1090,16 +1495,9 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
onUnpin: (threadRef: ScopedThreadRef) => void;
onAcknowledgeWoke: (threadRef: ScopedThreadRef, visitedAt: string) => void;
onFileDropThreads?: ((threadRef: ScopedThreadRef, files: File[]) => void) | undefined;
- changeRequestSnapshot: ThreadChangeRequestSnapshot | null;
- onChangeRequestSnapshot: (
- threadKey: string,
- snapshot: ThreadChangeRequestSnapshot | null,
- ) => void;
}) {
const {
isRenaming,
- changeRequestSnapshot,
- onChangeRequestSnapshot,
onCancelRename,
onCommitRename,
onContextMenu,
@@ -1121,10 +1519,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
variant,
variantAction,
} = props;
- const threadRef = useMemo(
- () => scopeThreadRef(thread.environmentId, thread.id),
- [thread.environmentId, thread.id],
- );
+ const threadRef = scopeThreadRef(thread.environmentId, thread.id);
const threadKey = scopedThreadKey(threadRef);
const canOperateThread = useEnvironmentScope(thread.environmentId, AuthOrchestrationOperateScope);
useEffect(() => {
@@ -1143,14 +1538,11 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
const terminalProcessCount = runningTerminalIds.length;
const hasUnsentDraft = useThreadHasUnsentDraft(threadRef) && !props.isActive;
- const handleDiscardDraftClick = useCallback(
- (event: ReactMouseEvent) => {
- event.preventDefault();
- event.stopPropagation();
- discardComposerDraft(threadRef);
- },
- [threadRef],
- );
+ const handleDiscardDraftClick = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ event.stopPropagation();
+ discardComposerDraft(threadRef);
+ };
const gitCwd = thread.worktreePath ?? props.project?.workspaceRoot ?? null;
const linkedPullRequestStatus = useLinkedThreadPullRequest(
@@ -1180,8 +1572,6 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
const isUnread = hasUnseenCompletion({ ...thread, lastVisitedAt });
const status = resolveSidebarThreadStatus(thread);
- const isInFlight =
- status === "working" || status === "waiting" || status === "approval" || status === "input";
const lastVisitedDate = lastVisitedAt === undefined ? null : parseTimestampDate(lastVisitedAt);
const wokeAtDate = props.wokeAt === null ? null : parseTimestampDate(props.wokeAt);
const isWoke =
@@ -1288,69 +1678,50 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
/>
);
- const handleClick = useCallback(
- (event: ReactMouseEvent) => {
- onThreadClick(event, threadRef);
- },
- [onThreadClick, threadRef],
- );
- const handleAcknowledgeWokeClick = useCallback(
- (event: ReactMouseEvent) => {
- event.preventDefault();
- event.stopPropagation();
- if (props.wokeAt === null) return;
- onAcknowledgeWoke(threadRef, props.wokeAt);
- },
- [onAcknowledgeWoke, props.wokeAt, threadRef],
- );
- const handleContextMenu = useCallback(
- (event: ReactMouseEvent) => {
- event.preventDefault();
- onContextMenu(threadRef, { x: event.clientX, y: event.clientY });
- },
- [onContextMenu, threadRef],
- );
- const handleKeyDown = useCallback(
- (event: ReactKeyboardEvent) => {
- if (event.target !== event.currentTarget) return;
- if (event.key !== "Enter" && event.key !== " ") return;
- event.preventDefault();
- onThreadActivate(threadRef);
- },
- [onThreadActivate, threadRef],
- );
- const handleDoubleClick = useCallback(
- (event: ReactMouseEvent) => {
- if (
- !readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope) ||
- isRenaming ||
- event.metaKey ||
- event.ctrlKey ||
- event.shiftKey ||
- event.altKey
- ) {
- return;
- }
- if ((event.target as HTMLElement).closest("button, a, input")) return;
- event.preventDefault();
- onStartRename(threadRef, thread.title);
- },
- [isRenaming, onStartRename, thread.title, threadRef],
- );
+ const handleClick = (event: ReactMouseEvent) => {
+ onThreadClick(event, threadRef);
+ };
+ const handleAcknowledgeWokeClick = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ event.stopPropagation();
+ if (props.wokeAt === null) return;
+ onAcknowledgeWoke(threadRef, props.wokeAt);
+ };
+ const handleContextMenu = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ onContextMenu(threadRef, { x: event.clientX, y: event.clientY });
+ };
+ const handleKeyDown = (event: ReactKeyboardEvent) => {
+ if (event.target !== event.currentTarget) return;
+ if (event.key !== "Enter" && event.key !== " ") return;
+ event.preventDefault();
+ onThreadActivate(threadRef);
+ };
+ const handleDoubleClick = (event: ReactMouseEvent) => {
+ if (
+ !readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope) ||
+ isRenaming ||
+ event.metaKey ||
+ event.ctrlKey ||
+ event.shiftKey ||
+ event.altKey
+ ) {
+ return;
+ }
+ if ((event.target as HTMLElement).closest("button, a, input")) return;
+ event.preventDefault();
+ onStartRename(threadRef, thread.title);
+ };
const [isFileDragOver, setIsFileDragOver] = useState(false);
- const fileDropHandlers = useMemo(
- () =>
- onFileDropThreads
- ? makeWorkspaceFileDropHandlers({
- setDragActive: setIsFileDragOver,
- addFiles: (files) => {
- onFileDropThreads(threadRef, files);
- },
- addFolders: () => {},
- })
- : null,
- [onFileDropThreads, threadRef],
- );
+ const fileDropHandlers = onFileDropThreads
+ ? makeWorkspaceFileDropHandlers({
+ setDragActive: setIsFileDragOver,
+ addFiles: (files) => {
+ onFileDropThreads(threadRef, files);
+ },
+ addFolders: () => {},
+ })
+ : null;
useEffect(() => {
if (!isFileDragOver) return;
const clearFileDrag = () => setIsFileDragOver(false);
@@ -1361,104 +1732,69 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
useEffect(() => {
if (isRenaming) renameCommittedRef.current = false;
}, [isRenaming]);
- const handleRenameKeyDown = useCallback(
- (event: ReactKeyboardEvent<HTMLInputElement>) => {
- event.stopPropagation();
- if (event.nativeEvent.isComposing || event.keyCode === 229) return;
- if (event.key === "Enter") {
- event.preventDefault();
- renameCommittedRef.current = true;
- onCommitRename(threadRef, renamingTitle, thread.title);
- } else if (event.key === "Escape") {
- event.preventDefault();
- renameCommittedRef.current = true;
- onCancelRename();
- }
- },
- [onCancelRename, onCommitRename, renamingTitle, thread.title, threadRef],
- );
- const handleRenameBlur = useCallback(() => {
+ const handleRenameKeyDown = (event: ReactKeyboardEvent<HTMLInputElement>) => {
+ event.stopPropagation();
+ if (event.nativeEvent.isComposing || event.keyCode === 229) return;
+ if (event.key === "Enter") {
+ event.preventDefault();
+ renameCommittedRef.current = true;
+ onCommitRename(threadRef, renamingTitle, thread.title);
+ } else if (event.key === "Escape") {
+ event.preventDefault();
+ renameCommittedRef.current = true;
+ onCancelRename();
+ }
+ };
+ const handleRenameBlur = () => {
if (!renameCommittedRef.current) {
onCommitRename(threadRef, renamingTitle, thread.title);
}
- }, [onCommitRename, renamingTitle, thread.title, threadRef]);
- const handleSettleClick = useCallback(
- (event: ReactMouseEvent) => {
- event.preventDefault();
- event.stopPropagation();
- onSettle(threadRef);
- },
- [onSettle, threadRef],
- );
- const handleActionPointerDown = useCallback(
- (event: ReactPointerEvent) => {
- if (!event.isPrimary || event.button !== 0) return;
- event.stopPropagation();
- onActionSweepStart(threadRef, variantAction, event.nativeEvent);
- },
- [onActionSweepStart, threadRef, variantAction],
- );
- const handleUnsettleClick = useCallback(
- (event: ReactMouseEvent) => {
- event.preventDefault();
- event.stopPropagation();
- onUnsettle(threadRef);
- },
- [onUnsettle, threadRef],
- );
- const handleUnsnoozeClick = useCallback(
- (event: ReactMouseEvent) => {
- event.preventDefault();
- event.stopPropagation();
- onUnsnooze(threadRef);
- },
- [onUnsnooze, threadRef],
- );
- const handleUnpinClick = useCallback(
- (event: ReactMouseEvent) => {
- event.preventDefault();
- event.stopPropagation();
- onUnpin(threadRef);
- },
- [onUnpin, threadRef],
- );
- const handleSnoozePreset = useCallback(
- (preset: Pick<SnoozePreset, "snoozedUntil">) => {
- onSnooze(threadRef, preset);
- },
- [onSnooze, threadRef],
- );
+ };
+ const handleSettleClick = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ event.stopPropagation();
+ onSettle(threadRef);
+ };
+ const handleActionPointerDown = (event: ReactPointerEvent) => {
+ if (!event.isPrimary || event.button !== 0) return;
+ event.stopPropagation();
+ onActionSweepStart(threadRef, variantAction, event.nativeEvent);
+ };
+ const handleUnsettleClick = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ event.stopPropagation();
+ onUnsettle(threadRef);
+ };
+ const handleUnsnoozeClick = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ event.stopPropagation();
+ onUnsnooze(threadRef);
+ };
+ const handleUnpinClick = (event: ReactMouseEvent) => {
+ event.preventDefault();
+ event.stopPropagation();
+ onUnpin(threadRef);
+ };
+ const handleSnoozePreset = (preset: Pick<SnoozePreset, "snoozedUntil">) => {
+ onSnooze(threadRef, preset);
+ };
const [snoozeMenuOpenRaw, setSnoozeMenuOpen] = useState(false);
const showSnoozeButton =
canOperateThread &&
props.snoozeSupported &&
canSnooze(thread, { now: new Date().toISOString() });
- const showHoverActions =
- (canOperateThread && props.settlementSupported) || showSnoozeButton || hasUnsentDraft;
+ // Closing here, not just masking, keeps the menu from reopening when the button returns.
+ if (snoozeMenuOpenRaw && !showSnoozeButton) setSnoozeMenuOpen(false);
const snoozeMenuOpen = snoozeMenuOpenRaw && showSnoozeButton;
- useEffect(() => {
- if (!showSnoozeButton) setSnoozeMenuOpen(false);
- }, [showSnoozeButton]);
- const handlePrClick = useCallback(
- (event: ReactMouseEvent<HTMLElement>, targetUrl?: string) => {
- const url = targetUrl ?? pr?.url ?? currentLinkedPr?.url;
- if (!url) return;
- const showInRightPanel = targetUrl !== undefined || openPullRequestsInRightPanel;
- const openedInRightPanel = openPrLink(event, url, showInRightPanel ? threadRef : undefined);
- if (openedInRightPanel && showInRightPanel && !props.isActive) {
- onThreadActivate(threadRef);
- }
- },
- [
- onThreadActivate,
- openPrLink,
- openPullRequestsInRightPanel,
- pr,
- currentLinkedPr,
- props.isActive,
- threadRef,
- ],
- );
+ const handlePrClick = (event: ReactMouseEvent<HTMLElement>, targetUrl?: string) => {
+ const url = targetUrl ?? pr?.url ?? currentLinkedPr?.url;
+ if (!url) return;
+ const showInRightPanel = targetUrl !== undefined || openPullRequestsInRightPanel;
+ const openedInRightPanel = openPrLink(event, url, showInRightPanel ? threadRef : undefined);
+ if (openedInRightPanel && showInRightPanel && !props.isActive) {
+ onThreadActivate(threadRef);
+ }
+ };
const rowSurfaceClassName = cn(
"group/sidebar-row relative w-full cursor-pointer overflow-hidden rounded-md text-left outline-none select-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
@@ -1571,10 +1907,10 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
const prBadgeShape = supportsMultiplePullRequests
? resolveThreadPullRequestBadge(thread.pullRequests)
: null;
- const handlePrListClick = useCallback(() => {
+ const handlePrListClick = () => {
useRightPanelStore.getState().open(threadRef, "pull-requests");
if (!props.isActive) onThreadActivate(threadRef);
- }, [onThreadActivate, props.isActive, threadRef]);
+ };
const prBadge =
prBadgeShape?.kind === "stack" || pr || currentLinkedPr ? (
<ThreadPullRequestBadgeControl
@@ -2093,10 +2429,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
projectDisplayName: props.projectDisplayName,
isActive: props.isRouteActive,
});
- const threadRef = useMemo(
- () => scopeThreadRef(thread.environmentId, thread.id),
- [thread.environmentId, thread.id],
- );
+ const threadRef = scopeThreadRef(thread.environmentId, thread.id);
const { leaseLiveStatus, rowRef } = useSidebarRowSubscriptionLease(
props.isHighlighted || props.isRouteActive,
);
@@ -2136,17 +2469,14 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
});
const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
const [isFileDragOver, setIsFileDragOver] = useState(false);
- const fileDropHandlers = useMemo(
- () =>
- makeWorkspaceFileDropHandlers({
- setDragActive: setIsFileDragOver,
- addFiles: (files) => {
- props.onFileDropThreads(threadRef, files);
- },
- addFolders: () => {},
- }),
- [props.onFileDropThreads, threadRef],
- );
+ const onFileDropThreads = props.onFileDropThreads;
+ const fileDropHandlers = makeWorkspaceFileDropHandlers({
+ setDragActive: setIsFileDragOver,
+ addFiles: (files) => {
+ onFileDropThreads(threadRef, files);
+ },
+ addFolders: () => {},
+ });
useEffect(() => {
if (!isFileDragOver) return;
const clearFileDrag = () => setIsFileDragOver(false);
@@ -2161,6 +2491,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
<button
ref={rowRef}
id={props.resultId}
+ data-search-result-key={scopedThreadKey(threadRef)}
type="button"
role="option"
tabIndex={-1}
@@ -2251,15 +2582,23 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
});
export default function Sidebar() {
- const projects = useProjects();
- const threads = useThreadShells();
+ const {
+ projects,
+ serverConfigs,
+ projectGroups,
+ environmentLabelById,
+ providerEntriesByEnvironment,
+ projectByKey,
+ projectDisplayNameByKey,
+ projectGroupByScopeKey,
+ projectScopeItems,
+ } = useSidebarLookups();
const router = useRouter();
const { isMobile, setOpenMobile } = useSidebar();
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
const confirmThreadDelete = useClientSettings((s) => s.confirmThreadDelete);
const confirmThreadArchive = useClientSettings((s) => s.confirmThreadArchive);
const timestampFormat = useClientSettings((s) => s.timestampFormat);
- const workingShelfEnabled = useClientSettings((s) => s.sidebarWorkingShelfEnabled);
const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
const {
settleThread,
@@ -2335,12 +2674,6 @@ export default function Sidebar() {
},
});
const newThreadContext = useHandleNewThread();
- const openAddProjectCommandPalette = useCallback(
- () => openCommandPalette({ open: "add-project" }),
- [],
- );
- const environments = useEnvironmentIdentities();
- const serverConfigs = useAtomValue(environmentServerConfigsAtom);
const primaryEnvironmentId = usePrimaryEnvironmentId();
const clearSelection = useThreadSelectionStore((s) => s.clearSelection);
const setSelectionAnchor = useThreadSelectionStore((s) => s.setAnchor);
@@ -2354,86 +2687,74 @@ export default function Sidebar() {
const routeDraftThread = useComposerDraftStore((store) =>
routeTarget?.kind === "draft" ? store.getDraftSession(routeTarget.draftId) : null,
);
- const routeThreadRef = useMemo(
- () => resolveActiveThreadRouteRef(routeTarget, routeDraftThread),
- [routeDraftThread, routeTarget],
- );
- const routeThreadKey = routeThreadRef ? scopedThreadKey(routeThreadRef) : null;
- const routeTargetRef = useRef(routeTarget);
- routeTargetRef.current = routeTarget;
- const routeThreadKeyRef = useRef(routeThreadKey);
- routeThreadKeyRef.current = routeThreadKey;
-
- const environmentLabelById = useMemo(
- () =>
- new Map(
- environments.map((environment) => [environment.environmentId, environment.label] as const),
- ),
- [environments],
- );
const environmentMachineById = useEnvironmentMachines();
- const projectGroups = useSidebarProjectGroups();
const projectGroupsRef = useRef(projectGroups);
- projectGroupsRef.current = projectGroups;
- // Threads on non-primary environments (T3 Connect, hosted) resolve their
- // provider entry from their own environment's config: default instance ids
- // are driver slugs, so a flat map would collide across environments.
- const providerEntriesByEnvironment = useMemo(
- () =>
- deriveProviderEntriesByEnvironment(
- [...serverConfigs].map(
- ([environmentId, config]) => [environmentId, config.providers, config.settings] as const,
- ),
- ),
- [serverConfigs],
- );
- const projectByKey = useMemo(
- () => new Map(projects.map((project) => [`${project.environmentId}:${project.id}`, project])),
- [projects],
- );
- const projectDisplayNameByKey = useMemo(
- () =>
- new Map(
- projectGroups.flatMap((group) =>
- group.memberProjects.map(
- (project) => [`${project.environmentId}:${project.id}`, group.displayName] as const,
- ),
- ),
- ),
- [projectGroups],
- );
-
- const nowMinute = useNowMinute();
- const changeRequestSnapshotByKey = useAtomValue(threadChangeRequestSnapshotsAtom);
- // Snooze wake times are second-precise, so classifying with the quantized
- // minute would hold a woken thread on the shelf for up to a minute.
- const [snoozeWakeTick, bumpSnoozeWakeTick] = useState(0);
-
const persistedProjectScopeKey = useUiStateStore((store) => store.sidebarProjectScopeKey);
- const setProjectScopeKey = useUiStateStore((store) => store.setSidebarProjectScopeKey);
- const projectScopeKey = useMemo(
- () =>
- resolveSidebarProjectScopeKey({
- groups: projectGroups,
- key: persistedProjectScopeKey,
- settings: projectGroupingSettings,
- }),
- [persistedProjectScopeKey, projectGroups, projectGroupingSettings],
- );
- const projectScopeItems = useMemo(
- () => [
- { value: "all", label: "All projects" },
- ...projectGroups.map((project) => ({
- value: project.projectKey,
- label: project.displayName,
- })),
- ],
- [projectGroups],
- );
- const showProjectEnvironments = useMemo(
- () => projectGroupsSpanEnvironments(projectGroups),
- [projectGroups],
+ const setPersistedProjectScopeKey = useUiStateStore((store) => store.setSidebarProjectScopeKey);
+ const [projectScopeMenuState, dispatchProjectScopeMenu] = useReducer(
+ reduceSidebarProjectScopeMenuState,
+ { open: false, query: "" },
);
+ const projectScopeFilter = useComboboxFilter();
+ const allProjectSnapshotsReady = useAllEnvironmentProjectSnapshotsReady();
+ const allProjectConfigsReady = useAtomValue(environmentProjects.projectConfigsReadyAtom);
+ const headerSearchRef = useRef<HTMLDivElement | null>(null);
+ // Safari can send a click after Ctrl+click opens settings. Ignore that one
+ // selection, then clear the guard when the picker opens again.
+ const suppressNextScopeChangeRef = useRef(false);
+ const highlightedProjectScopeKeyRef = useRef<string | null>(null);
+ // Keep a dropped row at its destination while its server applies the
+ // lifecycle command and any order-key writes. The next pickup waits for
+ // this hold so a second drop cannot replace an unconfirmed placement.
+ const [optimisticDrop, setOptimisticDrop] = useState<SidebarOptimisticDrop | null>(null);
+ const threadSearchInputRef = useRef<HTMLInputElement>(null);
+ const [threadSearchQuery, setThreadSearchQuery] = useState("");
+ const [searchHighlight, setSearchHighlight] = useState({ orderKey: "", index: 0 });
+ const searchEnvironmentIds = useConnectedEnvironmentIds();
+ const threadSearch = useThreadSearch(searchEnvironmentIds, threadSearchQuery);
+ const handleNewThreadRef = useRef(newThreadContext.handleNewThread);
+ const queuePendingFileDrop = useSidebarPendingFileDropStore((s) => s.queuePendingFileDrop);
+ const clearPendingFileDrop = useSidebarPendingFileDropStore((s) => s.clearPendingFileDrop);
+ const [renamingThreadKey, setRenamingThreadKey] = useState<string | null>(null);
+ const [renamingTitle, setRenamingTitle] = useState("");
+ const settlingThreadKeysRef = useRef(new Set<string>());
+ const threadListRef = useRef<HTMLUListElement | null>(null);
+ const dragLabelOffsetRef = useRef(0);
+ const listMotionRef = useRef<ReturnType<typeof createSidebarListMotion> | null>(null);
+ const [dragState, setDragState] = useState<{
+ readonly activeKey: string;
+ readonly activeSection: SidebarSection;
+ readonly occurredAt: string;
+ readonly activationY: number | null;
+ readonly targetSection: SidebarSection | null;
+ readonly contextDrag: boolean;
+ } | null>(null);
+ const dragSensorRef = useRef<SidebarPointerSensor | null>(null);
+ const contextDragKeyRef = useRef<string | null>(null);
+ const [actionSweep, setActionSweep] = useState<{
+ action: SidebarSweepAction;
+ keys: ReadonlySet<string>;
+ } | null>(null);
+ const snoozingThreadKeysRef = useRef(new Set<string>());
+ const removeFromSelection = useThreadSelectionStore((s) => s.removeFromSelection);
+ const shortcutModifiers = useShortcutModifierState();
+ const terminalFocused = useTerminalFocus();
+ const openAddProjectCommandPalette = () => openCommandPalette({ open: "add-project" });
+ const routeThreadRef = resolveActiveThreadRouteRef(routeTarget, routeDraftThread);
+ const routeThreadKey = routeThreadRef ? scopedThreadKey(routeThreadRef) : null;
+ const routeThreadKeyRef = useRef(routeThreadKey);
+
+ // A selection made under one scope can hold threads the next scope hides.
+ const setProjectScopeKey = (key: string | null) => {
+ clearSelection();
+ setPersistedProjectScopeKey(key);
+ };
+ const projectScopeKey = resolveSidebarProjectScopeKey({
+ groups: projectGroups,
+ key: persistedProjectScopeKey,
+ settings: projectGroupingSettings,
+ });
+ const showProjectEnvironments = projectGroupsSpanEnvironments(projectGroups);
const scratchMachineLabelFor = (
thread: Pick<SidebarThreadSummary, "environmentId" | "projectId">,
) => {
@@ -2447,51 +2768,27 @@ export default function Sidebar() {
}
return environmentLabelById.get(thread.environmentId) ?? null;
};
- const projectGroupByScopeKey = useMemo(
- () => new Map(projectGroups.map((project) => [project.projectKey, project] as const)),
- [projectGroups],
- );
- const selectedProjectScopeItem = useMemo(
- () =>
- projectScopeItems.find((item) => item.value === (projectScopeKey ?? "all")) ??
- projectScopeItems[0]!,
- [projectScopeItems, projectScopeKey],
- );
- const [projectScopeMenuState, dispatchProjectScopeMenu] = useReducer(
- reduceSidebarProjectScopeMenuState,
- { open: false, query: "" },
- );
- const projectScopeFilter = useComboboxFilter();
- const filteredProjectScopeItems = useMemo(
- () =>
- filterSidebarProjectScopeItems({
- items: projectScopeItems,
- query: projectScopeMenuState.query,
- matches: (item, query) =>
- projectScopeFilter.contains(item, query, (candidate) => candidate.label),
- }),
- [projectScopeFilter, projectScopeItems, projectScopeMenuState.query],
- );
- const scopedProjectGroup = useMemo(
- () =>
- projectScopeKey === null
- ? null
- : (projectGroups.find((project) => project.projectKey === projectScopeKey) ?? null),
- [projectGroups, projectScopeKey],
- );
- const scopedProjectKeys = useMemo(
- () =>
- scopedProjectGroup === null
- ? null
- : new Set(
- scopedProjectGroup.memberProjectRefs.map(
- (projectRef) => `${projectRef.environmentId}:${projectRef.projectId}`,
- ),
+ const selectedProjectScopeItem =
+ projectScopeItems.find((item) => item.value === (projectScopeKey ?? "all")) ??
+ projectScopeItems[0]!;
+ const filteredProjectScopeItems = filterSidebarProjectScopeItems({
+ items: projectScopeItems,
+ query: projectScopeMenuState.query,
+ matches: (item, query) =>
+ projectScopeFilter.contains(item, query, (candidate) => candidate.label),
+ });
+ const scopedProjectGroup =
+ projectScopeKey === null
+ ? null
+ : (projectGroups.find((project) => project.projectKey === projectScopeKey) ?? null);
+ const scopedProjectKeys =
+ scopedProjectGroup === null
+ ? null
+ : new Set(
+ scopedProjectGroup.memberProjectRefs.map(
+ (projectRef) => `${projectRef.environmentId}:${projectRef.projectId}`,
),
- [scopedProjectGroup],
- );
- const allProjectSnapshotsReady = useAllEnvironmentProjectSnapshotsReady();
- const allProjectConfigsReady = useAtomValue(environmentProjects.projectConfigsReadyAtom);
+ );
useEffect(() => {
const nextKey = resolveSidebarProjectScopeKey({
groups: projectGroups,
@@ -2500,15 +2797,17 @@ export default function Sidebar() {
canClearMissingScope: allProjectSnapshotsReady && allProjectConfigsReady,
});
if (nextKey !== persistedProjectScopeKey) {
- setProjectScopeKey(nextKey);
+ clearSelection();
+ setPersistedProjectScopeKey(nextKey);
}
}, [
allProjectConfigsReady,
allProjectSnapshotsReady,
+ clearSelection,
persistedProjectScopeKey,
projectGroups,
projectGroupingSettings,
- setProjectScopeKey,
+ setPersistedProjectScopeKey,
]);
const routeDraftIdForRows = routeTarget?.kind === "draft" ? routeTarget.draftId : null;
const visibleDraftSessionCount = useComposerDraftStore((store) => {
@@ -2530,9 +2829,6 @@ export default function Sidebar() {
}
return count;
});
- useEffect(() => {
- clearSelection();
- }, [clearSelection, projectScopeKey]);
const openProjectSettings = useStableCallback(
(projectGroup: SidebarProjectSnapshot, projectRef?: ScopedProjectRef) => {
@@ -2552,11 +2848,6 @@ export default function Sidebar() {
});
},
);
- const headerSearchRef = useRef<HTMLDivElement | null>(null);
- // Safari can send a click after Ctrl+click opens settings. Ignore that one
- // selection, then clear the guard when the picker opens again.
- const suppressNextScopeChangeRef = useRef(false);
- const highlightedProjectScopeKeyRef = useRef<string | null>(null);
const handleProjectSettings = useStableCallback(
(
event: ReactMouseEvent<HTMLElement> | ReactKeyboardEvent<HTMLInputElement>,
@@ -2570,20 +2861,9 @@ export default function Sidebar() {
},
);
- // Keep a dropped row at its destination while its server applies the
- // lifecycle command and any order-key writes. The next pickup waits for
- // this hold so a second drop cannot replace an unconfirmed placement.
- const [optimisticDrop, setOptimisticDrop] = useState<{
- readonly key: string;
- readonly sourceSection: SidebarSection;
- readonly section: "pinned" | "active" | "settled";
- readonly occurredAt: string;
- readonly clearsSnooze: boolean;
- readonly order: readonly string[] | null;
- readonly keysAtDrop: ReadonlyMap<string, string | null>;
- readonly assignedKeys: ReadonlyMap<string, string>;
- } | null>(null);
const {
+ threads,
+ workingShelfEnabled,
pinnedThreads,
draggableThreadKeys,
activeReorderableThreadKeys,
@@ -2592,315 +2872,83 @@ export default function Sidebar() {
snoozedThreads,
settledThreads,
snoozeNow,
- } = useMemo(() => {
- void snoozeWakeTick;
- const preciseNow = new Date().toISOString();
- const visible = filterSidebarV2VisibleThreads(threads, scopedProjectKeys);
- inboxReturns.observe(workingShelfEnabled ? threads : null);
- const pinned: EnvironmentThreadShell[] = [];
- const active: EnvironmentThreadShell[] = [];
- const working: EnvironmentThreadShell[] = [];
- const inbox = (thread: EnvironmentThreadShell) =>
- workingShelfEnabled && isSidebarThreadWorking(thread) ? working : active;
- const snoozed: EnvironmentThreadShell[] = [];
- const settled: EnvironmentThreadShell[] = [];
- const draggable = new Set<string>();
- const activeReorderable = new Set<string>();
- for (const thread of visible) {
- const capabilities = serverConfigs.get(thread.environmentId)?.environment.capabilities;
- const supportsSettlement = capabilities?.threadSettlement === true;
- const supportsSnooze = capabilities?.threadSnooze === true;
- const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
- if (capabilities?.threadActiveReorder === true) activeReorderable.add(threadKey);
- if (capabilities?.threadPinning === true && capabilities.threadPinReorder === true) {
- draggable.add(threadKey);
- }
- if (optimisticDrop?.key === threadKey) {
- const projected = applySidebarThreadDrop(
- thread,
- optimisticDrop.section,
- optimisticDrop.occurredAt,
- optimisticDrop.assignedKeys.get(threadKey),
- );
- (optimisticDrop.section === "pinned"
- ? pinned
- : optimisticDrop.section === "settled"
- ? settled
- : inbox(projected)
- ).push(
- optimisticDrop.clearsSnooze
- ? projected
- : { ...projected, snoozedAt: thread.snoozedAt, snoozedUntil: thread.snoozedUntil },
- );
- } else {
- const section = resolveSidebarThreadSection({
- snoozed: supportsSnooze && effectiveSnoozed(thread, { now: preciseNow }),
- settled: supportsSettlement && thread.settledOverride === "settled",
- pinned: thread.pinnedAt != null,
- });
- (section === "snoozed"
- ? snoozed
- : section === "settled"
- ? settled
- : section === "pinned"
- ? pinned
- : inbox(thread)
- ).push(thread);
- }
- }
- // Server capability only gates DRAGGING — it must not influence the
- // sort, or mixed-version fleets would render different pinned orders on
- // web and mobile from the same data.
- const sortedPinned = sortPinnedThreadsForSidebar(pinned);
- const sortedActive = workingShelfEnabled
- ? sortInboxThreadsByReturn(active, inboxReturns.returnedAt)
- : sortThreadsForSidebar(active);
- return {
- pinnedThreads:
- optimisticDrop?.section !== "pinned" || optimisticDrop.order === null
- ? sortedPinned
- : orderItemsByPreferredIds({
- items: sortedPinned,
- preferredIds: optimisticDrop.order,
- getId: (thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- }),
- draggableThreadKeys: draggable,
- activeReorderableThreadKeys: activeReorderable,
- activeThreads:
- optimisticDrop?.section !== "active" || optimisticDrop.order === null
- ? sortedActive
- : orderItemsByPreferredIds({
- items: sortedActive,
- preferredIds: optimisticDrop.order,
- getId: (thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- }),
- workingThreads: sortWorkingThreadsBySend(working),
- snoozedThreads: snoozed.toSorted(
- (left, right) =>
- firstValidTimestampMs(left.snoozedUntil ?? null) -
- firstValidTimestampMs(right.snoozedUntil ?? null),
- ),
- settledThreads: sortSettledThreads(settled),
- snoozeNow: preciseNow,
- };
- }, [
- nowMinute,
- optimisticDrop,
+ searchableThreads,
+ settledVisibleCount,
+ hiddenSettledCount,
+ showMoreSettled,
+ settledShelfExpanded,
+ toggleSettledShelf,
+ snoozedShelfExpanded,
+ toggleSnoozedShelf,
+ workingShelfExpanded,
+ toggleWorkingShelf,
+ renderedSettledThreads,
+ visibleSnoozedThreads,
+ visibleWorkingThreads,
+ orderedThreadKeys,
+ threadByKey,
+ settledThreadKeys,
+ snoozedThreadKeys,
+ sectionByThreadKey,
+ pinnedKeys,
+ activeKeys,
+ pinnedKeysById,
+ activeKeysById,
+ } = useSidebarThreads({
+ projectScopeKey,
scopedProjectKeys,
serverConfigs,
- snoozeWakeTick,
- threads,
- workingShelfEnabled,
- ]);
+ optimisticDrop,
+ routeThreadKey,
+ });
- const threadSearchInputRef = useRef<HTMLInputElement>(null);
- const [threadSearchQuery, setThreadSearchQuery] = useState("");
- const [activeSearchResultIndex, setActiveSearchResultIndex] = useState(0);
const isSearchingThreads = threadSearchQuery.trim().length > 0;
- const searchableThreads = useMemo(
- () => [
- ...pinnedThreads,
- ...activeThreads,
- ...workingThreads,
- ...snoozedThreads,
- ...settledThreads,
- ],
- [activeThreads, pinnedThreads, settledThreads, snoozedThreads, workingThreads],
- );
- const searchEnvironmentIds = useConnectedEnvironmentIds();
- const threadSearch = useThreadSearch(searchEnvironmentIds, threadSearchQuery);
- const threadSearchMatchByKey = useMemo(
- () =>
- new Map(threadSearch.matches.map((match) => [threadSearchMatchKey(match), match] as const)),
- [threadSearch.matches],
+ const threadSearchMatchByKey = new Map(
+ threadSearch.matches.map((match) => [threadSearchMatchKey(match), match] as const),
);
- const threadSearchResults = useMemo(
- () =>
- searchSidebarThreads(
- searchableThreads,
- threadSearchQuery,
- new Set(threadSearchMatchByKey.keys()),
- ),
- [searchableThreads, threadSearchQuery, threadSearchMatchByKey],
+ const threadSearchResults = searchSidebarThreads(
+ searchableThreads,
+ threadSearchQuery,
+ new Set(threadSearchMatchByKey.keys()),
);
const threadSearchResultOrderKey = threadSearchResults
.map((thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)))
.join("\0");
+ // The highlight belongs to one result list; a new list starts at the top.
+ const activeSearchResultIndex =
+ searchHighlight.orderKey === threadSearchResultOrderKey ? searchHighlight.index : 0;
+ const setActiveSearchResultIndex = (index: number | ((index: number) => number)) =>
+ setSearchHighlight((current) => {
+ const currentIndex = current.orderKey === threadSearchResultOrderKey ? current.index : 0;
+ return {
+ orderKey: threadSearchResultOrderKey,
+ index: typeof index === "function" ? index(currentIndex) : index,
+ };
+ });
+ const activeSearchResult = isSearchingThreads
+ ? threadSearchResults[activeSearchResultIndex]
+ : undefined;
+ const activeSearchResultKey =
+ activeSearchResult === undefined
+ ? null
+ : scopedThreadKey(scopeThreadRef(activeSearchResult.environmentId, activeSearchResult.id));
useEffect(() => {
- setActiveSearchResultIndex(0);
- }, [threadSearchResultOrderKey]);
-
- useEffect(() => {
- if (!isSearchingThreads) return;
+ if (activeSearchResultKey === null) return;
document
- .getElementById(`sidebar-thread-search-result-${activeSearchResultIndex}`)
+ .querySelector(`[data-search-result-key="${globalThis.CSS.escape(activeSearchResultKey)}"]`)
?.scrollIntoView({ block: "nearest" });
- }, [activeSearchResultIndex, isSearchingThreads, threadSearchResultOrderKey]);
-
- useEffect(() => {
- const nextWakeAtMs =
- snoozedThreads.length > 0 && snoozedThreads[0]?.snoozedUntil != null
- ? Date.parse(snoozedThreads[0].snoozedUntil)
- : Number.NaN;
- if (Number.isNaN(nextWakeAtMs)) return;
- // setTimeout delays are signed 32-bit: anything larger overflows and fires
- // immediately, turning a far-future wake (event-condition snoozes synced
- // from elsewhere) into a tight re-arm loop.
- const delayMs = Math.min(Math.max(0, nextWakeAtMs - Date.now()) + 50, 2_147_483_647);
- const id = window.setTimeout(() => bumpSnoozeWakeTick((tick) => tick + 1), delayMs);
- return () => window.clearTimeout(id);
- }, [snoozedThreads]);
-
- const [settledVisibleCount, setSettledVisibleCount] = useState(SETTLED_TAIL_INITIAL_COUNT);
- const settledResetKey = projectScopeKey ?? "all";
- const lastSettledResetKeyRef = useRef(settledResetKey);
- if (lastSettledResetKeyRef.current !== settledResetKey) {
- lastSettledResetKeyRef.current = settledResetKey;
- setSettledVisibleCount(SETTLED_TAIL_INITIAL_COUNT);
- }
- const visibleSettledThreads = useMemo(() => {
- if (settledThreads.length <= settledVisibleCount) return settledThreads;
- const visible = settledThreads.slice(0, settledVisibleCount);
- if (routeThreadKey !== null) {
- const routeThread = settledThreads
- .slice(settledVisibleCount)
- .find(
- (thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)) === routeThreadKey,
- );
- if (routeThread !== undefined) visible.push(routeThread);
- }
- return visible;
- }, [routeThreadKey, settledThreads, settledVisibleCount]);
- const hiddenSettledCount = settledThreads.length - visibleSettledThreads.length;
- const showMoreSettled = useCallback(
- () => setSettledVisibleCount((count) => count + SETTLED_TAIL_PAGE_COUNT),
- [],
- );
- const [settledShelfExpanded, setSettledShelfExpanded] = useLocalStorage(
- SETTLED_SHELF_EXPANDED_KEY,
- false,
- Schema.Boolean,
- );
- const toggleSettledShelf = useCallback(
- () => setSettledShelfExpanded((value) => !value),
- [setSettledShelfExpanded],
- );
- const renderedSettledThreads = useMemo(() => {
- if (settledShelfExpanded) return visibleSettledThreads;
- if (routeThreadKey === null) return EMPTY_THREADS;
- const routeThread = visibleSettledThreads.find(
- (thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)) === routeThreadKey,
- );
- return routeThread === undefined ? EMPTY_THREADS : [routeThread];
- }, [routeThreadKey, settledShelfExpanded, visibleSettledThreads]);
+ }, [activeSearchResultKey]);
- const [snoozedShelfExpanded, setSnoozedShelfExpanded] = useLocalStorage(
- SNOOZED_SHELF_EXPANDED_KEY,
- false,
- Schema.Boolean,
- );
- const toggleSnoozedShelf = useCallback(
- () => setSnoozedShelfExpanded((value) => !value),
- [setSnoozedShelfExpanded],
- );
- const visibleSnoozedThreads = useMemo(() => {
- if (snoozedShelfExpanded) return snoozedThreads;
- if (routeThreadKey === null) return EMPTY_THREADS;
- const routeThread = snoozedThreads.find(
- (thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)) === routeThreadKey,
- );
- return routeThread === undefined ? EMPTY_THREADS : [routeThread];
- }, [routeThreadKey, snoozedShelfExpanded, snoozedThreads]);
-
- const [workingShelfExpanded, setWorkingShelfExpanded] = useLocalStorage(
- WORKING_SHELF_EXPANDED_KEY,
- false,
- Schema.Boolean,
- );
- const toggleWorkingShelf = useCallback(
- () => setWorkingShelfExpanded((value) => !value),
- [setWorkingShelfExpanded],
- );
- const visibleWorkingThreads = useMemo(() => {
- if (workingShelfExpanded) return workingThreads;
- if (routeThreadKey === null) return EMPTY_THREADS;
- const routeThread = workingThreads.find(
- (thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)) === routeThreadKey,
- );
- return routeThread === undefined ? EMPTY_THREADS : [routeThread];
- }, [routeThreadKey, workingShelfExpanded, workingThreads]);
-
- const orderedThreads = useMemo(
- () => [
- ...pinnedThreads,
- ...activeThreads,
- ...visibleWorkingThreads,
- ...visibleSnoozedThreads,
- ...renderedSettledThreads,
- ],
- [
- pinnedThreads,
- activeThreads,
- visibleWorkingThreads,
- visibleSnoozedThreads,
- renderedSettledThreads,
- ],
- );
- const orderedThreadKeys = useMemo(
- () =>
- orderedThreads.map((thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- ),
- [orderedThreads],
- );
const orderedThreadKeysRef = useRef(orderedThreadKeys);
- orderedThreadKeysRef.current = orderedThreadKeys;
- const threadByKey = useMemo(
- () =>
- new Map(
- orderedThreads.map(
- (thread) =>
- [scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)), thread] as const,
- ),
- ),
- [orderedThreads],
- );
// Handlers read these through refs: depending on per-update Map/Set
// identities would give every row a fresh callback prop on each shell
// event and defeat row memoization during streaming.
const threadByKeyRef = useRef(threadByKey);
- threadByKeyRef.current = threadByKey;
- const handleNewThreadRef = useRef(newThreadContext.handleNewThread);
- handleNewThreadRef.current = newThreadContext.handleNewThread;
- const settledThreadKeys = useMemo(
- () =>
- new Set(
- settledThreads.map((thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- ),
- ),
- [settledThreads],
- );
const settledThreadKeysRef = useRef(settledThreadKeys);
- settledThreadKeysRef.current = settledThreadKeys;
- const snoozedThreadKeys = useMemo(
- () =>
- new Set(
- snoozedThreads.map((thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- ),
- ),
- [snoozedThreads],
- );
const snoozedThreadKeysRef = useRef(snoozedThreadKeys);
- snoozedThreadKeysRef.current = snoozedThreadKeys;
- const jumpLabelByKey = useMemo(() => {
+ const jumpLabelByKey = (() => {
const mapping = new Map<string, string>();
for (const [index, threadKey] of orderedThreadKeys.entries()) {
const jumpCommand = threadJumpCommandForIndex(index);
@@ -2909,323 +2957,265 @@ export default function Sidebar() {
if (label) mapping.set(threadKey, label);
}
return mapping;
- }, [keybindings, orderedThreadKeys]);
+ })();
const { showThreadJumpHints: showJumpHints, updateThreadJumpHintsVisibility } =
useThreadJumpHintVisibility();
- const navigateToThread = useCallback(
- (threadRef: ScopedThreadRef) => {
- if (useThreadSelectionStore.getState().selectedThreadKeys.size > 0) {
- clearSelection();
- }
- setSelectionAnchor(scopedThreadKey(threadRef));
- if (isMobile) {
- setOpenMobile(false);
- }
- return router.navigate({
+ const navigateToThread = (threadRef: ScopedThreadRef) => {
+ if (useThreadSelectionStore.getState().selectedThreadKeys.size > 0) {
+ clearSelection();
+ }
+ setSelectionAnchor(scopedThreadKey(threadRef));
+ if (isMobile) {
+ setOpenMobile(false);
+ }
+ return router.navigate({
+ to: "/$environmentId/$threadId",
+ params: buildThreadRouteParams(threadRef),
+ });
+ };
+
+ const handleThreadFileDrop = async (threadRef: ScopedThreadRef, files: File[]) => {
+ const dropId = queuePendingFileDrop({ threadRef, files });
+ const landedBefore =
+ router.buildLocation({
to: "/$environmentId/$threadId",
params: buildThreadRouteParams(threadRef),
- });
- },
- [clearSelection, isMobile, router, setOpenMobile, setSelectionAnchor],
- );
+ }).pathname === router.state.location.pathname;
+ if (landedBefore) return;
- const queuePendingFileDrop = useSidebarPendingFileDropStore((s) => s.queuePendingFileDrop);
- const clearPendingFileDrop = useSidebarPendingFileDropStore((s) => s.clearPendingFileDrop);
- const handleThreadFileDrop = useCallback(
- async (threadRef: ScopedThreadRef, files: File[]) => {
- const dropId = queuePendingFileDrop({ threadRef, files });
- const landedBefore =
+ try {
+ await navigateToThread(threadRef);
+ const landed =
router.buildLocation({
to: "/$environmentId/$threadId",
params: buildThreadRouteParams(threadRef),
}).pathname === router.state.location.pathname;
- if (landedBefore) return;
-
- try {
- await navigateToThread(threadRef);
- const landed =
- router.buildLocation({
- to: "/$environmentId/$threadId",
- params: buildThreadRouteParams(threadRef),
- }).pathname === router.state.location.pathname;
- if (!landed) clearPendingFileDrop(dropId);
- } catch {
- clearPendingFileDrop(dropId);
- }
- },
- [clearPendingFileDrop, navigateToThread, queuePendingFileDrop, router],
- );
+ if (!landed) clearPendingFileDrop(dropId);
+ } catch {
+ clearPendingFileDrop(dropId);
+ }
+ };
- const navigateToDraft = useCallback(
- (draftId: DraftId) => {
- clearSelection();
- if (isMobile) {
- setOpenMobile(false);
- }
- void router.navigate({ to: "/draft/$draftId", params: { draftId } });
- },
- [clearSelection, isMobile, router, setOpenMobile],
- );
+ const navigateToDraft = (draftId: DraftId) => {
+ clearSelection();
+ if (isMobile) {
+ setOpenMobile(false);
+ }
+ void router.navigate({ to: "/draft/$draftId", params: { draftId } });
+ };
- const clearThreadSearch = useCallback(() => {
+ const clearThreadSearch = () => {
setThreadSearchQuery("");
setActiveSearchResultIndex(0);
- }, []);
- const selectThreadSearchResult = useCallback(
- (thread: EnvironmentThreadShell) => {
+ };
+ const selectThreadSearchResult = (thread: EnvironmentThreadShell) => {
+ clearThreadSearch();
+ navigateToThread(scopeThreadRef(thread.environmentId, thread.id));
+ };
+ const handleThreadSearchKeyDown = (event: ReactKeyboardEvent<HTMLInputElement>) => {
+ if (event.nativeEvent.isComposing || event.keyCode === 229) return;
+ if (event.key === "Escape" && isSearchingThreads) {
+ event.preventDefault();
+ event.stopPropagation();
clearThreadSearch();
- navigateToThread(scopeThreadRef(thread.environmentId, thread.id));
- },
- [clearThreadSearch, navigateToThread],
- );
- const handleThreadSearchKeyDown = useCallback(
- (event: ReactKeyboardEvent<HTMLInputElement>) => {
- if (event.nativeEvent.isComposing || event.keyCode === 229) return;
- if (event.key === "Escape" && isSearchingThreads) {
- event.preventDefault();
- event.stopPropagation();
- clearThreadSearch();
- return;
- }
- if (threadSearchResults.length === 0) return;
- if (event.key === "ArrowDown") {
- event.preventDefault();
- setActiveSearchResultIndex((index) => (index + 1) % threadSearchResults.length);
- return;
- }
- if (event.key === "ArrowUp") {
- event.preventDefault();
- setActiveSearchResultIndex(
- (index) => (index - 1 + threadSearchResults.length) % threadSearchResults.length,
- );
- return;
- }
- if (event.key === "Enter") {
- event.preventDefault();
- const result = threadSearchResults[activeSearchResultIndex];
- if (result) selectThreadSearchResult(result);
- }
- },
- [
- activeSearchResultIndex,
- clearThreadSearch,
- isSearchingThreads,
- selectThreadSearchResult,
- threadSearchResults,
- ],
- );
+ return;
+ }
+ if (threadSearchResults.length === 0) return;
+ if (event.key === "ArrowDown") {
+ event.preventDefault();
+ setActiveSearchResultIndex((index) => (index + 1) % threadSearchResults.length);
+ return;
+ }
+ if (event.key === "ArrowUp") {
+ event.preventDefault();
+ setActiveSearchResultIndex(
+ (index) => (index - 1 + threadSearchResults.length) % threadSearchResults.length,
+ );
+ return;
+ }
+ if (event.key === "Enter") {
+ event.preventDefault();
+ const result = threadSearchResults[activeSearchResultIndex];
+ if (result) selectThreadSearchResult(result);
+ }
+ };
- const [renamingThreadKey, setRenamingThreadKey] = useState<string | null>(null);
- const [renamingTitle, setRenamingTitle] = useState("");
- const startThreadRename = useCallback((threadRef: ScopedThreadRef, title: string) => {
+ const startThreadRename = (threadRef: ScopedThreadRef, title: string) => {
if (!readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)) return;
setRenamingThreadKey(scopedThreadKey(threadRef));
setRenamingTitle(title);
- }, []);
- const cancelThreadRename = useCallback(() => setRenamingThreadKey(null), []);
- const commitThreadRename = useCallback(
- (threadRef: ScopedThreadRef, title: string, originalTitle: string) => {
- void (async () => {
- if (!readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)) return;
- const trimmed = title.trim();
- setRenamingThreadKey(null);
- if (trimmed.length === 0) {
- toastManager.add({ type: "warning", title: "Thread title cannot be empty" });
- return;
- }
- if (trimmed === originalTitle) return;
- const result = await updateThreadMetadata({
- environmentId: threadRef.environmentId,
- input: { threadId: threadRef.threadId, title: trimmed },
- });
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
- const error = squashAtomCommandFailure(result);
- toastManager.add(
- stackedThreadToast({
- type: "error",
- title: "Failed to rename thread",
- description: error instanceof Error ? error.message : "An error occurred.",
- }),
- );
- }
- })();
- },
- [updateThreadMetadata],
- );
-
- const handleThreadClick = useCallback(
- (event: ReactMouseEvent, threadRef: ScopedThreadRef) => {
- if (isSidebarNestedLinkClick(event.target)) return;
- const isMac = isMacPlatform(navigator.platform);
- const isModClick = isMac ? event.metaKey : event.ctrlKey;
- const threadKey = scopedThreadKey(threadRef);
- if (isModClick) {
- event.preventDefault();
- toggleThreadSelection(threadKey);
- return;
- }
- if (event.shiftKey) {
- event.preventDefault();
- rangeSelectTo(threadKey, orderedThreadKeysRef.current);
+ };
+ const cancelThreadRename = () => setRenamingThreadKey(null);
+ const commitThreadRename = (threadRef: ScopedThreadRef, title: string, originalTitle: string) => {
+ void (async () => {
+ if (!readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)) return;
+ const trimmed = title.trim();
+ setRenamingThreadKey(null);
+ if (trimmed.length === 0) {
+ toastManager.add({ type: "warning", title: "Thread title cannot be empty" });
return;
}
- if (isTrailingDoubleClick(event.detail)) {
- return;
+ if (trimmed === originalTitle) return;
+ const result = await updateThreadMetadata({
+ environmentId: threadRef.environmentId,
+ input: { threadId: threadRef.threadId, title: trimmed },
+ });
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
+ const error = squashAtomCommandFailure(result);
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title: "Failed to rename thread",
+ description: error instanceof Error ? error.message : "An error occurred.",
+ }),
+ );
}
- navigateToThread(threadRef);
- },
- [navigateToThread, rangeSelectTo, toggleThreadSelection],
- );
+ })();
+ };
+
+ const handleThreadClick = (event: ReactMouseEvent, threadRef: ScopedThreadRef) => {
+ if (isSidebarNestedLinkClick(event.target)) return;
+ const isMac = isMacPlatform(navigator.platform);
+ const isModClick = isMac ? event.metaKey : event.ctrlKey;
+ const threadKey = scopedThreadKey(threadRef);
+ if (isModClick) {
+ event.preventDefault();
+ toggleThreadSelection(threadKey);
+ return;
+ }
+ if (event.shiftKey) {
+ event.preventDefault();
+ rangeSelectTo(threadKey, orderedThreadKeysRef.current);
+ return;
+ }
+ if (isTrailingDoubleClick(event.detail)) {
+ return;
+ }
+ navigateToThread(threadRef);
+ };
- const settlingThreadKeysRef = useRef(new Set<string>());
// Callers snapshot the plan BEFORE the command mutates the partition;
// background parks never navigate (null plan).
- const planForwardNavigation = useCallback(
- (threadKey: string, coParkingKeys?: ReadonlySet<string>): (() => void) | null => {
- if (routeThreadKeyRef.current !== threadKey) return null;
- const shell = threadByKeyRef.current.get(threadKey);
- const orderedKeys = orderedThreadKeysRef.current;
- const settledKeys = settledThreadKeysRef.current;
- const snoozedKeys = snoozedThreadKeysRef.current;
- const currentIndex = orderedKeys.indexOf(threadKey);
- const nextCardKey =
- currentIndex === -1
- ? null
- : ([...orderedKeys.slice(currentIndex + 1), ...orderedKeys.slice(0, currentIndex)].find(
- (key) => !settledKeys.has(key) && !snoozedKeys.has(key) && !coParkingKeys?.has(key),
- ) ?? null);
- const nextThread = nextCardKey ? threadByKeyRef.current.get(nextCardKey) : null;
- return nextThread
- ? () => navigateToThread(scopeThreadRef(nextThread.environmentId, nextThread.id))
- : shell
- ? () =>
- void handleNewThreadRef.current(scopeProjectRef(shell.environmentId, shell.projectId))
- : () => void router.navigate({ to: "/" });
- },
- [navigateToThread, router],
- );
+ const planForwardNavigation = (
+ threadKey: string,
+ coParkingKeys?: ReadonlySet<string>,
+ ): (() => void) | null => {
+ if (routeThreadKeyRef.current !== threadKey) return null;
+ const shell = threadByKeyRef.current.get(threadKey);
+ const orderedKeys = orderedThreadKeysRef.current;
+ const settledKeys = settledThreadKeysRef.current;
+ const snoozedKeys = snoozedThreadKeysRef.current;
+ const currentIndex = orderedKeys.indexOf(threadKey);
+ const nextCardKey =
+ currentIndex === -1
+ ? null
+ : ([...orderedKeys.slice(currentIndex + 1), ...orderedKeys.slice(0, currentIndex)].find(
+ (key) => !settledKeys.has(key) && !snoozedKeys.has(key) && !coParkingKeys?.has(key),
+ ) ?? null);
+ const nextThread = nextCardKey ? threadByKeyRef.current.get(nextCardKey) : null;
+ return nextThread
+ ? () => navigateToThread(scopeThreadRef(nextThread.environmentId, nextThread.id))
+ : shell
+ ? () =>
+ void handleNewThreadRef.current(scopeProjectRef(shell.environmentId, shell.projectId))
+ : () => void router.navigate({ to: "/" });
+ };
- const attemptSettle = useCallback(
- (threadRef: ScopedThreadRef, opts: { coSettlingKeys?: ReadonlySet<string> } = {}) => {
- void (async () => {
- const threadKey = scopedThreadKey(threadRef);
- if (settlingThreadKeysRef.current.has(threadKey)) return;
- settlingThreadKeysRef.current.add(threadKey);
- try {
- const navigateAfterSettle = planForwardNavigation(threadKey, opts.coSettlingKeys);
- const result = await settleThread(threadRef);
- if (result._tag === "Failure") {
- if (!isAtomCommandInterrupted(result)) {
- const error = squashAtomCommandFailure(result);
- toastManager.add(
- stackedThreadToast({
- type: "error",
- title: "Failed to settle thread",
- description: error instanceof Error ? error.message : "An error occurred.",
- }),
- );
- }
- return;
- }
- if (
- shouldNavigateAfterThreadPark({
- threadKey,
- currentThreadKey: routeThreadKeyRef.current,
- action: "settle",
- now: new Date().toISOString(),
- thread: readThreadShell(threadRef),
- })
- ) {
- navigateAfterSettle?.();
- }
- } finally {
- settlingThreadKeysRef.current.delete(threadKey);
- }
- })();
- },
- [planForwardNavigation, settleThread],
- );
- useEffect(() => registerSettleAndAdvance(attemptSettle), [attemptSettle]);
- const settleThreads = useCallback(
- (threadKeys: readonly string[]) => {
- const coSettlingKeys = new Set(threadKeys);
- for (const threadKey of threadKeys) {
- const thread = threadByKeyRef.current.get(threadKey);
- if (!thread || thread.settledOverride === "settled") continue;
- attemptSettle(scopeThreadRef(thread.environmentId, thread.id), { coSettlingKeys });
- }
- },
- [attemptSettle],
- );
- const attemptUnsettle = useCallback(
- (threadRef: ScopedThreadRef) => {
- void (async () => {
- const result = await unsettleThread(threadRef);
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
- const error = squashAtomCommandFailure(result);
- toastManager.add(
- stackedThreadToast({
- type: "error",
- title: "Failed to un-settle thread",
- description: error instanceof Error ? error.message : "An error occurred.",
- }),
- );
- }
- })();
- },
- [unsettleThread],
- );
- const attemptUnsnooze = useCallback(
- (threadRef: ScopedThreadRef) => {
- void (async () => {
- const result = await unsnoozeThread(threadRef);
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
+ const attemptSettle = (
+ threadRef: ScopedThreadRef,
+ opts: { coSettlingKeys?: ReadonlySet<string> } = {},
+ ) => {
+ void (async () => {
+ const threadKey = scopedThreadKey(threadRef);
+ if (settlingThreadKeysRef.current.has(threadKey)) return;
+ settlingThreadKeysRef.current.add(threadKey);
+ const navigateAfterSettle = planForwardNavigation(threadKey, opts.coSettlingKeys);
+ const result = await settleThread(threadRef);
+ settlingThreadKeysRef.current.delete(threadKey);
+ if (result._tag === "Failure") {
+ if (!isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add(
stackedThreadToast({
type: "error",
- title: "Failed to wake thread",
+ title: "Failed to settle thread",
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
}
- })();
- },
- [unsnoozeThread],
- );
- const threadListRef = useRef<HTMLUListElement | null>(null);
- const dragLabelOffsetRef = useRef(0);
- const restrictBelowPins = useCallback<Modifier>(
- (args) => restrictBelowSidebarLabel(args, dragLabelOffsetRef.current),
- [],
- );
+ return;
+ }
+ if (
+ shouldNavigateAfterThreadPark({
+ threadKey,
+ currentThreadKey: routeThreadKeyRef.current,
+ action: "settle",
+ now: new Date().toISOString(),
+ thread: readThreadShell(threadRef),
+ })
+ ) {
+ navigateAfterSettle?.();
+ }
+ })();
+ };
+ useEffect(() => registerSettleAndAdvance(attemptSettle), [attemptSettle]);
+ const settleThreads = (threadKeys: readonly string[]) => {
+ const coSettlingKeys = new Set(threadKeys);
+ for (const threadKey of threadKeys) {
+ const thread = threadByKeyRef.current.get(threadKey);
+ if (!thread || thread.settledOverride === "settled") continue;
+ attemptSettle(scopeThreadRef(thread.environmentId, thread.id), { coSettlingKeys });
+ }
+ };
+ const attemptUnsettle = (threadRef: ScopedThreadRef) => {
+ void (async () => {
+ const result = await unsettleThread(threadRef);
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
+ const error = squashAtomCommandFailure(result);
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title: "Failed to un-settle thread",
+ description: error instanceof Error ? error.message : "An error occurred.",
+ }),
+ );
+ }
+ })();
+ };
+ const attemptUnsnooze = (threadRef: ScopedThreadRef) => {
+ void (async () => {
+ const result = await unsnoozeThread(threadRef);
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
+ const error = squashAtomCommandFailure(result);
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title: "Failed to wake thread",
+ description: error instanceof Error ? error.message : "An error occurred.",
+ }),
+ );
+ }
+ })();
+ };
+ const restrictBelowPins: Modifier = (args) =>
+ restrictBelowSidebarLabel(args, dragLabelOffsetRef.current);
// A new array re-renders DndContext and with it every sortable row.
- const dndModifiers = useMemo(
- () => [restrictToVerticalAxis, restrictBelowPins, restrictToFirstScrollableAncestor],
- [restrictBelowPins],
- );
- const listMotionRef = useRef<ReturnType<typeof createSidebarListMotion> | null>(null);
- const attachListMotionRef = useCallback((node: HTMLUListElement | null) => {
+ const dndModifiers = [
+ restrictToVerticalAxis,
+ restrictBelowPins,
+ restrictToFirstScrollableAncestor,
+ ];
+ const attachListMotionRef = (node: HTMLUListElement | null) => {
threadListRef.current = node;
listMotionRef.current?.dispose();
listMotionRef.current = node === null ? null : createSidebarListMotion(node);
listMotionRef.current?.update(false);
- }, []);
+ };
- const [dragState, setDragState] = useState<{
- readonly activeKey: string;
- readonly activeSection: SidebarSection;
- readonly occurredAt: string;
- readonly activationY: number | null;
- readonly targetSection: SidebarSection | null;
- readonly contextDrag: boolean;
- } | null>(null);
const isContextDrag = dragState?.contextDrag === true;
const dragTargetSection = isContextDrag ? null : (dragState?.targetSection ?? null);
- const dragSensorRef = useRef<SidebarPointerSensor | null>(null);
- const contextDragKeyRef = useRef<string | null>(null);
- const finishThreadDrag = useCallback((started: boolean) => {
+ const finishThreadDrag = (started: boolean) => {
dragSensorRef.current = null;
contextDragKeyRef.current = null;
endThreadContextDrag();
@@ -3233,14 +3223,14 @@ export default function Sidebar() {
listMotionRef.current?.release();
setDragState(null);
}
- }, []);
- const attachDragSensor = useCallback((sensor: SidebarPointerSensor) => {
+ };
+ const attachDragSensor = (sensor: SidebarPointerSensor) => {
dragSensorRef.current = sensor;
- }, []);
- const cancelThreadDrag = useCallback(() => {
+ };
+ const cancelThreadDrag = () => {
dragSensorRef.current?.cancel();
- }, []);
- const contextDragThreads = useCallback((): ReadonlyArray<ScopedThreadRef> => {
+ };
+ const contextDragThreads = (): ReadonlyArray<ScopedThreadRef> => {
const key = contextDragKeyRef.current;
if (key === null) return [];
const selected = useThreadSelectionStore.getState().selectedThreadKeys;
@@ -3249,72 +3239,54 @@ export default function Sidebar() {
const ref = parseScopedThreadKey(candidate);
return ref ? [ref] : [];
});
- }, []);
- const pointerOutsideThreadList = useCallback((point: { x: number; y: number }) => {
+ };
+ const pointerOutsideThreadList = (point: { x: number; y: number }) => {
const bounds = threadListRef.current?.getBoundingClientRect();
return bounds !== undefined && (point.x < bounds.left || point.x > bounds.right);
- }, []);
- const moveThreadContextDrag = useCallback(
- (point: { x: number; y: number }) => {
- const contextDrag = pointerOutsideThreadList(point);
- setDragState((current) =>
- current === null || current.contextDrag === contextDrag
- ? current
- : { ...current, contextDrag },
- );
- if (!contextDrag) {
- endThreadContextDrag();
- return false;
- }
- const threads = contextDragThreads();
- const title =
- threadByKeyRef.current.get(contextDragKeyRef.current ?? "")?.title.trim() || "Thread";
- moveThreadContextDragGhost(point, { title, count: threads.length });
- return true;
- },
- [contextDragThreads, pointerOutsideThreadList],
- );
- const dropThreadContextDrag = useCallback(
- (point: { x: number; y: number }) => {
- if (!pointerOutsideThreadList(point)) return false;
- dropThreadContext(point, contextDragThreads());
- return true;
- },
- [contextDragThreads, pointerOutsideThreadList],
- );
+ };
+ const moveThreadContextDrag = (point: { x: number; y: number }) => {
+ const contextDrag = pointerOutsideThreadList(point);
+ setDragState((current) =>
+ current === null || current.contextDrag === contextDrag
+ ? current
+ : { ...current, contextDrag },
+ );
+ if (!contextDrag) {
+ endThreadContextDrag();
+ return false;
+ }
+ const threads = contextDragThreads();
+ const title =
+ threadByKeyRef.current.get(contextDragKeyRef.current ?? "")?.title.trim() || "Thread";
+ moveThreadContextDragGhost(point, { title, count: threads.length });
+ return true;
+ };
+ const dropThreadContextDrag = (point: { x: number; y: number }) => {
+ if (!pointerOutsideThreadList(point)) return false;
+ dropThreadContext(point, contextDragThreads());
+ return true;
+ };
// useSensor memoizes on the options object; a fresh one each render rebuilds
// the sensors, and with them DndContext and every sortable row.
- const pointerSensorOptions = useMemo(
- () => ({
- distance: SIDEBAR_DRAG_DISTANCE,
- onAttach: attachDragSensor,
- onFinish: finishThreadDrag,
- onMove: moveThreadContextDrag,
- onDrop: dropThreadContextDrag,
- }),
- [attachDragSensor, dropThreadContextDrag, finishThreadDrag, moveThreadContextDrag],
- );
+ const pointerSensorOptions = {
+ distance: SIDEBAR_DRAG_DISTANCE,
+ onAttach: attachDragSensor,
+ onFinish: finishThreadDrag,
+ onMove: moveThreadContextDrag,
+ onDrop: dropThreadContextDrag,
+ };
const dndSensors = useSensors(useSensor(SidebarPointerSensor, pointerSensorOptions));
- const sectionByThreadKey = useMemo(() => {
- const map = new Map<string, SidebarSection>();
- const add = (list: readonly EnvironmentThreadShell[], section: SidebarSection) => {
- for (const thread of list) {
- map.set(scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)), section);
- }
- };
- add(pinnedThreads, "pinned");
- add(activeThreads, "active");
- add(workingThreads, "working");
- add(snoozedThreads, "snoozed");
- add(settledThreads, "settled");
- return map;
- }, [activeThreads, pinnedThreads, settledThreads, snoozedThreads, workingThreads]);
const sectionByThreadKeyRef = useRef(sectionByThreadKey);
- sectionByThreadKeyRef.current = sectionByThreadKey;
- const [actionSweep, setActionSweep] = useState<{
- action: SidebarSweepAction;
- keys: ReadonlySet<string>;
- } | null>(null);
+ useLayoutEffect(() => {
+ routeThreadKeyRef.current = routeThreadKey;
+ projectGroupsRef.current = projectGroups;
+ orderedThreadKeysRef.current = orderedThreadKeys;
+ threadByKeyRef.current = threadByKey;
+ handleNewThreadRef.current = newThreadContext.handleNewThread;
+ settledThreadKeysRef.current = settledThreadKeys;
+ snoozedThreadKeysRef.current = snoozedThreadKeys;
+ sectionByThreadKeyRef.current = sectionByThreadKey;
+ });
const startActionSweep = useStableCallback(
(threadRef: ScopedThreadRef, action: SidebarSweepAction, event: PointerEvent) => {
const originKey = scopedThreadKey(threadRef);
@@ -3372,160 +3344,82 @@ export default function Sidebar() {
});
},
);
- const pinnedKeys = useMemo(
- () =>
- pinnedThreads.map((thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- ),
- [pinnedThreads],
- );
- const activeKeys = useMemo(
- () =>
- activeThreads.map((thread) =>
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- ),
- [activeThreads],
- );
- useEffect(() => {
- if (optimisticDrop === null) return;
- const canonicalByKey = new Map(
- threads.map((thread) => [
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- thread,
- ]),
- );
- const thread = canonicalByKey.get(optimisticDrop.key);
- if (thread === undefined || thread.archivedAt !== null) {
- setOptimisticDrop(null);
- return;
- }
- const canonicalSection = effectiveSnoozed(thread, { now: new Date().toISOString() })
- ? "snoozed"
- : thread.settledOverride === "settled"
- ? "settled"
- : thread.pinnedAt != null
- ? "pinned"
- : "active";
- if (
- canonicalSection !== optimisticDrop.sourceSection &&
- canonicalSection !== optimisticDrop.section
- ) {
- setOptimisticDrop(null);
- return;
- }
- if (optimisticDrop.order === null) {
- // Settle also emits unpin/unsnooze events. Wait for the entire move
- // before releasing the projected fields and sort timestamps.
- if (
- canonicalSection === optimisticDrop.section &&
- thread.pinnedAt == null &&
- (!optimisticDrop.clearsSnooze || thread.snoozedUntil == null)
- ) {
- setOptimisticDrop(null);
+ // Release the hold during render once the server state catches up, so the
+ // canonical order shows in the same frame instead of after an effect pass.
+ if (
+ optimisticDrop !== null &&
+ isOptimisticDropReleased({
+ optimisticDrop,
+ threads,
+ pinnedKeys,
+ activeKeys,
+ now: snoozeNow,
+ })
+ ) {
+ setOptimisticDrop(null);
+ }
+ const attemptPin = (threadRef: ScopedThreadRef) => {
+ void (async () => {
+ // Fresh pins take the top of the arranged run: pinThread computes a
+ // key before the smallest key across ALL pinned shells — including
+ // snoozed pins hidden from this list, whose keys are still part of
+ // the run — so the new pin can't land beneath a hidden head.
+ const result = await pinThread(threadRef);
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
+ const error = squashAtomCommandFailure(result);
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title: "Failed to pin thread",
+ description: error instanceof Error ? error.message : "An error occurred.",
+ }),
+ );
}
- return;
+ })();
+ };
+ const attemptUnpin = (threadRef: ScopedThreadRef) => {
+ void (async () => {
+ const result = await confirmAndUnpinThread(threadRef);
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
+ const error = squashAtomCommandFailure(result);
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title: "Failed to unpin thread",
+ description: error instanceof Error ? error.message : "An error occurred.",
+ }),
+ );
+ }
+ })();
+ };
+
+ const handleThreadDragStart = (event: DragStartEvent) => {
+ const activeKey = String(event.active.id);
+ contextDragKeyRef.current = activeKey;
+ const activeSection = sectionByThreadKey.get(activeKey);
+ if (activeSection === undefined) return;
+ listMotionRef.current?.suspend();
+ const list = threadListRef.current;
+ const header = list?.querySelector<HTMLElement>('[data-testid="sidebar-pinned-header"]');
+ if (list && header) {
+ const listRect = list.getBoundingClientRect();
+ const scale = list.offsetWidth > 0 ? listRect.width / list.offsetWidth : 1;
+ dragLabelOffsetRef.current =
+ header.getBoundingClientRect().top - listRect.top + SIDEBAR_DRAG_LABEL_HEIGHT * scale;
+ } else {
+ dragLabelOffsetRef.current = 0;
}
- if (canonicalSection !== optimisticDrop.section) return;
- if (optimisticDrop.clearsSnooze && thread.snoozedUntil != null) return;
- const destinationKeys = optimisticDrop.section === "pinned" ? pinnedKeys : activeKeys;
- const canonicalDestination = destinationKeys.flatMap((key) => {
- const canonical = canonicalByKey.get(key);
- return canonical === undefined ? [] : [canonical];
- });
- const keyByThread = new Map(
- canonicalDestination.map((thread) => [
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- (optimisticDrop.section === "pinned" ? thread.pinOrderKey : thread.activeOrderKey) ?? null,
- ]),
- );
- const heldOrder = optimisticDrop.order;
- const heldKeys = new Set(heldOrder);
- const membershipChanged =
- destinationKeys.length !== heldOrder.length ||
- destinationKeys.some((key) => !heldKeys.has(key));
- const foreignKeyLanded = destinationKeys.some((threadKey) => {
- const currentKey = keyByThread.get(threadKey) ?? null;
- if (currentKey === (optimisticDrop.keysAtDrop.get(threadKey) ?? null)) return false;
- return currentKey !== optimisticDrop.assignedKeys.get(threadKey);
+ setDragState({
+ activeKey,
+ activeSection,
+ targetSection: activeSection,
+ contextDrag: false,
+ occurredAt: new Date().toISOString(),
+ activationY:
+ event.activatorEvent instanceof PointerEvent ? event.activatorEvent.clientY : null,
});
- const allAssignmentsLanded = [...optimisticDrop.assignedKeys].every(
- ([threadKey, orderKey]) => keyByThread.get(threadKey) === orderKey,
- );
- if (membershipChanged || foreignKeyLanded || allAssignmentsLanded) {
- setOptimisticDrop(null);
- }
- }, [activeKeys, optimisticDrop, pinnedKeys, threads]);
- const attemptPin = useCallback(
- (threadRef: ScopedThreadRef) => {
- void (async () => {
- // Fresh pins take the top of the arranged run: pinThread computes a
- // key before the smallest key across ALL pinned shells — including
- // snoozed pins hidden from this list, whose keys are still part of
- // the run — so the new pin can't land beneath a hidden head.
- const result = await pinThread(threadRef);
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
- const error = squashAtomCommandFailure(result);
- toastManager.add(
- stackedThreadToast({
- type: "error",
- title: "Failed to pin thread",
- description: error instanceof Error ? error.message : "An error occurred.",
- }),
- );
- }
- })();
- },
- [pinThread],
- );
- const attemptUnpin = useCallback(
- (threadRef: ScopedThreadRef) => {
- void (async () => {
- const result = await confirmAndUnpinThread(threadRef);
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
- const error = squashAtomCommandFailure(result);
- toastManager.add(
- stackedThreadToast({
- type: "error",
- title: "Failed to unpin thread",
- description: error instanceof Error ? error.message : "An error occurred.",
- }),
- );
- }
- })();
- },
- [confirmAndUnpinThread],
- );
-
- const handleThreadDragStart = useCallback(
- (event: DragStartEvent) => {
- const activeKey = String(event.active.id);
- contextDragKeyRef.current = activeKey;
- const activeSection = sectionByThreadKey.get(activeKey);
- if (activeSection === undefined) return;
- listMotionRef.current?.suspend();
- const list = threadListRef.current;
- const header = list?.querySelector<HTMLElement>('[data-testid="sidebar-pinned-header"]');
- if (list && header) {
- const listRect = list.getBoundingClientRect();
- const scale = list.offsetWidth > 0 ? listRect.width / list.offsetWidth : 1;
- dragLabelOffsetRef.current =
- header.getBoundingClientRect().top - listRect.top + SIDEBAR_DRAG_LABEL_HEIGHT * scale;
- } else {
- dragLabelOffsetRef.current = 0;
- }
- setDragState({
- activeKey,
- activeSection,
- targetSection: activeSection,
- contextDrag: false,
- occurredAt: new Date().toISOString(),
- activationY:
- event.activatorEvent instanceof PointerEvent ? event.activatorEvent.clientY : null,
- });
- },
- [sectionByThreadKey],
- );
- const sidebarListItemsFresh = useMemo((): readonly SidebarListItem[] => {
+ };
+ const sidebarListItemsFresh = ((): readonly SidebarListItem[] => {
const rowsOf = (
list: readonly EnvironmentThreadShell[],
section: SidebarSection,
@@ -3564,16 +3458,7 @@ export default function Sidebar() {
items.push({ kind: "marker", marker: "settled-placeholder" });
items.push(...settledRows);
return items;
- }, [
- activeThreads,
- pinnedThreads,
- renderedSettledThreads,
- settledThreads.length,
- snoozedThreads.length,
- visibleSnoozedThreads,
- visibleWorkingThreads,
- workingThreads.length,
- ]);
+ })();
// Thread shells update on every streamed event without moving rows. Keep the
// list's identity until its order changes, so the sorting strategy, the
// sortable context and every sortable row stay memoized meanwhile.
@@ -3594,113 +3479,60 @@ export default function Sidebar() {
cancelThreadDrag();
}
}, [cancelThreadDrag, dragState, sidebarListItems]);
- const listMotionPaused = dragState !== null;
- // Every shell event rebuilds sidebarListItems, but rows only move when the
- // rendered order or a row's section changes. Keying the motion pass on that
- // keeps ordinary updates from forcing a layout read and animating rows
- // whose position drifted for other reasons.
- const sidebarListOrderKey = useMemo(
- () =>
- sidebarListItems
- .map((item) => (item.kind === "thread" ? `${item.key}:${item.section}` : item.marker))
- .join("\0"),
- [sidebarListItems],
- );
- const sidebarListHasRows = sidebarListItems.length + visibleDraftSessionCount > 0;
- useLayoutEffect(() => {
- void sidebarListOrderKey;
- listMotionRef.current?.update(!listMotionPaused && sidebarListHasRows);
- }, [
- listMotionPaused,
- routeDraftIdForRows,
- sidebarListHasRows,
- sidebarListOrderKey,
+ // Rows only move when the rendered order, a row's section, or the draft block
+ // changes. Running the motion pass on that alone keeps ordinary shell events
+ // from forcing a layout read and animating rows that drifted for other reasons.
+ const listMotionLayoutKey = [
+ sidebarListItemsKey,
visibleDraftSessionCount,
- ]);
- const handleThreadDragOver = useCallback(
- (event: DragOverEvent) => {
- const target = event.over
- ? resolveSidebarDropTarget(sidebarListItems, String(event.active.id), String(event.over.id))
- : null;
- setDragState((current) =>
- current === null || current.activeKey !== String(event.active.id)
- ? current
- : { ...current, targetSection: target?.section ?? null },
- );
- },
- [sidebarListItems],
- );
- const sortableIds = useMemo(() => sidebarListItems.map(sidebarListItemId), [sidebarListItems]);
- const draggedSettledOrder = useMemo(() => {
- const thread = dragState === null ? undefined : threadByKey.get(dragState.activeKey);
- // Outside a drag this must keep one identity: the sorting strategy, and with
- // it every sortable row, rebuilds when it changes.
- if (dragState === null || thread === undefined) return NO_DRAGGED_ORDER;
- const key = (candidate: EnvironmentThreadShell) =>
- scopedThreadKey(scopeThreadRef(candidate.environmentId, candidate.id));
- return sortSettledThreads([
- ...settledThreads.filter((candidate) => key(candidate) !== dragState.activeKey),
- applySidebarThreadDrop(thread, "settled", dragState.occurredAt),
- ]).map(key);
- }, [dragState, settledThreads, threadByKey]);
- const draggedActiveOrder = useMemo(() => {
- const thread = dragState === null ? undefined : threadByKey.get(dragState.activeKey);
- if (!workingShelfEnabled || dragState === null || thread === undefined) return undefined;
- const key = (candidate: EnvironmentThreadShell) =>
- scopedThreadKey(scopeThreadRef(candidate.environmentId, candidate.id));
- return sortInboxThreadsByReturn(
- [
- ...activeThreads.filter((candidate) => key(candidate) !== dragState.activeKey),
- applySidebarThreadDrop(thread, "active", dragState.occurredAt),
- ],
- inboxReturns.returnedAt,
- ).map(key);
- }, [activeThreads, dragState, threadByKey, workingShelfEnabled]);
- const sidebarSortingStrategy = useMemo(
- () =>
- createSidebarSortingStrategy({
- items: sidebarListItems,
- enabled: !isContextDrag,
- boundaryLabelHeight: SIDEBAR_DRAG_LABEL_HEIGHT,
- settledOrder: draggedSettledOrder,
- ...(draggedActiveOrder === undefined ? {} : { activeOrder: draggedActiveOrder }),
- settledExpanded: settledShelfExpanded,
- settledVisibleCount,
- routeThreadKey,
- snoozedThreadCount: snoozedThreads.length,
- }),
- [
- draggedActiveOrder,
- draggedSettledOrder,
- isContextDrag,
- routeThreadKey,
- settledShelfExpanded,
- settledVisibleCount,
- sidebarListItems,
- snoozedThreads.length,
- ],
- );
- const { pinnedKeysById, activeKeysById } = useMemo(
- () => ({
- pinnedKeysById: new Map(
- threads.map((thread) => [
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- thread.pinOrderKey ?? null,
- ]),
- ),
- activeKeysById: new Map(
- threads.map((thread) => [
- scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
- thread.activeOrderKey ?? null,
- ]),
- ),
- }),
- [threads],
- );
+ routeDraftIdForRows ?? "",
+ ].join("\0");
+ const listMotionAnimate =
+ dragState === null && sidebarListItems.length + visibleDraftSessionCount > 0;
+ // The effect reads only `animate`; the pass object is cached on both fields,
+ // so a new layout key alone also triggers it.
+ const listMotionPass = { layoutKey: listMotionLayoutKey, animate: listMotionAnimate };
+ useLayoutEffect(() => {
+ listMotionRef.current?.update(listMotionPass.animate);
+ }, [listMotionPass]);
+ const handleThreadDragOver = (event: DragOverEvent) => {
+ const target = event.over
+ ? resolveSidebarDropTarget(sidebarListItems, String(event.active.id), String(event.over.id))
+ : null;
+ setDragState((current) =>
+ current === null || current.activeKey !== String(event.active.id)
+ ? current
+ : { ...current, targetSection: target?.section ?? null },
+ );
+ };
+ const sortableIds = sidebarListItems.map(sidebarListItemId);
+ // Outside a drag the orders are constants, so the strategy, and with it
+ // every sortable row, keeps its identity while thread shells stream in.
+ const { settledOrder: draggedSettledOrder, activeOrder: draggedActiveOrder } =
+ dragState === null
+ ? NO_DRAGGED_ORDERS
+ : resolveDraggedOrders({
+ drag: dragState,
+ threadByKey,
+ settledThreads,
+ activeThreads,
+ workingShelfEnabled,
+ });
+ const sidebarSortingStrategy = createSidebarSortingStrategy({
+ items: sidebarListItems,
+ enabled: !isContextDrag,
+ boundaryLabelHeight: SIDEBAR_DRAG_LABEL_HEIGHT,
+ settledOrder: draggedSettledOrder,
+ ...(draggedActiveOrder === undefined ? {} : { activeOrder: draggedActiveOrder }),
+ settledExpanded: settledShelfExpanded,
+ settledVisibleCount,
+ routeThreadKey,
+ snoozedThreadCount: snoozedThreads.length,
+ });
const draggedThreadKey = dragState?.activeKey;
const draggedFromSection = dragState?.activeSection;
const dragActivationY = dragState?.activationY;
- const dndCollisionDetection = useMemo(() => {
+ const dndCollisionDetection = (() => {
if (draggedThreadKey === undefined || draggedFromSection === undefined)
return createSidebarCollisionDetection(() => true);
const source = threadByKey.get(draggedThreadKey);
@@ -3734,253 +3566,201 @@ export default function Sidebar() {
activationY: dragActivationY ?? null,
},
);
- }, [
- activeKeysById,
- pinnedKeysById,
- serverConfigs,
- activeKeys,
- activeReorderableThreadKeys,
- draggedThreadKey,
- draggedFromSection,
- dragActivationY,
- draggableThreadKeys,
- pinnedKeys,
- sidebarListItems,
- threadByKey,
- workingShelfEnabled,
- ]);
- const handleThreadDragEnd = useCallback(
- (event: DragEndEvent) => {
- 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 activeThread = threadByKey.get(activeKey);
- if (activeSection === undefined || target === null || activeThread === undefined) return;
- const threadRef = scopeThreadRef(activeThread.environmentId, activeThread.id);
- const plan = planSidebarThreadDrop({
- activeKey,
- activeSection,
- activePinned: activeThread.pinnedAt != null,
- activeSettled: activeThread.settledOverride === "settled",
- supportsSettlement:
- serverConfigs.get(activeThread.environmentId)?.environment.capabilities
- .threadSettlement === true,
- target,
- pinnedOrder: pinnedKeys,
- pinnedKeysById,
- reorderableKeys: draggableThreadKeys,
- activeOrder: activeKeys,
- activeKeysById,
- activeReorderableKeys: activeReorderableThreadKeys,
- activeTimeOrdered: workingShelfEnabled,
- });
- if (plan.kind === "none") return;
- if (plan.kind === "settle" && settlingThreadKeysRef.current.has(activeKey)) return;
- const assignments =
- plan.kind === "pin"
- ? [
- ...(plan.orderKey === undefined ? [] : [{ id: activeKey, orderKey: plan.orderKey }]),
- ...plan.extraAssignments,
- ]
- : plan.kind === "reorder-pinned" || plan.kind === "move-active"
- ? plan.assignments
- : [];
- if (
- !checkThreadOperations(
- [activeKey, ...assignments.map(({ id }) => id)].flatMap((key) => {
- const thread = threadByKey.get(key);
- return thread ? [thread] : [];
- }),
- )
+ })();
+ const handleThreadDragEnd = (event: DragEndEvent) => {
+ 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 activeThread = threadByKey.get(activeKey);
+ if (activeSection === undefined || target === null || activeThread === undefined) return;
+ const threadRef = scopeThreadRef(activeThread.environmentId, activeThread.id);
+ const plan = planSidebarThreadDrop({
+ activeKey,
+ activeSection,
+ activePinned: activeThread.pinnedAt != null,
+ activeSettled: activeThread.settledOverride === "settled",
+ supportsSettlement:
+ serverConfigs.get(activeThread.environmentId)?.environment.capabilities.threadSettlement ===
+ true,
+ target,
+ pinnedOrder: pinnedKeys,
+ pinnedKeysById,
+ reorderableKeys: draggableThreadKeys,
+ activeOrder: activeKeys,
+ activeKeysById,
+ activeReorderableKeys: activeReorderableThreadKeys,
+ activeTimeOrdered: workingShelfEnabled,
+ });
+ if (plan.kind === "none") return;
+ if (plan.kind === "settle" && settlingThreadKeysRef.current.has(activeKey)) return;
+ const assignments =
+ plan.kind === "pin"
+ ? [
+ ...(plan.orderKey === undefined ? [] : [{ id: activeKey, orderKey: plan.orderKey }]),
+ ...plan.extraAssignments,
+ ]
+ : plan.kind === "reorder-pinned" || plan.kind === "move-active"
+ ? plan.assignments
+ : [];
+ if (
+ !checkThreadOperations(
+ [activeKey, ...assignments.map(({ id }) => id)].flatMap((key) => {
+ const thread = threadByKey.get(key);
+ return thread ? [thread] : [];
+ }),
)
- return;
- const drop = {
- key: activeKey,
- sourceSection: activeSection,
- section: target.section,
- occurredAt: new Date().toISOString(),
- clearsSnooze:
- plan.kind === "pin" ||
- plan.kind === "settle" ||
- (plan.kind === "move-active" && plan.unsnooze),
- order: plan.kind === "settle" ? null : plan.order,
- keysAtDrop: target.section === "active" ? activeKeysById : pinnedKeysById,
- assignedKeys: new Map(assignments.map(({ id, orderKey }) => [id, orderKey])),
+ )
+ return;
+ const drop = {
+ key: activeKey,
+ sourceSection: activeSection,
+ section: target.section,
+ occurredAt: new Date().toISOString(),
+ clearsSnooze:
+ plan.kind === "pin" ||
+ plan.kind === "settle" ||
+ (plan.kind === "move-active" && plan.unsnooze),
+ order: plan.kind === "settle" ? null : plan.order,
+ keysAtDrop: target.section === "active" ? activeKeysById : pinnedKeysById,
+ assignedKeys: new Map(assignments.map(({ id, orderKey }) => [id, orderKey])),
+ };
+ setOptimisticDrop(drop);
+ void (async () => {
+ const run = async (
+ operation: Promise<AtomCommandResult<unknown, unknown>>,
+ title: string,
+ ) => {
+ const result = await operation;
+ if (result._tag === "Success") return true;
+ setOptimisticDrop((current) => (current === drop ? null : current));
+ if (!isAtomCommandInterrupted(result)) {
+ const error = squashAtomCommandFailure(result);
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title,
+ description: error instanceof Error ? error.message : "An error occurred.",
+ }),
+ );
+ }
+ return false;
};
- setOptimisticDrop(drop);
- void (async () => {
- const run = async (
- operation: Promise<AtomCommandResult<unknown, unknown>>,
- title: string,
- ) => {
- const result = await operation;
- if (result._tag === "Success") return true;
- setOptimisticDrop((current) => (current === drop ? null : current));
- if (!isAtomCommandInterrupted(result)) {
- const error = squashAtomCommandFailure(result);
- toastManager.add(
- stackedThreadToast({
- type: "error",
- title,
- description: error instanceof Error ? error.message : "An error occurred.",
- }),
- );
- }
- return false;
- };
- switch (plan.kind) {
- case "settle": {
- settlingThreadKeysRef.current.add(activeKey);
- const navigateAfterSettle = planForwardNavigation(activeKey);
- const settled = await run(settleThread(threadRef), "Failed to settle thread").finally(
- () => settlingThreadKeysRef.current.delete(activeKey),
- );
- if (
- settled &&
- shouldNavigateAfterThreadPark({
- threadKey: activeKey,
- currentThreadKey: routeThreadKeyRef.current,
- action: "settle",
- now: new Date().toISOString(),
- thread: readThreadShell(threadRef),
- })
- )
- navigateAfterSettle?.();
- return;
- }
- case "move-active":
- if (plan.unpin && !(await run(unpinThread(threadRef), "Failed to unpin thread")))
- return;
- if (
- plan.unsettle &&
- !(await run(unsettleThread(threadRef), "Failed to un-settle thread"))
- )
- return;
- if (plan.unsnooze && !(await run(unsnoozeThread(threadRef), "Failed to wake thread")))
- return;
- break;
- case "pin":
- if (
- !(await run(
- pinThread(
- threadRef,
- plan.orderKey === undefined ? {} : { orderKey: plan.orderKey },
- ),
- "Failed to pin thread",
- ))
- )
- return;
- break;
- case "reorder-pinned":
- break;
+ switch (plan.kind) {
+ case "settle": {
+ settlingThreadKeysRef.current.add(activeKey);
+ const navigateAfterSettle = planForwardNavigation(activeKey);
+ const settled = await run(settleThread(threadRef), "Failed to settle thread").finally(
+ () => settlingThreadKeysRef.current.delete(activeKey),
+ );
+ if (
+ settled &&
+ shouldNavigateAfterThreadPark({
+ threadKey: activeKey,
+ currentThreadKey: routeThreadKeyRef.current,
+ action: "settle",
+ now: new Date().toISOString(),
+ thread: readThreadShell(threadRef),
+ })
+ )
+ navigateAfterSettle?.();
+ return;
}
- const keyWrites = plan.kind === "pin" ? plan.extraAssignments : plan.assignments;
- for (const assignment of keyWrites) {
- const thread = threadByKey.get(assignment.id);
- if (thread === undefined) continue;
+ case "move-active":
+ if (plan.unpin && !(await run(unpinThread(threadRef), "Failed to unpin thread"))) return;
+ if (
+ plan.unsettle &&
+ !(await run(unsettleThread(threadRef), "Failed to un-settle thread"))
+ )
+ return;
+ if (plan.unsnooze && !(await run(unsnoozeThread(threadRef), "Failed to wake thread")))
+ return;
+ break;
+ case "pin":
if (
!(await run(
- (plan.kind === "move-active" ? reorderActiveThread : reorderPinnedThread)(
- scopeThreadRef(thread.environmentId, thread.id),
- assignment.orderKey,
- ),
- plan.kind === "move-active"
- ? "Failed to reorder active threads"
- : "Failed to reorder pinned threads",
+ pinThread(threadRef, plan.orderKey === undefined ? {} : { orderKey: plan.orderKey }),
+ "Failed to pin thread",
))
)
return;
- }
- })();
- },
- [
- activeKeysById,
- pinnedKeysById,
- serverConfigs,
- activeKeys,
- activeReorderableThreadKeys,
- draggableThreadKeys,
- pinThread,
- pinnedKeys,
- planForwardNavigation,
- reorderPinnedThread,
- reorderActiveThread,
- sectionByThreadKey,
- settleThread,
- sidebarListItems,
- threadByKey,
- unpinThread,
- unsettleThread,
- unsnoozeThread,
- workingShelfEnabled,
- ],
- );
- const snoozingThreadKeysRef = useRef(new Set<string>());
- const performSnooze = useCallback(
- async (
- threadRef: ScopedThreadRef,
- preset: Pick<SnoozePreset, "snoozedUntil">,
- opts: { coSnoozingKeys?: ReadonlySet<string> } = {},
- ) => {
- const threadKey = scopedThreadKey(threadRef);
- if (snoozingThreadKeysRef.current.has(threadKey)) {
- return { status: "skipped" } as const;
+ break;
+ case "reorder-pinned":
+ break;
}
- snoozingThreadKeysRef.current.add(threadKey);
- try {
- const navigateAfterSnooze = planForwardNavigation(threadKey, opts.coSnoozingKeys);
- const result = await snoozeThread(threadRef, preset.snoozedUntil);
- if (result._tag === "Failure") {
- return isAtomCommandInterrupted(result)
- ? ({ status: "interrupted" } as const)
- : ({ status: "failure", error: squashAtomCommandFailure(result) } as const);
- }
+ const keyWrites = plan.kind === "pin" ? plan.extraAssignments : plan.assignments;
+ for (const assignment of keyWrites) {
+ const thread = threadByKey.get(assignment.id);
+ if (thread === undefined) continue;
if (
- shouldNavigateAfterThreadPark({
- threadKey,
- currentThreadKey: routeThreadKeyRef.current,
- action: "snooze",
- now: new Date().toISOString(),
- thread: readThreadShell(threadRef),
- })
- ) {
- navigateAfterSnooze?.();
- }
- return { status: "success" } as const;
- } finally {
- snoozingThreadKeysRef.current.delete(threadKey);
- }
- },
- [planForwardNavigation, snoozeThread],
- );
- const attemptSnooze = useCallback(
- (
- threadRef: ScopedThreadRef,
- preset: Pick<SnoozePreset, "snoozedUntil">,
- opts: { coSnoozingKeys?: ReadonlySet<string> } = {},
- ) => {
- void (async () => {
- const outcome = await performSnooze(threadRef, preset, opts);
- if (outcome.status === "failure") {
- toastManager.add(
- stackedThreadToast({
- type: "error",
- title: "Failed to snooze thread",
- description:
- outcome.error instanceof Error ? outcome.error.message : "An error occurred.",
- }),
- );
+ !(await run(
+ (plan.kind === "move-active" ? reorderActiveThread : reorderPinnedThread)(
+ scopeThreadRef(thread.environmentId, thread.id),
+ assignment.orderKey,
+ ),
+ plan.kind === "move-active"
+ ? "Failed to reorder active threads"
+ : "Failed to reorder pinned threads",
+ ))
+ )
return;
- }
- })();
- },
- [performSnooze],
- );
+ }
+ })();
+ };
+ const performSnooze = async (
+ threadRef: ScopedThreadRef,
+ preset: Pick<SnoozePreset, "snoozedUntil">,
+ opts: { coSnoozingKeys?: ReadonlySet<string> } = {},
+ ) => {
+ const threadKey = scopedThreadKey(threadRef);
+ if (snoozingThreadKeysRef.current.has(threadKey)) {
+ return { status: "skipped" } as const;
+ }
+ snoozingThreadKeysRef.current.add(threadKey);
+ const navigateAfterSnooze = planForwardNavigation(threadKey, opts.coSnoozingKeys);
+ const result = await snoozeThread(threadRef, preset.snoozedUntil);
+ snoozingThreadKeysRef.current.delete(threadKey);
+ if (result._tag === "Failure") {
+ return isAtomCommandInterrupted(result)
+ ? ({ status: "interrupted" } as const)
+ : ({ status: "failure", error: squashAtomCommandFailure(result) } as const);
+ }
+ if (
+ shouldNavigateAfterThreadPark({
+ threadKey,
+ currentThreadKey: routeThreadKeyRef.current,
+ action: "snooze",
+ now: new Date().toISOString(),
+ thread: readThreadShell(threadRef),
+ })
+ ) {
+ navigateAfterSnooze?.();
+ }
+ return { status: "success" } as const;
+ };
+ const attemptSnooze = (
+ threadRef: ScopedThreadRef,
+ preset: Pick<SnoozePreset, "snoozedUntil">,
+ opts: { coSnoozingKeys?: ReadonlySet<string> } = {},
+ ) => {
+ void (async () => {
+ const outcome = await performSnooze(threadRef, preset, opts);
+ if (outcome.status === "failure") {
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title: "Failed to snooze thread",
+ description:
+ outcome.error instanceof Error ? outcome.error.message : "An error occurred.",
+ }),
+ );
+ return;
+ }
+ })();
+ };
- const removeFromSelection = useThreadSelectionStore((s) => s.removeFromSelection);
const handleMultiSelectContextMenu = useStableCallback(
async (position: { x: number; y: number }) => {
const api = readLocalApi();
@@ -4602,8 +4382,6 @@ export default function Sidebar() {
threadByKey,
]);
- const shortcutModifiers = useShortcutModifierState();
- const terminalFocused = useTerminalFocus();
const shouldShowJumpHintsNow = shouldShowThreadJumpHintsForModifiers(
shortcutModifiers,
keybindings,
@@ -4622,23 +4400,20 @@ export default function Sidebar() {
updateThreadJumpHintsVisibility(shouldShowJumpHintsNow);
}, [shouldShowJumpHintsNow, updateThreadJumpHintsVisibility]);
- const handleNewThreadClick = useCallback(
- (event?: ReactMouseEvent) => {
- if (shouldCreateNewThreadInCurrentProject(event?.shiftKey ?? false, projectGroups.length)) {
- if (isMobile) setOpenMobile(false);
- void startNewThreadFromContext({
- activeDraftThread: newThreadContext.activeDraftThread,
- activeThread: newThreadContext.activeThread ?? undefined,
- defaultProjectRef: newThreadContext.defaultProjectRef,
- handleNewThread: newThreadContext.handleNewThread,
- });
- return;
- }
+ const handleNewThreadClick = (event?: ReactMouseEvent) => {
+ if (shouldCreateNewThreadInCurrentProject(event?.shiftKey ?? false, projectGroups.length)) {
if (isMobile) setOpenMobile(false);
- openCommandPalette({ open: "new-thread-in" });
- },
- [isMobile, newThreadContext, projectGroups.length, setOpenMobile],
- );
+ void startNewThreadFromContext({
+ activeDraftThread: newThreadContext.activeDraftThread,
+ activeThread: newThreadContext.activeThread ?? undefined,
+ defaultProjectRef: newThreadContext.defaultProjectRef,
+ handleNewThread: newThreadContext.handleNewThread,
+ });
+ return;
+ }
+ if (isMobile) setOpenMobile(false);
+ openCommandPalette({ open: "new-thread-in" });
+ };
const newThreadShortcutLabel =
shortcutLabelForCommand(keybindings, "chat.new") ??
@@ -5001,10 +4776,6 @@ export default function Sidebar() {
onUnpin={attemptUnpin}
onAcknowledgeWoke={acknowledgeWoke}
onFileDropThreads={handleThreadFileDrop}
- changeRequestSnapshot={
- changeRequestSnapshotByKey.get(threadKey) ?? null
- }
- onChangeRequestSnapshot={setThreadChangeRequestSnapshot}
/>
);
};
diff --git a/apps/web/src/components/ThreadStatusIndicators.tsx b/apps/web/src/components/ThreadStatusIndicators.tsx
index 88c91c9aaf7..626345bc006 100644
--- a/apps/web/src/components/ThreadStatusIndicators.tsx
+++ b/apps/web/src/components/ThreadStatusIndicators.tsx
@@ -17,7 +17,6 @@ import {
import { Atom } from "effect/reactivity";
import { FolderGit2Icon, TerminalIcon } from "lucide-react";
import { useCallback, useMemo } from "react";
-import { appAtomRegistry } from "../rpc/atomRegistry";
import { useEnvironment, usePrimaryEnvironmentId } from "../state/environments";
import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
import { useProject } from "../state/entities";
@@ -536,27 +535,6 @@ export function threadChangeRequestSnapshotsEqual(
);
}
-export function setThreadChangeRequestSnapshot(
- threadKey: string,
- snapshot: ThreadChangeRequestSnapshot | null,
-): void {
- appAtomRegistry.modify(threadChangeRequestSnapshotsAtom, (current) => {
- const existing = current.get(threadKey);
- if (snapshot === null) {
- if (existing === undefined) return [false, current];
- const next = new Map(current);
- next.delete(threadKey);
- return [true, next];
- }
- if (existing !== undefined && threadChangeRequestSnapshotsEqual(existing, snapshot)) {
- return [false, current];
- }
- const next = new Map(current);
- next.set(threadKey, snapshot);
- return [true, next];
- });
-}
-
export function nextThreadChangeRequestSnapshot(input: {
threadBranch: string | null;
gitStatus: VcsStatusResult | null;