Skip to content
TypeScript

perf(web): a minute tick no longer re-renders every sidebar thread row (#20814)

Julius Marminge130f4a245717
diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx
index 0a9ce25a10a..9eabd853a1f 100644
--- a/apps/web/src/components/Sidebar.tsx
+++ b/apps/web/src/components/Sidebar.tsx
@@ -758,14 +758,19 @@ function compactSidebarTimeLabel(label: string): string {
return label.endsWith(" ago") ? label.slice(0, -4) : label;
}
-function threadTimeLabel(thread: SidebarThreadSummary): string {
- const timestamp = thread.latestUserMessageAt ?? thread.updatedAt;
- return compactSidebarTimeLabel(formatRelativeTimeLabel(timestamp));
+// Subscribes to the minute tick itself, so only the label re-renders each
+// minute. The minute is passed as the clock so it is the formatter's input.
+function RelativeTimeLabel(props: { timestamp: string | null }) {
+ const nowMinute = useNowMinute();
+ return props.timestamp === null
+ ? null
+ : compactSidebarTimeLabel(
+ formatRelativeTimeLabel(props.timestamp, Date.parse(`${nowMinute}:00.000Z`)),
+ );
}
-function settledTimeLabel(thread: SidebarThreadSummary): string {
- const timestamp = resolveSettledThreadTimestamp(thread);
- return timestamp === null ? "" : compactSidebarTimeLabel(formatRelativeTimeLabel(timestamp));
+function threadTimeTimestamp(thread: SidebarThreadSummary): string {
+ return thread.latestUserMessageAt ?? thread.updatedAt;
}
function JumpHintBadge(props: { label: string }) {
@@ -1052,18 +1057,25 @@ type SortableThreadRowBag = Pick<
const SIDEBAR_SORTABLE_DRAG_DISABLED = { draggable: true } as const;
const SIDEBAR_SORTABLE_DRAG_ENABLED = { draggable: false } as const;
-function SortableThreadRow(props: {
- id: string;
- disabled: boolean;
- contextDrag: boolean;
- environmentId: EnvironmentId;
- children: (bag: SortableThreadRowBag) => ReactNode;
-}) {
- const canOperateThread = useEnvironmentScope(props.environmentId, AuthOrchestrationOperateScope);
+// Takes the row's props rather than a render function: a closure from the
+// sidebar body is new on every sidebar render, which rebuilt the bag and
+// re-rendered every memoized row with it.
+function SortableThreadRow(
+ props: Omit<SidebarThreadRowProps, "sortable"> & {
+ sortableId: string;
+ sortableDisabled: boolean;
+ contextDrag: boolean;
+ },
+) {
+ const { sortableId, sortableDisabled, contextDrag, ...rowProps } = props;
+ const canOperateThread = useEnvironmentScope(
+ rowProps.thread.environmentId,
+ AuthOrchestrationOperateScope,
+ );
const { listeners, setNodeRef, transform, transition, isDragging } = useSortable({
- id: props.id,
+ id: sortableId,
disabled:
- props.disabled || !canOperateThread
+ sortableDisabled || !canOperateThread
? SIDEBAR_SORTABLE_DRAG_DISABLED
: SIDEBAR_SORTABLE_DRAG_ENABLED,
animateLayoutChanges: animateSidebarLayoutChanges,
@@ -1073,11 +1085,12 @@ function SortableThreadRow(props: {
const bag = {
listeners,
setNodeRef,
- transform: props.contextDrag ? null : transform,
- transition: props.contextDrag && isDragging ? "transform 150ms ease-out" : transition,
- isDragging: isDragging && !props.contextDrag,
+ transform: contextDrag ? null : transform,
+ transition: contextDrag && isDragging ? "transform 150ms ease-out" : transition,
+ isDragging: isDragging && !contextDrag,
};
- return props.children(bag);
+ // Keyed by variant so a card/slim switch remounts the row.
+ return <SidebarThreadRow key={rowProps.variant} {...rowProps} sortable={bag} />;
}
const draftSurfaceClassName = "bg-warning/4 hover:bg-warning/8";
@@ -1446,7 +1459,7 @@ const dropVerbBadge: Record<SidebarDropVerb, ReactNode> = {
),
};
-const SidebarThreadRow = memo(function SidebarThreadRow(props: {
+type SidebarThreadRowProps = {
thread: SidebarThreadSummary;
variant: "card" | "slim";
variantAction: SidebarSweepAction;
@@ -1492,7 +1505,9 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
onUnpin: (threadRef: ScopedThreadRef) => void;
onAcknowledgeWoke: (threadRef: ScopedThreadRef, visitedAt: string) => void;
onFileDropThreads?: ((threadRef: ScopedThreadRef, files: File[]) => void) | undefined;
-}) {
+};
+
+const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowProps) {
const {
isRenaming,
onCancelRename,
@@ -2068,9 +2083,13 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
</Tooltip>
) : (
<span className="text-xs">
- {variantAction === "unsettle"
- ? settledTimeLabel(thread)
- : threadTimeLabel(thread)}
+ <RelativeTimeLabel
+ timestamp={
+ variantAction === "unsettle"
+ ? resolveSettledThreadTimestamp(thread)
+ : threadTimeTimestamp(thread)
+ }
+ />
</span>
)}
</span>
@@ -2260,7 +2279,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
</span>
)
) : (
- threadTimeLabel(thread)
+ <RelativeTimeLabel timestamp={threadTimeTimestamp(thread)} />
)}
</span>
{props.settlementSupported || showSnoozeButton || hasUnsentDraft ? (
@@ -2544,7 +2563,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
/>
</span>
<span className="w-8 shrink-0 text-right text-xs text-muted-foreground/55 tabular-nums">
- {threadTimeLabel(thread)}
+ <RelativeTimeLabel timestamp={threadTimeTimestamp(thread)} />
</span>
</span>
{props.searchMatch ? (
@@ -4675,10 +4694,9 @@ export default function Sidebar() {
)}
>
{(() => {
- const renderThreadRowInner = (
+ const renderThreadRow = (
thread: EnvironmentThreadShell,
section: SidebarSection,
- sortable?: SortableThreadRowBag,
) => {
const threadKey = scopedThreadKey(
scopeThreadRef(thread.environmentId, thread.id),
@@ -4687,8 +4705,16 @@ export default function Sidebar() {
section === "active" || section === "pinned" || section === "working";
const rowVariant = isCard ? "card" : "slim";
return (
- <SidebarThreadRow
- key={`${threadKey}:${rowVariant}`}
+ <SortableThreadRow
+ key={threadKey}
+ sortableId={threadKey}
+ contextDrag={isContextDrag}
+ sortableDisabled={
+ renamingThreadKey === threadKey ||
+ section === "working" ||
+ !draggableThreadKeys.has(threadKey) ||
+ optimisticDrop !== null
+ }
thread={thread}
variant={rowVariant}
variantAction={
@@ -4711,7 +4737,6 @@ export default function Sidebar() {
.threadPinning === true
}
isPinned={thread.pinnedAt != null}
- sortable={sortable}
dropVerb={
dragState?.activeKey === threadKey
? resolveSidebarDropVerb(dragState.activeSection, dragTargetSection)
@@ -4778,30 +4803,6 @@ export default function Sidebar() {
/>
);
};
- const renderThreadRow = (
- thread: EnvironmentThreadShell,
- section: SidebarSection,
- ) => {
- const threadKey = scopedThreadKey(
- scopeThreadRef(thread.environmentId, thread.id),
- );
- return (
- <SortableThreadRow
- key={threadKey}
- id={threadKey}
- contextDrag={isContextDrag}
- environmentId={thread.environmentId}
- disabled={
- renamingThreadKey === threadKey ||
- section === "working" ||
- !draggableThreadKeys.has(threadKey) ||
- optimisticDrop !== null
- }
- >
- {(bag) => renderThreadRowInner(thread, section, bag)}
- </SortableThreadRow>
- );
- };
const from = isContextDrag ? null : (dragState?.activeSection ?? null);
const items: ReactNode[] = [
<SidebarDraftBlock
diff --git a/apps/web/src/timestampFormat.ts b/apps/web/src/timestampFormat.ts
index 2d80bdfeaf1..c1d1b2446c1 100644
--- a/apps/web/src/timestampFormat.ts
+++ b/apps/web/src/timestampFormat.ts
@@ -249,13 +249,16 @@ export function formatRelativeElapsed(elapsedMs: number): RelativeTimeParts {
return { value: `${days}d`, suffix: "ago" };
}
-export function formatRelativeTime(isoDate: string): RelativeTimeParts | null {
- const elapsedMs = relativeTimeElapsedMs(isoDate);
+export function formatRelativeTime(
+ isoDate: string,
+ nowMs: number = Date.now(),
+): RelativeTimeParts | null {
+ const elapsedMs = relativeTimeElapsedMs(isoDate, nowMs);
return elapsedMs === null ? null : formatRelativeElapsed(elapsedMs);
}
-export function formatRelativeTimeLabel(isoDate: string) {
- const relative = formatRelativeTime(isoDate);
+export function formatRelativeTimeLabel(isoDate: string, nowMs: number = Date.now()) {
+ const relative = formatRelativeTime(isoDate, nowMs);
if (!relative) return "";
return relative.suffix ? `${relative.value} ${relative.suffix}` : relative.value;
}