The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 05:30 UTC
perf(web): right panel tabs no longer all re-render on unrelated chat updates (#20853)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 17, 2026 at 03:39 UTC
f96aebeb4bbdaf2b3d05177bd115dad4b8b41a8b
1 file changed
+222
-153
apps/web/src/components/RightPanelTabs.tsx
+222
-153
@@ -38,7 +38,6 @@ import {
38
} from "lucide-react";
39
import { Volume2, VolumeOff } from "lucide";
40
import {
41
- type ComponentProps,
41
type KeyboardEvent as ReactKeyboardEvent,
42
type MouseEvent as ReactMouseEvent,
43
type ReactElement,
@@ -55,6 +54,7 @@ import type { RightPanelSurface } from "~/rightPanelStore";
54
import { cn } from "~/lib/utils";
55
import { resolveShortcutCommand, type ShortcutMatchContext } from "~/keybindings";
56
import { readLocalApi } from "~/localApi";
57
+import { useStableCallback } from "~/hooks/useStableCallback";
58
import { Button } from "~/components/ui/button";
59
import { MorphIcon } from "~/components/MorphIcon";
60
import { AndroidIcon, AppleIcon } from "~/components/Icons";
@@ -598,30 +598,37 @@ function sameOrigin(left: string, right: string): boolean {
598
}
599
}
600
601
+function previewFaviconOf(
602
+ surface: RightPanelSurface,
603
+ sessions: Readonly<Record<string, PreviewSessionSnapshot>>,
604
+ desktopByTabId: Readonly<Record<string, DesktopPreviewOverlay>>,
605
+): { url: string | null; capturedUrl: string | null } {
606
+ const snapshot =
607
+ surface.kind === "preview" && surface.resourceId ? sessions[surface.resourceId] : null;
608
+ const url = !snapshot || snapshot.navStatus._tag === "Idle" ? null : snapshot.navStatus.url;
609
+ const favicon = snapshot ? (desktopByTabId[snapshot.tabId]?.favicon ?? null) : null;
610
+ const capturedUrl = favicon && url && sameOrigin(favicon.pageUrl, url) ? favicon.dataUrl : null;
611
+ return { url, capturedUrl };
612
+}
613
+
614
function SurfaceIcon({
615
surface,
603
- sessions,
604
- desktopByTabId,
616
+ previewUrl,
617
+ previewCapturedUrl,
618
theme,
619
environmentId,
607
- pullRequestStatusSeeds,
620
+ pullRequestStatusSeed,
621
}: {
622
surface: RightPanelSurface;
610
- sessions: Readonly<Record<string, PreviewSessionSnapshot>>;
611
- desktopByTabId: Readonly<Record<string, DesktopPreviewOverlay>>;
623
+ previewUrl: string | null;
624
+ previewCapturedUrl: string | null;
625
theme: "light" | "dark";
626
environmentId: EnvironmentId | null;
614
- pullRequestStatusSeeds: Readonly<Record<string, PullRequestTabStatusSeed>> | undefined;
627
+ pullRequestStatusSeed: PullRequestTabStatusSeed | undefined;
628
}) {
629
switch (surface.kind) {
617
- case "preview": {
618
- const snapshot = surface.resourceId ? sessions[surface.resourceId] : null;
619
- const url = !snapshot || snapshot.navStatus._tag === "Idle" ? null : snapshot.navStatus.url;
620
- const favicon = snapshot ? (desktopByTabId[snapshot.tabId]?.favicon ?? null) : null;
621
- const capturedUrl =
622
- favicon && url && sameOrigin(favicon.pageUrl, url) ? favicon.dataUrl : null;
623
- return <PreviewFavicon capturedUrl={capturedUrl} url={url} />;
624
- }
630
+ case "preview":
631
+ return <PreviewFavicon capturedUrl={previewCapturedUrl} url={previewUrl} />;
632
case "diff":
633
return <FileDiff className="size-3 shrink-0" />;
634
case "files":
@@ -642,7 +649,7 @@ function SurfaceIcon({
649
<PullRequestSurfaceIcon
650
surface={surface}
651
environmentId={environmentId}
645
- seed={pullRequestStatusSeeds?.[surface.id]}
652
+ seed={pullRequestStatusSeed}
653
/>
654
);
655
case "pull-requests":
@@ -783,24 +790,168 @@ function SortableTabList(props: {
790
);
791
}
792
786
-function SortableTab({
787
- surfaceId,
788
- disabled,
789
- className,
790
- ...props
791
-}: ComponentProps<"div"> & { surfaceId: string; disabled: boolean }) {
793
+interface RightPanelTabHandlers {
794
+ onActivate: (surface: RightPanelSurface) => void;
795
+ onClose: (surface: RightPanelSurface) => void;
796
+ onAuxClick: (event: ReactMouseEvent, surface: RightPanelSurface) => void;
797
+ onContextMenu: (event: ReactMouseEvent, surface: RightPanelSurface) => void;
798
+ onStartRename: (surfaceId: string) => void;
799
+ onFinishRename: (surfaceId: string, title: string) => void;
800
+}
801
+
802
+function handleTabMouseDown(event: ReactMouseEvent) {
803
+ if (event.button !== 1) return;
804
+ event.preventDefault();
805
+}
806
+
807
+// One tab owns its sortable state and receives only its own values, so the
808
+// compiled subtree is reused unless this tab's surface, title or state changed.
809
+// The handlers must be stable: the tab list re-renders on every parent render.
810
+function RightPanelTab(
811
+ props: RightPanelTabHandlers & {
812
+ surface: RightPanelSurface;
813
+ dragDisabled: boolean;
814
+ active: boolean;
815
+ pending: boolean;
816
+ title: string;
817
+ audio: TabAudioState;
818
+ audioRuntimeTabId: string | null;
819
+ previewUrl: string | null;
820
+ previewCapturedUrl: string | null;
821
+ theme: "light" | "dark";
822
+ environmentId: EnvironmentId | null;
823
+ pullRequestStatusSeed: PullRequestTabStatusSeed | undefined;
824
+ renaming: boolean;
825
+ canRename: boolean;
826
+ ownsDesktopTitleBar: boolean;
827
+ },
828
+) {
829
+ const {
830
+ surface,
831
+ title,
832
+ audio,
833
+ audioRuntimeTabId,
834
+ onActivate,
835
+ onClose,
836
+ onAuxClick,
837
+ onContextMenu,
838
+ onStartRename,
839
+ onFinishRename,
840
+ } = props;
841
const { listeners, setNodeRef, transform, transition, isDragging } = useSortable({
793
- id: surfaceId,
794
- disabled,
842
+ id: surface.id,
843
+ disabled: props.dragDisabled,
844
});
845
+ const content = (
846
+ <>
847
+ <PanelTabCloseButton label={`Close ${title}`} onClick={() => onClose(surface)}>
848
+ <SurfaceIcon
849
+ surface={surface}
850
+ previewUrl={props.previewUrl}
851
+ previewCapturedUrl={props.previewCapturedUrl}
852
+ theme={props.theme}
853
+ environmentId={props.environmentId}
854
+ pullRequestStatusSeed={props.pullRequestStatusSeed}
855
+ />
856
+ {props.pending ? (
857
+ <span
858
+ className="absolute -right-0.5 -bottom-0.5 size-1.5 rounded-full bg-current"
859
+ aria-hidden
860
+ />
861
+ ) : null}
862
+ </PanelTabCloseButton>
863
+ {audio === "none" || !audioRuntimeTabId ? null : (
864
+ <Tooltip>
865
+ <TooltipTrigger
866
+ render={
867
+ <button
868
+ type="button"
869
+ className="cursor-pointer flex size-4 shrink-0 items-center justify-center rounded-sm hover:bg-muted"
870
+ aria-label={audio === "muted" ? `Unmute ${title}` : `Mute ${title}`}
871
+ onClick={(event) => {
872
+ event.stopPropagation();
873
+ void previewBridge
874
+ ?.setAudioMuted(audioRuntimeTabId, audio !== "muted")
875
+ .catch(() => undefined);
876
+ }}
877
+ >
878
+ <MorphIcon className="size-3" icon={audio === "muted" ? VolumeOff : Volume2} />
879
+ </button>
880
+ }
881
+ />
882
+ <TooltipPopup>{audio === "muted" ? "Unmute tab" : "Mute tab"}</TooltipPopup>
883
+ </Tooltip>
884
+ )}
885
+ {props.renaming ? (
886
+ <input
887
+ aria-label="Device tab name"
888
+ className="w-24 min-w-0 rounded-sm bg-background px-1 outline-none ring-1 ring-inset ring-ring"
889
+ defaultValue={title}
890
+ ref={(element) => {
891
+ element?.focus();
892
+ element?.select();
893
+ }}
894
+ onBlur={(event) => onFinishRename(surface.id, event.currentTarget.value)}
895
+ onKeyDown={(event) => {
896
+ event.stopPropagation();
897
+ if (event.key === "Enter") event.currentTarget.blur();
898
+ if (event.key === "Escape") {
899
+ event.currentTarget.value = title;
900
+ event.currentTarget.blur();
901
+ }
902
+ }}
903
+ />
904
+ ) : (
905
+ <Tooltip>
906
+ <TooltipTrigger
907
+ render={
908
+ <button
909
+ type="button"
910
+ onDoubleClick={() => {
911
+ if (surface.kind === "device" && props.canRename) onStartRename(surface.id);
912
+ }}
913
+ className="cursor-pointer flex min-w-0 items-center"
914
+ onClick={() => onActivate(surface)}
915
+ >
916
+ <span className="truncate">{title}</span>
917
+ </button>
918
+ }
919
+ />
920
+ <TooltipPopup>
921
+ {surface.kind === "device" ? (
922
+ <DeviceTabTooltip
923
+ surface={surface}
924
+ environmentId={props.environmentId}
925
+ title={title}
926
+ />
927
+ ) : (
928
+ title
929
+ )}
930
+ </TooltipPopup>
931
+ </Tooltip>
932
+ )}
933
+ </>
934
+ );
935
return (
936
<div
798
- {...props}
937
+ data-active-tab={props.active}
938
+ onMouseDown={handleTabMouseDown}
939
+ onAuxClick={(event) => onAuxClick(event, surface)}
940
+ onContextMenu={(event) => onContextMenu(event, surface)}
941
{...listeners}
942
ref={setNodeRef}
943
style={{ transform: CSS.Translate.toString(transform), transition }}
802
- className={cn(className, isDragging && "relative z-10")}
803
- />
944
+ className={cn(
945
+ "cursor-pointer group/tab flex h-6 max-w-36 shrink-0 items-center gap-0.5 rounded-md pr-2 pl-1.5 text-xs",
946
+ props.ownsDesktopTitleBar && "[-webkit-app-region:no-drag]",
947
+ props.active
948
+ ? "bg-accent text-foreground"
949
+ : "text-muted-foreground hover:bg-accent/60 hover:text-foreground",
950
+ isDragging && "relative z-10",
951
+ )}
952
+ >
953
+ {content}
954
+ </div>
955
);
956
}
957
@@ -1024,16 +1175,27 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
1175
break;
1176
}
1177
};
1027
- const handleTabMouseDown = (event: ReactMouseEvent) => {
1028
- if (event.button !== 1) return;
1029
- event.preventDefault();
1030
- };
1178
const handleTabAuxClick = (event: ReactMouseEvent, surface: RightPanelSurface) => {
1179
if (event.button !== 1) return;
1180
event.preventDefault();
1181
event.stopPropagation();
1182
props.onCloseSurface(surface);
1183
};
1184
+ // Every tab receives these. Parents pass fresh closures on each render, so
1185
+ // stable forwarders keep a tab's props equal unless its own surface changed.
1186
+ const onActivateTab = useStableCallback(props.onActivate);
1187
+ const onCloseTab = useStableCallback(props.onCloseSurface);
1188
+ const onTabAuxClick = useStableCallback(handleTabAuxClick);
1189
+ const onTabContextMenu = useStableCallback(
1190
+ (event: ReactMouseEvent, surface: RightPanelSurface) => {
1191
+ void handleTabContextMenu(event, surface);
1192
+ },
1193
+ );
1194
+ const onStartRenameTab = useStableCallback((surfaceId: string) => setRenamingDevice(surfaceId));
1195
+ const onFinishRenameTab = useStableCallback((surfaceId: string, title: string) => {
1196
+ props.onRenameDevice?.(surfaceId, title);
1197
+ setRenamingDevice(null);
1198
+ });
1199
1200
useEffect(() => {
1201
if (!props.activeSurfaceId || !tabScrollState.hasOverflow) return;
@@ -1111,134 +1273,41 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
1273
<div className="flex h-full w-max min-w-full items-center gap-1">
1274
<SortableTabList surfaces={props.surfaces} onMove={props.onMoveSurface}>
1275
{props.surfaces.map((surface) => {
1114
- const active = surface.id === props.activeSurfaceId;
1115
- const pending = props.pendingSurfaceIds.has(surface.id);
1116
- const title = surfaceTitle(
1276
+ const previewTabId = previewTabIdOf(surface, props.previewSessions);
1277
+ const favicon = previewFaviconOf(
1278
surface,
1279
props.previewSessions,
1119
- props.terminalLabelsById,
1120
- );
1121
- const previewTabId = previewTabIdOf(surface, props.previewSessions);
1122
- const audio = tabAudioState(
1123
- previewTabId ? (props.desktopByTabId[previewTabId] ?? null) : null,
1280
+ props.desktopByTabId,
1281
);
1125
- const audioRuntimeTabId = previewTabId
1126
- ? (props.previewRuntimeTabId?.(previewTabId) ?? null)
1127
- : null;
1282
return (
1129
- <SortableTab
1283
+ <RightPanelTab
1284
key={surface.id}
1131
- surfaceId={surface.id}
1132
- disabled={!props.onMoveSurface || renamingDevice === surface.id}
1133
- data-active-tab={active}
1134
- onMouseDown={handleTabMouseDown}
1135
- onAuxClick={(event) => handleTabAuxClick(event, surface)}
1136
- onContextMenu={(event) => void handleTabContextMenu(event, surface)}
1137
- className={cn(
1138
- "cursor-pointer group/tab flex h-6 max-w-36 shrink-0 items-center gap-0.5 rounded-md pr-2 pl-1.5 text-xs",
1139
- ownsDesktopTitleBar && "[-webkit-app-region:no-drag]",
1140
- active
1141
- ? "bg-accent text-foreground"
1142
- : "text-muted-foreground hover:bg-accent/60 hover:text-foreground",
1285
+ surface={surface}
1286
+ dragDisabled={!props.onMoveSurface || renamingDevice === surface.id}
1287
+ active={surface.id === props.activeSurfaceId}
1288
+ pending={props.pendingSurfaceIds.has(surface.id)}
1289
+ title={surfaceTitle(surface, props.previewSessions, props.terminalLabelsById)}
1290
+ audio={tabAudioState(
1291
+ previewTabId ? (props.desktopByTabId[previewTabId] ?? null) : null,
1292
)}
1144
- >
1145
- <PanelTabCloseButton
1146
- label={`Close ${title}`}
1147
- onClick={() => props.onCloseSurface(surface)}
1148
- >
1149
- <SurfaceIcon
1150
- surface={surface}
1151
- sessions={props.previewSessions}
1152
- desktopByTabId={props.desktopByTabId}
1153
- theme={resolvedTheme}
1154
- environmentId={props.environmentId}
1155
- pullRequestStatusSeeds={props.pullRequestStatusSeeds}
1156
- />
1157
- {pending ? (
1158
- <span
1159
- className="absolute -right-0.5 -bottom-0.5 size-1.5 rounded-full bg-current"
1160
- aria-hidden
1161
- />
1162
- ) : null}
1163
- </PanelTabCloseButton>
1164
- {audio === "none" || !audioRuntimeTabId ? null : (
1165
- <Tooltip>
1166
- <TooltipTrigger
1167
- render={
1168
- <button
1169
- type="button"
1170
- className="cursor-pointer flex size-4 shrink-0 items-center justify-center rounded-sm hover:bg-muted"
1171
- aria-label={audio === "muted" ? `Unmute ${title}` : `Mute ${title}`}
1172
- onClick={(event) => {
1173
- event.stopPropagation();
1174
- void previewBridge
1175
- ?.setAudioMuted(audioRuntimeTabId, audio !== "muted")
1176
- .catch(() => undefined);
1177
- }}
1178
- >
1179
- <MorphIcon
1180
- className="size-3"
1181
- icon={audio === "muted" ? VolumeOff : Volume2}
1182
- />
1183
- </button>
1184
- }
1185
- />
1186
- <TooltipPopup>{audio === "muted" ? "Unmute tab" : "Mute tab"}</TooltipPopup>
1187
- </Tooltip>
1188
- )}
1189
- {renamingDevice === surface.id ? (
1190
- <input
1191
- aria-label="Device tab name"
1192
- className="w-24 min-w-0 rounded-sm bg-background px-1 outline-none ring-1 ring-inset ring-ring"
1193
- defaultValue={title}
1194
- ref={(element) => {
1195
- element?.focus();
1196
- element?.select();
1197
- }}
1198
- onBlur={(event) => {
1199
- props.onRenameDevice?.(surface.id, event.currentTarget.value);
1200
- setRenamingDevice(null);
1201
- }}
1202
- onKeyDown={(event) => {
1203
- event.stopPropagation();
1204
- if (event.key === "Enter") event.currentTarget.blur();
1205
- if (event.key === "Escape") {
1206
- event.currentTarget.value = title;
1207
- event.currentTarget.blur();
1208
- }
1209
- }}
1210
- />
1211
- ) : (
1212
- <Tooltip>
1213
- <TooltipTrigger
1214
- render={
1215
- <button
1216
- type="button"
1217
- onDoubleClick={() => {
1218
- if (surface.kind === "device" && props.onRenameDevice)
1219
- setRenamingDevice(surface.id);
1220
- }}
1221
- className="cursor-pointer flex min-w-0 items-center"
1222
- onClick={() => props.onActivate(surface)}
1223
- >
1224
- <span className="truncate">{title}</span>
1225
- </button>
1226
- }
1227
- />
1228
- <TooltipPopup>
1229
- {surface.kind === "device" ? (
1230
- <DeviceTabTooltip
1231
- surface={surface}
1232
- environmentId={props.environmentId}
1233
- title={title}
1234
- />
1235
- ) : (
1236
- title
1237
- )}
1238
- </TooltipPopup>
1239
- </Tooltip>
1240
- )}
1241
- </SortableTab>
1293
+ audioRuntimeTabId={
1294
+ previewTabId ? (props.previewRuntimeTabId?.(previewTabId) ?? null) : null
1295
+ }
1296
+ previewUrl={favicon.url}
1297
+ previewCapturedUrl={favicon.capturedUrl}
1298
+ theme={resolvedTheme}
1299
+ environmentId={props.environmentId}
1300
+ pullRequestStatusSeed={props.pullRequestStatusSeeds?.[surface.id]}
1301
+ renaming={renamingDevice === surface.id}
1302
+ canRename={props.onRenameDevice !== undefined}
1303
+ ownsDesktopTitleBar={ownsDesktopTitleBar}
1304
+ onActivate={onActivateTab}
1305
+ onClose={onCloseTab}
1306
+ onAuxClick={onTabAuxClick}
1307
+ onContextMenu={onTabContextMenu}
1308
+ onStartRename={onStartRenameTab}
1309
+ onFinishRename={onFinishRenameTab}
1310
+ />
1311
);
1312
})}
1313
</SortableTabList>