Skip to content

Commit 130f4a2

Browse files
perf(web): a minute tick no longer re-renders every sidebar thread row (#20814)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 489d08a commit 130f4a2

2 files changed

Lines changed: 64 additions & 60 deletions

File tree

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,19 @@ function compactSidebarTimeLabel(label: string): string {
758758
return label.endsWith(" ago") ? label.slice(0, -4) : label;
759759
}
760760

761-
function threadTimeLabel(thread: SidebarThreadSummary): string {
762-
const timestamp = thread.latestUserMessageAt ?? thread.updatedAt;
763-
return compactSidebarTimeLabel(formatRelativeTimeLabel(timestamp));
761+
// Subscribes to the minute tick itself, so only the label re-renders each
762+
// minute. The minute is passed as the clock so it is the formatter's input.
763+
function RelativeTimeLabel(props: { timestamp: string | null }) {
764+
const nowMinute = useNowMinute();
765+
return props.timestamp === null
766+
? null
767+
: compactSidebarTimeLabel(
768+
formatRelativeTimeLabel(props.timestamp, Date.parse(`${nowMinute}:00.000Z`)),
769+
);
764770
}
765771

766-
function settledTimeLabel(thread: SidebarThreadSummary): string {
767-
const timestamp = resolveSettledThreadTimestamp(thread);
768-
return timestamp === null ? "" : compactSidebarTimeLabel(formatRelativeTimeLabel(timestamp));
772+
function threadTimeTimestamp(thread: SidebarThreadSummary): string {
773+
return thread.latestUserMessageAt ?? thread.updatedAt;
769774
}
770775

771776
function JumpHintBadge(props: { label: string }) {
@@ -1052,18 +1057,25 @@ type SortableThreadRowBag = Pick<
10521057
const SIDEBAR_SORTABLE_DRAG_DISABLED = { draggable: true } as const;
10531058
const SIDEBAR_SORTABLE_DRAG_ENABLED = { draggable: false } as const;
10541059

1055-
function SortableThreadRow(props: {
1056-
id: string;
1057-
disabled: boolean;
1058-
contextDrag: boolean;
1059-
environmentId: EnvironmentId;
1060-
children: (bag: SortableThreadRowBag) => ReactNode;
1061-
}) {
1062-
const canOperateThread = useEnvironmentScope(props.environmentId, AuthOrchestrationOperateScope);
1060+
// Takes the row's props rather than a render function: a closure from the
1061+
// sidebar body is new on every sidebar render, which rebuilt the bag and
1062+
// re-rendered every memoized row with it.
1063+
function SortableThreadRow(
1064+
props: Omit<SidebarThreadRowProps, "sortable"> & {
1065+
sortableId: string;
1066+
sortableDisabled: boolean;
1067+
contextDrag: boolean;
1068+
},
1069+
) {
1070+
const { sortableId, sortableDisabled, contextDrag, ...rowProps } = props;
1071+
const canOperateThread = useEnvironmentScope(
1072+
rowProps.thread.environmentId,
1073+
AuthOrchestrationOperateScope,
1074+
);
10631075
const { listeners, setNodeRef, transform, transition, isDragging } = useSortable({
1064-
id: props.id,
1076+
id: sortableId,
10651077
disabled:
1066-
props.disabled || !canOperateThread
1078+
sortableDisabled || !canOperateThread
10671079
? SIDEBAR_SORTABLE_DRAG_DISABLED
10681080
: SIDEBAR_SORTABLE_DRAG_ENABLED,
10691081
animateLayoutChanges: animateSidebarLayoutChanges,
@@ -1073,11 +1085,12 @@ function SortableThreadRow(props: {
10731085
const bag = {
10741086
listeners,
10751087
setNodeRef,
1076-
transform: props.contextDrag ? null : transform,
1077-
transition: props.contextDrag && isDragging ? "transform 150ms ease-out" : transition,
1078-
isDragging: isDragging && !props.contextDrag,
1088+
transform: contextDrag ? null : transform,
1089+
transition: contextDrag && isDragging ? "transform 150ms ease-out" : transition,
1090+
isDragging: isDragging && !contextDrag,
10791091
};
1080-
return props.children(bag);
1092+
// Keyed by variant so a card/slim switch remounts the row.
1093+
return <SidebarThreadRow key={rowProps.variant} {...rowProps} sortable={bag} />;
10811094
}
10821095

10831096
const draftSurfaceClassName = "bg-warning/4 hover:bg-warning/8";
@@ -1446,7 +1459,7 @@ const dropVerbBadge: Record<SidebarDropVerb, ReactNode> = {
14461459
),
14471460
};
14481461

1449-
const SidebarThreadRow = memo(function SidebarThreadRow(props: {
1462+
type SidebarThreadRowProps = {
14501463
thread: SidebarThreadSummary;
14511464
variant: "card" | "slim";
14521465
variantAction: SidebarSweepAction;
@@ -1492,7 +1505,9 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
14921505
onUnpin: (threadRef: ScopedThreadRef) => void;
14931506
onAcknowledgeWoke: (threadRef: ScopedThreadRef, visitedAt: string) => void;
14941507
onFileDropThreads?: ((threadRef: ScopedThreadRef, files: File[]) => void) | undefined;
1495-
}) {
1508+
};
1509+
1510+
const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowProps) {
14961511
const {
14971512
isRenaming,
14981513
onCancelRename,
@@ -2068,9 +2083,13 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
20682083
</Tooltip>
20692084
) : (
20702085
<span className="text-xs">
2071-
{variantAction === "unsettle"
2072-
? settledTimeLabel(thread)
2073-
: threadTimeLabel(thread)}
2086+
<RelativeTimeLabel
2087+
timestamp={
2088+
variantAction === "unsettle"
2089+
? resolveSettledThreadTimestamp(thread)
2090+
: threadTimeTimestamp(thread)
2091+
}
2092+
/>
20742093
</span>
20752094
)}
20762095
</span>
@@ -2260,7 +2279,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
22602279
</span>
22612280
)
22622281
) : (
2263-
threadTimeLabel(thread)
2282+
<RelativeTimeLabel timestamp={threadTimeTimestamp(thread)} />
22642283
)}
22652284
</span>
22662285
{props.settlementSupported || showSnoozeButton || hasUnsentDraft ? (
@@ -2544,7 +2563,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
25442563
/>
25452564
</span>
25462565
<span className="w-8 shrink-0 text-right text-xs text-muted-foreground/55 tabular-nums">
2547-
{threadTimeLabel(thread)}
2566+
<RelativeTimeLabel timestamp={threadTimeTimestamp(thread)} />
25482567
</span>
25492568
</span>
25502569
{props.searchMatch ? (
@@ -4675,10 +4694,9 @@ export default function Sidebar() {
46754694
)}
46764695
>
46774696
{(() => {
4678-
const renderThreadRowInner = (
4697+
const renderThreadRow = (
46794698
thread: EnvironmentThreadShell,
46804699
section: SidebarSection,
4681-
sortable?: SortableThreadRowBag,
46824700
) => {
46834701
const threadKey = scopedThreadKey(
46844702
scopeThreadRef(thread.environmentId, thread.id),
@@ -4687,8 +4705,16 @@ export default function Sidebar() {
46874705
section === "active" || section === "pinned" || section === "working";
46884706
const rowVariant = isCard ? "card" : "slim";
46894707
return (
4690-
<SidebarThreadRow
4691-
key={`${threadKey}:${rowVariant}`}
4708+
<SortableThreadRow
4709+
key={threadKey}
4710+
sortableId={threadKey}
4711+
contextDrag={isContextDrag}
4712+
sortableDisabled={
4713+
renamingThreadKey === threadKey ||
4714+
section === "working" ||
4715+
!draggableThreadKeys.has(threadKey) ||
4716+
optimisticDrop !== null
4717+
}
46924718
thread={thread}
46934719
variant={rowVariant}
46944720
variantAction={
@@ -4711,7 +4737,6 @@ export default function Sidebar() {
47114737
.threadPinning === true
47124738
}
47134739
isPinned={thread.pinnedAt != null}
4714-
sortable={sortable}
47154740
dropVerb={
47164741
dragState?.activeKey === threadKey
47174742
? resolveSidebarDropVerb(dragState.activeSection, dragTargetSection)
@@ -4778,30 +4803,6 @@ export default function Sidebar() {
47784803
/>
47794804
);
47804805
};
4781-
const renderThreadRow = (
4782-
thread: EnvironmentThreadShell,
4783-
section: SidebarSection,
4784-
) => {
4785-
const threadKey = scopedThreadKey(
4786-
scopeThreadRef(thread.environmentId, thread.id),
4787-
);
4788-
return (
4789-
<SortableThreadRow
4790-
key={threadKey}
4791-
id={threadKey}
4792-
contextDrag={isContextDrag}
4793-
environmentId={thread.environmentId}
4794-
disabled={
4795-
renamingThreadKey === threadKey ||
4796-
section === "working" ||
4797-
!draggableThreadKeys.has(threadKey) ||
4798-
optimisticDrop !== null
4799-
}
4800-
>
4801-
{(bag) => renderThreadRowInner(thread, section, bag)}
4802-
</SortableThreadRow>
4803-
);
4804-
};
48054806
const from = isContextDrag ? null : (dragState?.activeSection ?? null);
48064807
const items: ReactNode[] = [
48074808
<SidebarDraftBlock

‎apps/web/src/timestampFormat.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,16 @@ export function formatRelativeElapsed(elapsedMs: number): RelativeTimeParts {
249249
return { value: `${days}d`, suffix: "ago" };
250250
}
251251

252-
export function formatRelativeTime(isoDate: string): RelativeTimeParts | null {
253-
const elapsedMs = relativeTimeElapsedMs(isoDate);
252+
export function formatRelativeTime(
253+
isoDate: string,
254+
nowMs: number = Date.now(),
255+
): RelativeTimeParts | null {
256+
const elapsedMs = relativeTimeElapsedMs(isoDate, nowMs);
254257
return elapsedMs === null ? null : formatRelativeElapsed(elapsedMs);
255258
}
256259

257-
export function formatRelativeTimeLabel(isoDate: string) {
258-
const relative = formatRelativeTime(isoDate);
260+
export function formatRelativeTimeLabel(isoDate: string, nowMs: number = Date.now()) {
261+
const relative = formatRelativeTime(isoDate, nowMs);
259262
if (!relative) return "";
260263
return relative.suffix ? `${relative.value} ${relative.suffix}` : relative.value;
261264
}

0 commit comments

Comments
 (0)