Skip to content

Commit 6badbc9

Browse files
perf(web): a minute tick no longer re-renders every sidebar thread row
SortableThreadRow took a render-prop closure that Sidebar rebuilt on every render, so the compiler rebuilt each row's sortable bag and every memoized SidebarThreadRow re-rendered. It now takes the row's props directly, and the relative-time labels subscribe to the minute tick themselves. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b987b57 commit 6badbc9

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
@@ -761,14 +761,19 @@ function compactSidebarTimeLabel(label: string): string {
761761
return label.endsWith(" ago") ? label.slice(0, -4) : label;
762762
}
763763

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

769-
function settledTimeLabel(thread: SidebarThreadSummary): string {
770-
const timestamp = resolveSettledThreadTimestamp(thread);
771-
return timestamp === null ? "" : compactSidebarTimeLabel(formatRelativeTimeLabel(timestamp));
775+
function threadTimeTimestamp(thread: SidebarThreadSummary): string {
776+
return thread.latestUserMessageAt ?? thread.updatedAt;
772777
}
773778

774779
function JumpHintBadge(props: { label: string }) {
@@ -1055,18 +1060,25 @@ type SortableThreadRowBag = Pick<
10551060
const SIDEBAR_SORTABLE_DRAG_DISABLED = { draggable: true } as const;
10561061
const SIDEBAR_SORTABLE_DRAG_ENABLED = { draggable: false } as const;
10571062

1058-
function SortableThreadRow(props: {
1059-
id: string;
1060-
disabled: boolean;
1061-
contextDrag: boolean;
1062-
environmentId: EnvironmentId;
1063-
children: (bag: SortableThreadRowBag) => ReactNode;
1064-
}) {
1065-
const canOperateThread = useEnvironmentScope(props.environmentId, AuthOrchestrationOperateScope);
1063+
// Takes the row's props rather than a render function: a closure from the
1064+
// sidebar body is new on every sidebar render, which rebuilt the bag and
1065+
// re-rendered every memoized row with it.
1066+
function SortableThreadRow(
1067+
props: Omit<SidebarThreadRowProps, "sortable"> & {
1068+
sortableId: string;
1069+
sortableDisabled: boolean;
1070+
contextDrag: boolean;
1071+
},
1072+
) {
1073+
const { sortableId, sortableDisabled, contextDrag, ...rowProps } = props;
1074+
const canOperateThread = useEnvironmentScope(
1075+
rowProps.thread.environmentId,
1076+
AuthOrchestrationOperateScope,
1077+
);
10661078
const { listeners, setNodeRef, transform, transition, isDragging } = useSortable({
1067-
id: props.id,
1079+
id: sortableId,
10681080
disabled:
1069-
props.disabled || !canOperateThread
1081+
sortableDisabled || !canOperateThread
10701082
? SIDEBAR_SORTABLE_DRAG_DISABLED
10711083
: SIDEBAR_SORTABLE_DRAG_ENABLED,
10721084
animateLayoutChanges: animateSidebarLayoutChanges,
@@ -1076,11 +1088,12 @@ function SortableThreadRow(props: {
10761088
const bag = {
10771089
listeners,
10781090
setNodeRef,
1079-
transform: props.contextDrag ? null : transform,
1080-
transition: props.contextDrag && isDragging ? "transform 150ms ease-out" : transition,
1081-
isDragging: isDragging && !props.contextDrag,
1091+
transform: contextDrag ? null : transform,
1092+
transition: contextDrag && isDragging ? "transform 150ms ease-out" : transition,
1093+
isDragging: isDragging && !contextDrag,
10821094
};
1083-
return props.children(bag);
1095+
// Keyed by variant so a card/slim switch remounts the row.
1096+
return <SidebarThreadRow key={rowProps.variant} {...rowProps} sortable={bag} />;
10841097
}
10851098

10861099
const draftSurfaceClassName = "bg-warning/4 hover:bg-warning/8";
@@ -1449,7 +1462,7 @@ const dropVerbBadge: Record<SidebarDropVerb, ReactNode> = {
14491462
),
14501463
};
14511464

1452-
const SidebarThreadRow = memo(function SidebarThreadRow(props: {
1465+
type SidebarThreadRowProps = {
14531466
thread: SidebarThreadSummary;
14541467
variant: "card" | "slim";
14551468
variantAction: SidebarSweepAction;
@@ -1495,7 +1508,9 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
14951508
onUnpin: (threadRef: ScopedThreadRef) => void;
14961509
onAcknowledgeWoke: (threadRef: ScopedThreadRef, visitedAt: string) => void;
14971510
onFileDropThreads?: ((threadRef: ScopedThreadRef, files: File[]) => void) | undefined;
1498-
}) {
1511+
};
1512+
1513+
const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowProps) {
14991514
const {
15001515
isRenaming,
15011516
onCancelRename,
@@ -2071,9 +2086,13 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
20712086
</Tooltip>
20722087
) : (
20732088
<span className="text-xs">
2074-
{variantAction === "unsettle"
2075-
? settledTimeLabel(thread)
2076-
: threadTimeLabel(thread)}
2089+
<RelativeTimeLabel
2090+
timestamp={
2091+
variantAction === "unsettle"
2092+
? resolveSettledThreadTimestamp(thread)
2093+
: threadTimeTimestamp(thread)
2094+
}
2095+
/>
20772096
</span>
20782097
)}
20792098
</span>
@@ -2263,7 +2282,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
22632282
</span>
22642283
)
22652284
) : (
2266-
threadTimeLabel(thread)
2285+
<RelativeTimeLabel timestamp={threadTimeTimestamp(thread)} />
22672286
)}
22682287
</span>
22692288
{props.settlementSupported || showSnoozeButton || hasUnsentDraft ? (
@@ -2547,7 +2566,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
25472566
/>
25482567
</span>
25492568
<span className="w-8 shrink-0 text-right text-xs text-muted-foreground/55 tabular-nums">
2550-
{threadTimeLabel(thread)}
2569+
<RelativeTimeLabel timestamp={threadTimeTimestamp(thread)} />
25512570
</span>
25522571
</span>
25532572
{props.searchMatch ? (
@@ -4681,10 +4700,9 @@ export default function Sidebar() {
46814700
)}
46824701
>
46834702
{(() => {
4684-
const renderThreadRowInner = (
4703+
const renderThreadRow = (
46854704
thread: EnvironmentThreadShell,
46864705
section: SidebarSection,
4687-
sortable?: SortableThreadRowBag,
46884706
) => {
46894707
const threadKey = scopedThreadKey(
46904708
scopeThreadRef(thread.environmentId, thread.id),
@@ -4693,8 +4711,16 @@ export default function Sidebar() {
46934711
section === "active" || section === "pinned" || section === "working";
46944712
const rowVariant = isCard ? "card" : "slim";
46954713
return (
4696-
<SidebarThreadRow
4697-
key={`${threadKey}:${rowVariant}`}
4714+
<SortableThreadRow
4715+
key={threadKey}
4716+
sortableId={threadKey}
4717+
contextDrag={isContextDrag}
4718+
sortableDisabled={
4719+
renamingThreadKey === threadKey ||
4720+
section === "working" ||
4721+
!draggableThreadKeys.has(threadKey) ||
4722+
optimisticDrop !== null
4723+
}
46984724
thread={thread}
46994725
variant={rowVariant}
47004726
variantAction={
@@ -4717,7 +4743,6 @@ export default function Sidebar() {
47174743
.threadPinning === true
47184744
}
47194745
isPinned={thread.pinnedAt != null}
4720-
sortable={sortable}
47214746
dropVerb={
47224747
dragState?.activeKey === threadKey
47234748
? resolveSidebarDropVerb(dragState.activeSection, dragTargetSection)
@@ -4784,30 +4809,6 @@ export default function Sidebar() {
47844809
/>
47854810
);
47864811
};
4787-
const renderThreadRow = (
4788-
thread: EnvironmentThreadShell,
4789-
section: SidebarSection,
4790-
) => {
4791-
const threadKey = scopedThreadKey(
4792-
scopeThreadRef(thread.environmentId, thread.id),
4793-
);
4794-
return (
4795-
<SortableThreadRow
4796-
key={threadKey}
4797-
id={threadKey}
4798-
contextDrag={isContextDrag}
4799-
environmentId={thread.environmentId}
4800-
disabled={
4801-
renamingThreadKey === threadKey ||
4802-
section === "working" ||
4803-
!draggableThreadKeys.has(threadKey) ||
4804-
optimisticDrop !== null
4805-
}
4806-
>
4807-
{(bag) => renderThreadRowInner(thread, section, bag)}
4808-
</SortableThreadRow>
4809-
);
4810-
};
48114812
const from = isContextDrag ? null : (dragState?.activeSection ?? null);
48124813
const items: ReactNode[] = [
48134814
<SidebarDraftBlock

‎apps/web/src/timestampFormat.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,13 @@ export type RelativeTimeState =
203203
| { status: "invalid" }
204204
| { status: "relative"; value: string; suffix: string | null };
205205

206-
export function formatRelativeTime(isoDate: string): RelativeTimeParts | null {
206+
export function formatRelativeTime(
207+
isoDate: string,
208+
nowMs: number = Date.now(),
209+
): RelativeTimeParts | null {
207210
const date = parseTimestampDate(isoDate);
208211
if (!date) return null;
209-
const diffMs = Date.now() - date.getTime();
212+
const diffMs = nowMs - date.getTime();
210213
if (diffMs < 0) return { value: "just now", suffix: null };
211214
const seconds = Math.floor(diffMs / 1000);
212215
if (seconds < 60) return { value: "just now", suffix: null };
@@ -218,8 +221,8 @@ export function formatRelativeTime(isoDate: string): RelativeTimeParts | null {
218221
return { value: `${days}d`, suffix: "ago" };
219222
}
220223

221-
export function formatRelativeTimeLabel(isoDate: string) {
222-
const relative = formatRelativeTime(isoDate);
224+
export function formatRelativeTimeLabel(isoDate: string, nowMs: number = Date.now()) {
225+
const relative = formatRelativeTime(isoDate, nowMs);
223226
if (!relative) return "";
224227
return relative.suffix ? `${relative.value} ${relative.suffix}` : relative.value;
225228
}

0 commit comments

Comments
 (0)