Skip to content

Commit 1d2e7d1

Browse files
Merge remote-tracking branch 'origin/main' into takeover/19928
# Conflicts: # apps/web/src/components/chat/TraitsPicker.tsx
2 parents 902aa4e + 33672c4 commit 1d2e7d1

182 files changed

Lines changed: 7757 additions & 8515 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,6 @@ export const REACT_COMPILED_FILES = [
9696
"apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx",
9797
"apps/web/src/components/ThreadTerminalDrawer.tsx",
9898
"apps/web/src/components/settings/SettingsPanels.tsx",
99+
"apps/web/src/routes/_chat.pull-requests.tsx",
100+
"apps/web/src/components/LegacySidebar.tsx",
99101
] as const;

‎apps/web/src/browser/BrowserSurfaceSlot.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ export function BrowserSurfaceSlot(props: {
8787
useLayoutEffect(() => {
8888
presentationRef.current = { visible, cornerRadius, zIndex };
8989
updateRef.current?.();
90-
}, [cornerRadius, layoutVersion, visible, zIndex]);
90+
}, [
91+
cornerRadius,
92+
// oxlint-disable-next-line react/exhaustive-effect-dependencies -- A layoutVersion bump means the slot moved without resizing, so it re-presents at the new position.
93+
layoutVersion,
94+
visible,
95+
zIndex,
96+
]);
9197

9298
return <div ref={elementRef} className={className} data-browser-surface-slot={tabId} />;
9399
}

‎apps/web/src/browser/ElectronBrowserHost.tsx‎

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
"use client";
22

33
import { parseScopedThreadKey } from "@t3tools/client-runtime/environment";
4-
import { AuthPreviewOperateScope, FILL_PREVIEW_VIEWPORT } from "@t3tools/contracts";
4+
import {
5+
AuthPreviewOperateScope,
6+
type DesktopPreviewOpenLinkEvent,
7+
FILL_PREVIEW_VIEWPORT,
8+
} from "@t3tools/contracts";
59
import { useAtomValue } from "@effect/atom-react";
6-
import { type ComponentProps, useEffect, useMemo, useRef, useState } from "react";
10+
import { type ComponentProps, useEffect, useEffectEvent, useState } from "react";
711

812
import { primaryEnvironmentIdAtom } from "~/state/primaryEnvironment";
913

1014
import { isElectron } from "~/env";
1115
import { useClientSettingsHydrated } from "~/hooks/useSettings";
12-
import { useTheme } from "~/hooks/useTheme";
1316
import { useActivePreviewSessions } from "~/previewStateStore";
1417
import { previewEnvironment } from "~/state/preview";
1518
import { useEnvironmentScope } from "~/state/session";
@@ -24,36 +27,27 @@ import { rendersServerTabNatively } from "./previewRuntime";
2427
import { previewRuntimeTabId } from "./previewRuntimeTabId";
2528

2629
export function ElectronBrowserHost() {
27-
const { resolvedTheme } = useTheme();
2830
const previewByThreadKey = useActivePreviewSessions();
2931
const primaryEnvironmentId = useAtomValue(primaryEnvironmentIdAtom);
30-
const sessions = useMemo(
31-
() =>
32-
Object.entries(previewByThreadKey).flatMap(([threadKey, previewState]) => {
33-
const threadRef = parseScopedThreadKey(threadKey);
34-
return threadRef
35-
? Object.values(previewState.sessions)
36-
.filter(
37-
(snapshot) =>
38-
snapshot.runtime !== "server" ||
39-
rendersServerTabNatively(threadRef.environmentId, primaryEnvironmentId, snapshot),
40-
)
41-
.map((snapshot) => ({
42-
threadRef,
43-
snapshot,
44-
runtimeTabId: previewRuntimeTabId(
45-
threadRef,
46-
previewState.serverEpoch,
47-
snapshot.tabId,
48-
),
49-
pictureInPicture:
50-
previewState.desktopByTabId[snapshot.tabId]?.pictureInPicture ?? false,
51-
zoomFactor: previewState.desktopByTabId[snapshot.tabId]?.zoomFactor ?? 1,
52-
}))
53-
: [];
54-
}),
55-
[previewByThreadKey, primaryEnvironmentId],
56-
);
32+
const sessions = Object.entries(previewByThreadKey).flatMap(([threadKey, previewState]) => {
33+
const threadRef = parseScopedThreadKey(threadKey);
34+
return threadRef
35+
? Object.values(previewState.sessions)
36+
.filter(
37+
(snapshot) =>
38+
snapshot.runtime !== "server" ||
39+
rendersServerTabNatively(threadRef.environmentId, primaryEnvironmentId, snapshot),
40+
)
41+
.map((snapshot) => ({
42+
threadRef,
43+
snapshot,
44+
runtimeTabId: previewRuntimeTabId(threadRef, previewState.serverEpoch, snapshot.tabId),
45+
pictureInPicture:
46+
previewState.desktopByTabId[snapshot.tabId]?.pictureInPicture ?? false,
47+
zoomFactor: previewState.desktopByTabId[snapshot.tabId]?.zoomFactor ?? 1,
48+
}))
49+
: [];
50+
});
5751

5852
useEffect(() => {
5953
const preview = window.desktopBridge?.preview;
@@ -86,7 +80,7 @@ export function ElectronBrowserHost() {
8680
observer.disconnect();
8781
headObserver.disconnect();
8882
};
89-
}, [resolvedTheme]);
83+
}, []);
9084

9185
useEffect(() => {
9286
const preview = window.desktopBridge?.preview;
@@ -97,27 +91,22 @@ export function ElectronBrowserHost() {
9791
}, []);
9892

9993
const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: true });
100-
const sessionByRuntimeTabId = useRef(new Map<string, (typeof sessions)[number]>());
101-
useEffect(() => {
102-
sessionByRuntimeTabId.current = new Map(
103-
sessions.map((session) => [session.runtimeTabId, session]),
104-
);
105-
}, [sessions]);
94+
const onOpenLink = useEffectEvent(({ tabId, url, background }: DesktopPreviewOpenLinkEvent) => {
95+
const source = sessions.find((session) => session.runtimeTabId === tabId);
96+
if (!source) return;
97+
void openUrlInPreview({
98+
threadRef: source.threadRef,
99+
url,
100+
openPreview,
101+
profileId: source.snapshot.profileId,
102+
background,
103+
});
104+
});
106105
useEffect(() => {
107106
const preview = window.desktopBridge?.preview;
108107
if (!preview) return;
109-
return preview.onOpenLink(({ tabId, url, background }) => {
110-
const source = sessionByRuntimeTabId.current.get(tabId);
111-
if (!source) return;
112-
void openUrlInPreview({
113-
threadRef: source.threadRef,
114-
url,
115-
openPreview,
116-
profileId: source.snapshot.profileId,
117-
background,
118-
});
119-
});
120-
}, [openPreview]);
108+
return preview.onOpenLink(onOpenLink);
109+
}, []);
121110

122111
if (!isElectron) return null;
123112
return (

‎apps/web/src/browser/HostedBrowserWebview.tsx‎

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ import type {
66
ScopedThreadRef,
77
} from "@t3tools/contracts";
88
import { useShallow } from "zustand/react/shallow";
9-
import { useCallback, useEffect, useRef, useState } from "react";
9+
import { useEffect, useEffectEvent, useRef, useState } from "react";
1010

1111
import { previewBridge } from "~/components/preview/previewBridge";
1212
import { usePreviewBridge } from "~/components/preview/usePreviewBridge";
1313
import { useClientSettingsHydrated } from "~/hooks/useSettings";
1414
import { cn, isMacPlatform } from "~/lib/utils";
1515

16-
import { resolveBrowserSurfacePanelRect, useBrowserSurfaceStore } from "./browserSurfaceStore";
16+
import {
17+
type BrowserSurfaceContentPresentation,
18+
type BrowserSurfaceRect,
19+
resolveBrowserSurfacePanelRect,
20+
useBrowserSurfaceStore,
21+
} from "./browserSurfaceStore";
1722
import { useActiveBrowserRecordingTabIds } from "./browserRecording";
1823
import {
1924
browserViewportSettingKey,
@@ -136,15 +141,14 @@ export function HostedBrowserWebview(props: {
136141

137142
const [webviewGeneration, setWebviewGeneration] = useState(0);
138143
const [recoverySrc, setRecoverySrc] = useState(initialSrc);
139-
const latestUrlRef = useRef(initialUrl);
140-
141-
useEffect(() => {
142-
latestUrlRef.current = initialUrl;
143-
}, [initialUrl]);
144+
const remountCrashedWebview = useEffectEvent(() => {
145+
setRecoverySrc(initialUrl ?? initialSrc);
146+
setWebviewGeneration((generation) => generation + 1);
147+
});
144148

145-
const setWebviewRef = useCallback((node: HTMLElement | null) => {
149+
const setWebviewRef = (node: HTMLElement | null) => {
146150
webviewRef.current = node as ElectronWebview | null;
147-
}, []);
151+
};
148152

149153
useEffect(() => {
150154
const webview = webviewRef.current;
@@ -176,10 +180,7 @@ export function HostedBrowserWebview(props: {
176180
crashRecoveryRef.current = recovery.state;
177181
recoveryTimeout = setTimeout(() => {
178182
recoveryTimeout = null;
179-
if (!disposed) {
180-
setRecoverySrc(latestUrlRef.current ?? initialSrc);
181-
setWebviewGeneration((generation) => generation + 1);
182-
}
183+
if (!disposed) remountCrashedWebview();
183184
}, recovery.delayMs);
184185
};
185186
// A click inside the guest only reaches this document as a webview focus
@@ -203,7 +204,13 @@ export function HostedBrowserWebview(props: {
203204
webview.removeEventListener("render-process-gone", recoverGuest);
204205
webview.removeEventListener("focus", dismissHostPopups);
205206
};
206-
}, [clientSettingsHydrated, config, initialSrc, runtimeTabId, webviewGeneration]);
207+
}, [
208+
clientSettingsHydrated,
209+
config,
210+
runtimeTabId,
211+
// oxlint-disable-next-line react/exhaustive-effect-dependencies -- A crash recovery remounts the webview under a new key, so the listeners attach to the new element.
212+
webviewGeneration,
213+
]);
207214

208215
const active = presentation.visible && presentation.rect !== null;
209216
const lastRect = presentation.rect;
@@ -214,25 +221,10 @@ export function HostedBrowserWebview(props: {
214221
viewportWidth === null || viewportHeight === null ? null : viewportWidth / viewportHeight;
215222
const lockedAspectRatio =
216223
aspectRatioLocked && viewportAspectRatio !== null ? viewportAspectRatio : null;
217-
const handleAspectRatioChange = useCallback((aspectRatio: number | null) => {
224+
const handleAspectRatioChange = (aspectRatio: number | null) => {
218225
setAspectRatioLocked(aspectRatio !== null);
219-
}, []);
220-
const hiddenContentSize = presentation.content
221-
? {
222-
width: presentation.content.width / presentation.content.scale,
223-
height: presentation.content.height / presentation.content.scale,
224-
}
225-
: null;
226-
const hiddenSize =
227-
viewport._tag !== "fill"
228-
? {
229-
width: viewport.width * normalizedZoomFactor,
230-
height: viewport.height * normalizedZoomFactor,
231-
}
232-
: {
233-
width: hiddenContentSize?.width ?? lastRect?.width ?? 1280,
234-
height: hiddenContentSize?.height ?? lastRect?.height ?? 800,
235-
};
226+
};
227+
const hiddenSize = useHiddenSize(viewport, normalizedZoomFactor, presentation.content, lastRect);
236228
const containerSize = active && lastRect ? lastRect : hiddenSize;
237229
const deviceToolbarVisible = active && viewport._tag !== "fill" && !presentation.fitSourceContent;
238230
const {
@@ -263,7 +255,7 @@ export function HostedBrowserWebview(props: {
263255
? resolveBrowserViewportLayout(lastRect, fittedSourceViewport, normalizedZoomFactor)
264256
: viewportLayout;
265257

266-
const syncContentPresentation = useCallback(() => {
258+
const syncContentPresentation = () => {
267259
const wrapper = wrapperRef.current;
268260
if (!wrapper) return;
269261
useBrowserSurfaceStore.getState().presentContent(runtimeTabId, {
@@ -275,7 +267,7 @@ export function HostedBrowserWebview(props: {
275267
scrollLeft: wrapper.scrollLeft,
276268
scrollTop: wrapper.scrollTop,
277269
});
278-
}, [layout, runtimeTabId]);
270+
};
279271

280272
useEffect(() => {
281273
const frameId = window.requestAnimationFrame(syncContentPresentation);
@@ -286,7 +278,13 @@ export function HostedBrowserWebview(props: {
286278
const wrapper = wrapperRef.current;
287279
if (!wrapper) return;
288280
wrapper.scrollTo({ left: 0, top: 0 });
289-
}, [runtimeTabId, viewport._tag, viewportHeight, viewportWidth]);
281+
}, [
282+
// oxlint-disable-next-line react/exhaustive-effect-dependencies -- A new tab or viewport size starts the canvas scrolled to its origin.
283+
runtimeTabId,
284+
viewport._tag,
285+
viewportHeight,
286+
viewportWidth,
287+
]);
290288

291289
if (!clientSettingsHydrated || !config) return null;
292290

@@ -392,3 +390,20 @@ export function HostedBrowserWebview(props: {
392390
</div>
393391
);
394392
}
393+
394+
// The size feeds useBrowserViewportResize, whose layout keys the content sync effect.
395+
function useHiddenSize(
396+
viewport: PreviewViewportSetting,
397+
zoomFactor: number,
398+
content: BrowserSurfaceContentPresentation | null,
399+
lastRect: BrowserSurfaceRect | null,
400+
) {
401+
"use memo";
402+
if (viewport._tag !== "fill") {
403+
return { width: viewport.width * zoomFactor, height: viewport.height * zoomFactor };
404+
}
405+
return {
406+
width: content ? content.width / content.scale : (lastRect?.width ?? 1280),
407+
height: content ? content.height / content.scale : (lastRect?.height ?? 800),
408+
};
409+
}

‎apps/web/src/browser/useOpenLink.ts‎

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import {
33
isAtomCommandInterrupted,
44
squashAtomCommandFailure,
55
} from "@t3tools/client-runtime/state/runtime";
6-
import { useCallback } from "react";
7-
86
import { recordVisitForThread } from "~/browserHistoryStore";
97
import { readLocalApi } from "~/localApi";
108
import { previewEnvironment } from "~/state/preview";
@@ -36,33 +34,30 @@ export function useOpenLink(threadRef: ScopedThreadRef | null | undefined): (
3634
},
3735
) => Promise<void> {
3836
const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
39-
return useCallback(
40-
async (url, options = {}) => {
41-
const targetThreadRef = options.threadRef ?? threadRef;
42-
const target = resolveLinkTarget({
43-
url,
44-
event: options.event ?? NO_MODIFIER,
45-
preference: await resolveBrowserLinkTargetPreference(),
46-
canOpenInApp:
47-
targetThreadRef != null &&
48-
readEnvironmentScope(targetThreadRef.environmentId, AuthPreviewOperateScope) &&
49-
canOpenLinksInApp(targetThreadRef),
50-
});
51-
if (target === "app" && targetThreadRef) {
52-
const result = await openUrlInPreview({ threadRef: targetThreadRef, url, openPreview });
53-
if (isAtomCommandInterrupted(result)) return;
54-
if (result._tag === "Success") {
55-
recordVisitForThread(targetThreadRef, url);
56-
return;
57-
}
58-
const failure = squashAtomCommandFailure(result);
59-
if (failure instanceof BrowserSettingsReadError) throw failure;
60-
console.error(result.cause);
37+
return async (url, options = {}) => {
38+
const targetThreadRef = options.threadRef ?? threadRef;
39+
const target = resolveLinkTarget({
40+
url,
41+
event: options.event ?? NO_MODIFIER,
42+
preference: await resolveBrowserLinkTargetPreference(),
43+
canOpenInApp:
44+
targetThreadRef != null &&
45+
readEnvironmentScope(targetThreadRef.environmentId, AuthPreviewOperateScope) &&
46+
canOpenLinksInApp(targetThreadRef),
47+
});
48+
if (target === "app" && targetThreadRef) {
49+
const result = await openUrlInPreview({ threadRef: targetThreadRef, url, openPreview });
50+
if (isAtomCommandInterrupted(result)) return;
51+
if (result._tag === "Success") {
52+
recordVisitForThread(targetThreadRef, url);
53+
return;
6154
}
62-
const api = readLocalApi();
63-
if (!api) throw new Error("Link opening is unavailable.");
64-
await api.shell.openExternal(url);
65-
},
66-
[openPreview, threadRef],
67-
);
55+
const failure = squashAtomCommandFailure(result);
56+
if (failure instanceof BrowserSettingsReadError) throw failure;
57+
console.error(result.cause);
58+
}
59+
const api = readLocalApi();
60+
if (!api) throw new Error("Link opening is unavailable.");
61+
await api.shell.openExternal(url);
62+
};
6863
}

‎apps/web/src/browserFaviconStore.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
} from "@t3tools/client-runtime/environment";
66
import type { DesktopPreviewFavicon, ScopedProjectRef, ScopedThreadRef } from "@t3tools/contracts";
77
import * as Option from "effect/Option";
8-
import { useMemo } from "react";
98
import { create } from "zustand";
109
import { createJSONStorage, persist } from "zustand/middleware";
1110

@@ -199,10 +198,8 @@ export function registerFaviconProjectForThread(
199198
export function useFaviconProjectRefForThread(threadRef: ScopedThreadRef): ScopedProjectRef | null {
200199
const shell = useThreadShell(threadRef);
201200
const shellProjectId = shell?.projectId ?? null;
202-
const shellProjectRef = useMemo(
203-
() => (shellProjectId ? scopeProjectRef(threadRef.environmentId, shellProjectId) : null),
204-
[shellProjectId, threadRef.environmentId],
205-
);
201+
const environmentId = threadRef.environmentId;
202+
const shellProjectRef = shellProjectId ? scopeProjectRef(environmentId, shellProjectId) : null;
206203
const registered = useBrowserFaviconStore(
207204
(state) => state.projectRefByThreadKey[scopedThreadKey(threadRef)] ?? null,
208205
);

0 commit comments

Comments
 (0)