Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Reduce timeline row rerenders (#2498)

cursor[bot]536dcad19e32
diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx
index f34c2a56e..4b15b8090 100644
--- a/apps/web/src/components/chat/MessagesTimeline.tsx
+++ b/apps/web/src/components/chat/MessagesTimeline.tsx
@@ -70,11 +70,6 @@ import { formatWorkspaceRelativePath } from "../../filePathDisplay";
// ---------------------------------------------------------------------------
interface TimelineRowSharedState {
- activeTurnInProgress: boolean;
- activeTurnId: TurnId | null | undefined;
- isWorking: boolean;
- isRevertingCheckpoint: boolean;
- completionSummary: string | null;
timestampFormat: TimestampFormat;
routeThreadKey: string;
markdownCwd: string | undefined;
@@ -86,7 +81,16 @@ interface TimelineRowSharedState {
onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void;
}
+interface TimelineRowActivityState {
+ activeTurnInProgress: boolean;
+ activeTurnId: TurnId | null;
+ isWorking: boolean;
+ isRevertingCheckpoint: boolean;
+ completionSummary: string | null;
+}
+
const TimelineRowCtx = createContext<TimelineRowSharedState>(null!);
+const TimelineRowActivityCtx = createContext<TimelineRowActivityState>(null!);
const TIMELINE_LIST_HEADER = <div className="h-3 sm:h-4" />;
const TIMELINE_LIST_FOOTER = <div className="h-3 sm:h-4" />;
@@ -191,15 +195,8 @@ export const MessagesTimeline = memo(function MessagesTimeline({
};
}, [listRef, onIsAtEndChange, rows.length]);
- // Memoised context value — only changes on state transitions, NOT on
- // every streaming chunk. Callbacks from ChatView are useCallback-stable.
const sharedState = useMemo<TimelineRowSharedState>(
() => ({
- activeTurnInProgress,
- activeTurnId: activeTurnId ?? null,
- isWorking,
- isRevertingCheckpoint,
- completionSummary,
timestampFormat,
routeThreadKey,
markdownCwd,
@@ -211,11 +208,6 @@ export const MessagesTimeline = memo(function MessagesTimeline({
onOpenTurnDiff,
}),
[
- activeTurnInProgress,
- activeTurnId,
- isWorking,
- isRevertingCheckpoint,
- completionSummary,
timestampFormat,
routeThreadKey,
markdownCwd,
@@ -227,6 +219,16 @@ export const MessagesTimeline = memo(function MessagesTimeline({
onOpenTurnDiff,
],
);
+ const activityState = useMemo<TimelineRowActivityState>(
+ () => ({
+ activeTurnInProgress,
+ activeTurnId: activeTurnId ?? null,
+ isWorking,
+ isRevertingCheckpoint,
+ completionSummary,
+ }),
+ [activeTurnInProgress, activeTurnId, completionSummary, isRevertingCheckpoint, isWorking],
+ );
// Stable renderItem — no closure deps. Row components read shared state
// from TimelineRowCtx, which propagates through LegendList's memo.
@@ -251,21 +253,23 @@ export const MessagesTimeline = memo(function MessagesTimeline({
return (
<TimelineRowCtx.Provider value={sharedState}>
- <LegendList<MessagesTimelineRow>
- ref={listRef}
- data={rows}
- keyExtractor={keyExtractor}
- renderItem={renderItem}
- estimatedItemSize={90}
- initialScrollAtEnd
- maintainScrollAtEnd
- maintainScrollAtEndThreshold={0.1}
- maintainVisibleContentPosition
- onScroll={handleScroll}
- className="h-full overflow-x-hidden overscroll-y-contain px-3 sm:px-5"
- ListHeaderComponent={TIMELINE_LIST_HEADER}
- ListFooterComponent={TIMELINE_LIST_FOOTER}
- />
+ <TimelineRowActivityCtx.Provider value={activityState}>
+ <LegendList<MessagesTimelineRow>
+ ref={listRef}
+ data={rows}
+ keyExtractor={keyExtractor}
+ renderItem={renderItem}
+ estimatedItemSize={90}
+ initialScrollAtEnd
+ maintainScrollAtEnd
+ maintainScrollAtEndThreshold={0.1}
+ maintainVisibleContentPosition
+ onScroll={handleScroll}
+ className="h-full overflow-x-hidden overscroll-y-contain px-3 sm:px-5"
+ ListHeaderComponent={TIMELINE_LIST_HEADER}
+ ListFooterComponent={TIMELINE_LIST_FOOTER}
+ />
+ </TimelineRowActivityCtx.Provider>
</TimelineRowCtx.Provider>
);
});
@@ -283,9 +287,7 @@ type TimelineMessage = Extract<TimelineEntry, { kind: "message" }>["message"];
type TimelineWorkEntry = Extract<MessagesTimelineRow, { kind: "work" }>["groupedEntries"][number];
type TimelineRow = MessagesTimelineRow;
-function TimelineRowContent({ row }: { row: TimelineRow }) {
- const ctx = use(TimelineRowCtx);
-
+const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: TimelineRow }) {
return (
<div
className={cn(
@@ -297,187 +299,222 @@ function TimelineRowContent({ row }: { row: TimelineRow }) {
data-message-id={row.kind === "message" ? row.message.id : undefined}
data-message-role={row.kind === "message" ? row.message.role : undefined}
>
- {row.kind === "work" && <WorkGroupSection groupedEntries={row.groupedEntries} />}
-
- {row.kind === "message" &&
- row.message.role === "user" &&
- (() => {
- const userImages = row.message.attachments ?? [];
- const displayedUserMessage = deriveDisplayedUserMessageState(row.message.text);
- const terminalContexts = displayedUserMessage.contexts;
- const canRevertAgentWork = typeof row.revertTurnCount === "number";
- return (
- <div className="flex justify-end">
- <div className="group relative max-w-[80%] rounded-2xl rounded-br-sm border border-border bg-secondary px-4 py-3">
- {userImages.length > 0 && (
- <div className="mb-2 grid max-w-[420px] grid-cols-2 gap-2">
- {userImages.map(
- (image: NonNullable<TimelineMessage["attachments"]>[number]) => (
- <div
- key={image.id}
- className="overflow-hidden rounded-lg border border-border/80 bg-background/70"
- >
- {image.previewUrl ? (
- <button
- type="button"
- className="h-full w-full cursor-zoom-in"
- aria-label={`Preview ${image.name}`}
- onClick={() => {
- const preview = buildExpandedImagePreview(userImages, image.id);
- if (!preview) return;
- ctx.onImageExpand(preview);
- }}
- >
- <img
- src={image.previewUrl}
- alt={image.name}
- className="block h-auto max-h-[220px] w-full object-cover"
- />
- </button>
- ) : (
- <div className="flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-[11px] text-muted-foreground/70">
- {image.name}
- </div>
- )}
- </div>
- ),
- )}
+ {row.kind === "work" ? <WorkGroupSection groupedEntries={row.groupedEntries} /> : null}
+ {row.kind === "message" && row.message.role === "user" ? <UserTimelineRow row={row} /> : null}
+ {row.kind === "message" && row.message.role === "assistant" ? (
+ <AssistantTimelineRow row={row} />
+ ) : null}
+ {row.kind === "proposed-plan" ? <ProposedPlanTimelineRow row={row} /> : null}
+ {row.kind === "working" ? <WorkingTimelineRow row={row} /> : null}
+ </div>
+ );
+});
+
+function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
+ const ctx = use(TimelineRowCtx);
+ const userImages = row.message.attachments ?? [];
+ const displayedUserMessage = deriveDisplayedUserMessageState(row.message.text);
+ const terminalContexts = displayedUserMessage.contexts;
+ const canRevertAgentWork = typeof row.revertTurnCount === "number";
+
+ return (
+ <div className="flex justify-end">
+ <div className="group relative max-w-[80%] rounded-2xl rounded-br-sm border border-border bg-secondary px-4 py-3">
+ {userImages.length > 0 && (
+ <div className="mb-2 grid max-w-[420px] grid-cols-2 gap-2">
+ {userImages.map((image: NonNullable<TimelineMessage["attachments"]>[number]) => (
+ <div
+ key={image.id}
+ className="overflow-hidden rounded-lg border border-border/80 bg-background/70"
+ >
+ {image.previewUrl ? (
+ <button
+ type="button"
+ className="h-full w-full cursor-zoom-in"
+ aria-label={`Preview ${image.name}`}
+ onClick={() => {
+ const preview = buildExpandedImagePreview(userImages, image.id);
+ if (!preview) return;
+ ctx.onImageExpand(preview);
+ }}
+ >
+ <img
+ src={image.previewUrl}
+ alt={image.name}
+ className="block h-auto max-h-[220px] w-full object-cover"
+ />
+ </button>
+ ) : (
+ <div className="flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-[11px] text-muted-foreground/70">
+ {image.name}
</div>
)}
- {(displayedUserMessage.visibleText.trim().length > 0 ||
- terminalContexts.length > 0) && (
- <UserMessageBody
- text={displayedUserMessage.visibleText}
- terminalContexts={terminalContexts}
- />
- )}
- <div className="mt-1.5 flex items-center justify-end gap-2">
- <div className="flex items-center gap-1.5 opacity-0 transition-opacity duration-200 focus-within:opacity-100 group-hover:opacity-100">
- {displayedUserMessage.copyText && (
- <MessageCopyButton text={displayedUserMessage.copyText} />
- )}
- {canRevertAgentWork && (
- <Button
- type="button"
- size="xs"
- variant="outline"
- disabled={ctx.isRevertingCheckpoint || ctx.isWorking}
- onClick={() => ctx.onRevertUserMessage(row.message.id)}
- title="Revert to this message"
- >
- <Undo2Icon className="size-3" />
- </Button>
- )}
- </div>
- <p className="text-right text-xs text-muted-foreground/50">
- {formatTimestamp(row.message.createdAt, ctx.timestampFormat)}
- </p>
- </div>
</div>
- </div>
- );
- })()}
-
- {row.kind === "message" &&
- row.message.role === "assistant" &&
- (() => {
- const messageText = row.message.text || (row.message.streaming ? "" : "(empty response)");
- const assistantTurnStillInProgress =
- ctx.activeTurnInProgress &&
- ctx.activeTurnId !== null &&
- ctx.activeTurnId !== undefined &&
- row.message.turnId === ctx.activeTurnId;
- const assistantCopyState = resolveAssistantMessageCopyState({
- text: row.message.text ?? null,
- showCopyButton: row.showAssistantCopyButton,
- streaming: row.message.streaming || assistantTurnStillInProgress,
- });
- return (
- <>
- {row.showCompletionDivider && (
- <div className="my-3 flex items-center gap-3">
- <span className="h-px flex-1 bg-border" />
- <span className="rounded-full border border-border bg-background px-2.5 py-1 text-[10px] uppercase tracking-[0.14em] text-muted-foreground/80">
- {ctx.completionSummary ? `Response • ${ctx.completionSummary}` : "Response"}
- </span>
- <span className="h-px flex-1 bg-border" />
- </div>
- )}
- <div className="min-w-0 px-1 py-0.5">
- <ChatMarkdown
- text={messageText}
- cwd={ctx.markdownCwd}
- isStreaming={Boolean(row.message.streaming)}
- />
- <AssistantChangedFilesSection
- turnSummary={row.assistantTurnDiffSummary}
- routeThreadKey={ctx.routeThreadKey}
- resolvedTheme={ctx.resolvedTheme}
- onOpenTurnDiff={ctx.onOpenTurnDiff}
- />
- <div className="mt-1.5 flex items-center gap-2">
- <p className="text-[10px] text-muted-foreground/30">
- {row.message.streaming ? (
- <LiveMessageMeta
- createdAt={row.message.createdAt}
- durationStart={row.durationStart}
- timestampFormat={ctx.timestampFormat}
- />
- ) : (
- formatMessageMeta(
- row.message.createdAt,
- formatElapsed(row.durationStart, row.message.completedAt),
- ctx.timestampFormat,
- )
- )}
- </p>
- {assistantCopyState.visible ? (
- <div className="flex items-center opacity-0 transition-opacity duration-200 group-hover/assistant:opacity-100">
- <MessageCopyButton
- text={assistantCopyState.text ?? ""}
- size="icon-xs"
- variant="outline"
- className="border-border/50 bg-background/35 text-muted-foreground/45 shadow-none hover:border-border/70 hover:bg-background/55 hover:text-muted-foreground/70"
- />
- </div>
- ) : null}
- </div>
- </div>
- </>
- );
- })()}
-
- {row.kind === "proposed-plan" && (
- <div className="min-w-0 px-1 py-0.5">
- <ProposedPlanCard
- planMarkdown={row.proposedPlan.planMarkdown}
- environmentId={ctx.activeThreadEnvironmentId}
- cwd={ctx.markdownCwd}
- workspaceRoot={ctx.workspaceRoot}
+ ))}
+ </div>
+ )}
+ {(displayedUserMessage.visibleText.trim().length > 0 || terminalContexts.length > 0) && (
+ <UserMessageBody
+ text={displayedUserMessage.visibleText}
+ terminalContexts={terminalContexts}
/>
+ )}
+ <div className="mt-1.5 flex items-center justify-end gap-2">
+ <div className="flex items-center gap-1.5 opacity-0 transition-opacity duration-200 focus-within:opacity-100 group-hover:opacity-100">
+ {displayedUserMessage.copyText && (
+ <MessageCopyButton text={displayedUserMessage.copyText} />
+ )}
+ {canRevertAgentWork && <RevertUserMessageButton messageId={row.message.id} />}
+ </div>
+ <p className="text-right text-xs text-muted-foreground/50">
+ {formatTimestamp(row.message.createdAt, ctx.timestampFormat)}
+ </p>
</div>
- )}
+ </div>
+ </div>
+ );
+}
- {row.kind === "working" && (
- <div className="py-0.5 pl-1.5">
- <div className="flex items-center gap-2 pt-1 text-[11px] text-muted-foreground/70">
- <span className="inline-flex items-center gap-[3px]">
- <span className="h-1 w-1 rounded-full bg-muted-foreground/30 animate-pulse" />
- <span className="h-1 w-1 rounded-full bg-muted-foreground/30 animate-pulse [animation-delay:200ms]" />
- <span className="h-1 w-1 rounded-full bg-muted-foreground/30 animate-pulse [animation-delay:400ms]" />
- </span>
- <span>
- {row.createdAt ? (
- <>
- Working for <WorkingTimer createdAt={row.createdAt} />
- </>
- ) : (
- "Working..."
- )}
- </span>
- </div>
+function RevertUserMessageButton({ messageId }: { messageId: MessageId }) {
+ const ctx = use(TimelineRowCtx);
+ const activity = use(TimelineRowActivityCtx);
+
+ return (
+ <Button
+ type="button"
+ size="xs"
+ variant="outline"
+ disabled={activity.isRevertingCheckpoint || activity.isWorking}
+ onClick={() => ctx.onRevertUserMessage(messageId)}
+ title="Revert to this message"
+ >
+ <Undo2Icon className="size-3" />
+ </Button>
+ );
+}
+
+function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
+ const ctx = use(TimelineRowCtx);
+ const messageText = row.message.text || (row.message.streaming ? "" : "(empty response)");
+
+ return (
+ <>
+ {row.showCompletionDivider && <AssistantCompletionDivider />}
+ <div className="min-w-0 px-1 py-0.5">
+ <ChatMarkdown
+ text={messageText}
+ cwd={ctx.markdownCwd}
+ isStreaming={Boolean(row.message.streaming)}
+ />
+ <AssistantChangedFilesSection
+ turnSummary={row.assistantTurnDiffSummary}
+ routeThreadKey={ctx.routeThreadKey}
+ resolvedTheme={ctx.resolvedTheme}
+ onOpenTurnDiff={ctx.onOpenTurnDiff}
+ />
+ <div className="mt-1.5 flex items-center gap-2">
+ <p className="text-[10px] text-muted-foreground/30">
+ {row.message.streaming ? (
+ <LiveMessageMeta
+ createdAt={row.message.createdAt}
+ durationStart={row.durationStart}
+ timestampFormat={ctx.timestampFormat}
+ />
+ ) : (
+ formatMessageMeta(
+ row.message.createdAt,
+ formatElapsed(row.durationStart, row.message.completedAt),
+ ctx.timestampFormat,
+ )
+ )}
+ </p>
+ <AssistantCopyButton row={row} />
</div>
- )}
+ </div>
+ </>
+ );
+}
+
+function AssistantCompletionDivider() {
+ const activity = use(TimelineRowActivityCtx);
+
+ return (
+ <div className="my-3 flex items-center gap-3">
+ <span className="h-px flex-1 bg-border" />
+ <span className="rounded-full border border-border bg-background px-2.5 py-1 text-[10px] uppercase tracking-[0.14em] text-muted-foreground/80">
+ {activity.completionSummary ? `Response • ${activity.completionSummary}` : "Response"}
+ </span>
+ <span className="h-px flex-1 bg-border" />
+ </div>
+ );
+}
+
+function AssistantCopyButton({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
+ const activity = use(TimelineRowActivityCtx);
+ const assistantTurnStillInProgress =
+ activity.activeTurnInProgress &&
+ activity.activeTurnId !== null &&
+ row.message.turnId === activity.activeTurnId;
+ const assistantCopyState = resolveAssistantMessageCopyState({
+ text: row.message.text ?? null,
+ showCopyButton: row.showAssistantCopyButton,
+ streaming: row.message.streaming || assistantTurnStillInProgress,
+ });
+
+ if (!assistantCopyState.visible) {
+ return null;
+ }
+
+ return (
+ <div className="flex items-center opacity-0 transition-opacity duration-200 group-hover/assistant:opacity-100">
+ <MessageCopyButton
+ text={assistantCopyState.text ?? ""}
+ size="icon-xs"
+ variant="outline"
+ className="border-border/50 bg-background/35 text-muted-foreground/45 shadow-none hover:border-border/70 hover:bg-background/55 hover:text-muted-foreground/70"
+ />
+ </div>
+ );
+}
+
+function ProposedPlanTimelineRow({
+ row,
+}: {
+ row: Extract<TimelineRow, { kind: "proposed-plan" }>;
+}) {
+ const ctx = use(TimelineRowCtx);
+
+ return (
+ <div className="min-w-0 px-1 py-0.5">
+ <ProposedPlanCard
+ planMarkdown={row.proposedPlan.planMarkdown}
+ environmentId={ctx.activeThreadEnvironmentId}
+ cwd={ctx.markdownCwd}
+ workspaceRoot={ctx.workspaceRoot}
+ />
+ </div>
+ );
+}
+
+function WorkingTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "working" }> }) {
+ return (
+ <div className="py-0.5 pl-1.5">
+ <div className="flex items-center gap-2 pt-1 text-[11px] text-muted-foreground/70">
+ <span className="inline-flex items-center gap-[3px]">
+ <span className="h-1 w-1 rounded-full bg-muted-foreground/30 animate-pulse" />
+ <span className="h-1 w-1 rounded-full bg-muted-foreground/30 animate-pulse [animation-delay:200ms]" />
+ <span className="h-1 w-1 rounded-full bg-muted-foreground/30 animate-pulse [animation-delay:400ms]" />
+ </span>
+ <span>
+ {row.createdAt ? (
+ <>
+ Working for <WorkingTimer createdAt={row.createdAt} />
+ </>
+ ) : (
+ "Working..."
+ )}
+ </span>
+ </div>
</div>
);
}