GeezerVison is showing github.com — fix(web): browser viewport resizes work without an open preview panel… · pingdotgg/t3code@f05c38f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/AppRoot.tsx -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,7 +5,6 @@ import { subscribeComposerDraftModelReconciliation } from "./composerDraftStore" | 5 | 5 | import { environmentThreadShells } from "./state/threads"; | 6 | 6 | import { environmentShell } from "./state/shell"; | 7 | 7 |
| 8 | | -import { BrowserProfileReporter } from "./browser/BrowserProfileReporter"; | 9 | 8 | import { ElectronBrowserHost } from "./browser/ElectronBrowserHost"; | 10 | 9 | import { QuitHoldOverlay } from "./components/QuitHoldOverlay"; | 11 | 10 | import { appAtomRegistry, AppAtomRegistryProvider } from "./rpc/atomRegistry"; | @@ -27,7 +26,6 @@ export function AppRoot({ router }: { readonly router: AppRouter }) { | 27 | 26 | <AppAtomRegistryProvider> | 28 | 27 | <RouterProvider router={router} /> | 29 | 28 | <ElectronBrowserHost /> | 30 | | - <BrowserProfileReporter /> | 31 | 29 | <QuitHoldOverlay /> | 32 | 30 | </AppAtomRegistryProvider> | 33 | 31 | ); | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/appInit.tsx +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,8 @@ import { createBrowserHistory, createHashHistory } from "@tanstack/react-router" | 5 | 5 | import { applyAppearanceContrast } from "./appearanceContrast"; | 6 | 6 | import { applyAppearanceFontVariables } from "./appearanceFonts"; | 7 | 7 | import { startElectronBrowserHost } from "./browser/ElectronBrowserHost"; | | 8 | +import { startBrowserProfileReporting } from "./browser/BrowserProfileReporter"; | | 9 | +import { startPreviewTitleRecording } from "./browserHistoryStore"; | 8 | 10 | import { retainThreadOverlaysForThread } from "./components/chat/threadOverlayRetention"; | 9 | 11 | import { startBackgroundThreadAnnouncements } from "./components/BackgroundThreadAnnouncer"; | 10 | 12 | import { startSshPasswordPromptQueue } from "./components/desktop/SshPasswordPromptDialog"; | @@ -164,6 +166,8 @@ export function startAppServices(): AppRouter { | 164 | 166 | startModelPreferencesMigration(); | 165 | 167 | startAgentOpenedPreviews(); | 166 | 168 | if (isElectron) startElectronBrowserHost(); | | 169 | + startPreviewTitleRecording(); | | 170 | + startBrowserProfileReporting(); | 167 | 171 | startThreadOverlayRetention(router); | 168 | 172 | startMainAppLocationTracking(router); | 169 | 173 | startBackgroundThreadAnnouncements(appAtomRegistry, router); | |
Collapse file Copy file name to clipboard +64Lines changed: 64 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { EnvironmentServerConfig } from "@t3tools/client-runtime/connection"; | | 2 | +import { EnvironmentId } from "@t3tools/contracts"; | | 3 | +import { describe, expect, it } from "vite-plus/test"; | | 4 | + | | 5 | +import { | | 6 | + type ProfileReport, | | 7 | + type ReportedProfiles, | | 8 | + takeDueProfileReports, | | 9 | +} from "./BrowserProfileReporter"; | | 10 | + | | 11 | +const first = EnvironmentId.make("environment-1"); | | 12 | +const second = EnvironmentId.make("environment-2"); | | 13 | +const config = (label: string) => ({ label }) as unknown as EnvironmentServerConfig; | | 14 | +const input = (recordingFrameRate: number) => | | 15 | + ({ profiles: [], defaultProfileId: "default", recordingFrameRate }) as unknown as ProfileReport; | | 16 | + | | 17 | +describe("takeDueProfileReports", () => { | | 18 | + it("reports each environment once per config and input", () => { | | 19 | + const reported: ReportedProfiles = new Map(); | | 20 | + const configA = config("a"); | | 21 | + const configB = config("b"); | | 22 | + const settings = input(10); | | 23 | + const targets = new Map([ | | 24 | + [first, configA], | | 25 | + [second, configB], | | 26 | + ]); | | 27 | + | | 28 | + expect(takeDueProfileReports(reported, targets, settings)).toEqual([first, second]); | | 29 | + expect(takeDueProfileReports(reported, new Map(targets), settings)).toEqual([]); | | 30 | + | | 31 | + const restarted = config("a-restarted"); | | 32 | + expect( | | 33 | + takeDueProfileReports( | | 34 | + reported, | | 35 | + new Map([ | | 36 | + [first, restarted], | | 37 | + [second, configB], | | 38 | + ]), | | 39 | + settings, | | 40 | + ), | | 41 | + ).toEqual([first]); | | 42 | + }); | | 43 | + | | 44 | + it("reports every target again when the profile settings change", () => { | | 45 | + const reported: ReportedProfiles = new Map(); | | 46 | + const targets = new Map([ | | 47 | + [first, config("a")], | | 48 | + [second, config("b")], | | 49 | + ]); | | 50 | + takeDueProfileReports(reported, targets, input(10)); | | 51 | + | | 52 | + expect(takeDueProfileReports(reported, targets, input(30))).toEqual([first, second]); | | 53 | + }); | | 54 | + | | 55 | + it("reports an environment again once it becomes operable again", () => { | | 56 | + const reported: ReportedProfiles = new Map(); | | 57 | + const configA = config("a"); | | 58 | + const settings = input(10); | | 59 | + takeDueProfileReports(reported, new Map([[first, configA]]), settings); | | 60 | + | | 61 | + expect(takeDueProfileReports(reported, new Map(), settings)).toEqual([]); | | 62 | + expect(takeDueProfileReports(reported, new Map([[first, configA]]), settings)).toEqual([first]); | | 63 | + }); | | 64 | +}); |
Collapse file Copy file name to clipboard +106Lines changed: 106 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { EnvironmentServerConfig } from "@t3tools/client-runtime/connection"; | | 2 | +import { runAtomCommand } from "@t3tools/client-runtime/state/runtime"; | | 3 | +import { AuthPreviewOperateScope, type EnvironmentId } from "@t3tools/contracts"; | | 4 | +import type { ClientSettings } from "@t3tools/contracts/settings"; | | 5 | +import { Atom } from "effect/reactivity"; | | 6 | + | | 7 | +import { | | 8 | + getClientSettings, | | 9 | + getClientSettingsHydratedSnapshot, | | 10 | + subscribeClientSettings, | | 11 | + subscribeClientSettingsHydration, | | 12 | +} from "~/hooks/useSettings"; | | 13 | +import { appAtomRegistry } from "~/rpc/atomRegistry"; | | 14 | +import { environmentPresentations } from "~/state/presentation"; | | 15 | +import { previewEnvironment } from "~/state/preview"; | | 16 | +import { environmentSession, sessionHasScope } from "~/state/session"; | | 17 | + | | 18 | +export interface ProfileReport { | | 19 | + readonly profiles: ClientSettings["browserProfiles"]; | | 20 | + readonly defaultProfileId: ClientSettings["browserDefaultProfileId"]; | | 21 | + readonly recordingFrameRate: ClientSettings["browserRecordingFrameRate"]; | | 22 | +} | | 23 | + | | 24 | +/** Server-browser environments this client may operate, with the config each currently runs. */ | | 25 | +const reportTargetsAtom = Atom.make((get) => { | | 26 | + const targets = new Map<EnvironmentId, EnvironmentServerConfig>(); | | 27 | + for (const [environmentId, config] of get(environmentPresentations.serverConfigsAtom)) { | | 28 | + if (!config.environment.capabilities.serverBrowser) continue; | | 29 | + const session = get(environmentSession.sessionStateAtom(environmentId)); | | 30 | + if (!sessionHasScope(session, AuthPreviewOperateScope)) continue; | | 31 | + targets.set(environmentId, config); | | 32 | + } | | 33 | + return targets; | | 34 | +}).pipe(Atom.withLabel("browser-profile-report-targets")); | | 35 | + | | 36 | +/** | | 37 | + * Keeps every operable server browser on this client's profiles. An environment is reported | | 38 | + * again when its server config changes (a restarted server has forgotten them), when it becomes | | 39 | + * operable, and when the profile settings change. | | 40 | + */ | | 41 | +export function startBrowserProfileReporting(): () => void { | | 42 | + const reported: ReportedProfiles = new Map(); | | 43 | + const report = () => { | | 44 | + if (!getClientSettingsHydratedSnapshot()) return; | | 45 | + const settings = getClientSettings(); | | 46 | + const input: ProfileReport = { | | 47 | + profiles: settings.browserProfiles, | | 48 | + defaultProfileId: settings.browserDefaultProfileId, | | 49 | + recordingFrameRate: settings.browserRecordingFrameRate, | | 50 | + }; | | 51 | + const targets = appAtomRegistry.get(reportTargetsAtom); | | 52 | + for (const environmentId of takeDueProfileReports(reported, targets, input)) { | | 53 | + void runAtomCommand( | | 54 | + appAtomRegistry, | | 55 | + previewEnvironment.reportProfiles, | | 56 | + { environmentId, input }, | | 57 | + { reportFailure: false }, | | 58 | + ); | | 59 | + } | | 60 | + }; | | 61 | + const unsubscribes = [ | | 62 | + appAtomRegistry.subscribe(reportTargetsAtom, report), | | 63 | + subscribeClientSettings(report), | | 64 | + subscribeClientSettingsHydration(report), | | 65 | + ]; | | 66 | + report(); | | 67 | + return () => { | | 68 | + for (const unsubscribe of unsubscribes) unsubscribe(); | | 69 | + }; | | 70 | +} | | 71 | + | | 72 | +export type ReportedProfiles = Map< | | 73 | + EnvironmentId, | | 74 | + { readonly config: EnvironmentServerConfig; readonly input: ProfileReport } | | 75 | +>; | | 76 | + | | 77 | +/** | | 78 | + * Returns the targets that need `input` reported and records them in `reported`. An | | 79 | + * environment that dropped out of `targets` is forgotten, so it is reported again once it | | 80 | + * becomes operable. | | 81 | + */ | | 82 | +export function takeDueProfileReports( | | 83 | + reported: ReportedProfiles, | | 84 | + targets: ReadonlyMap<EnvironmentId, EnvironmentServerConfig>, | | 85 | + input: ProfileReport, | | 86 | +): EnvironmentId[] { | | 87 | + for (const environmentId of reported.keys()) { | | 88 | + if (!targets.has(environmentId)) reported.delete(environmentId); | | 89 | + } | | 90 | + const due: EnvironmentId[] = []; | | 91 | + for (const [environmentId, config] of targets) { | | 92 | + const last = reported.get(environmentId); | | 93 | + if (last && last.config === config && sameReportInput(last.input, input)) continue; | | 94 | + reported.set(environmentId, { config, input }); | | 95 | + due.push(environmentId); | | 96 | + } | | 97 | + return due; | | 98 | +} | | 99 | + | | 100 | +function sameReportInput(left: ProfileReport, right: ProfileReport): boolean { | | 101 | + return ( | | 102 | + left.profiles === right.profiles && | | 103 | + left.defaultProfileId === right.defaultProfileId && | | 104 | + left.recordingFrameRate === right.recordingFrameRate | | 105 | + ); | | 106 | +} |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/browser/BrowserProfileReporter.tsx -42Lines changed: 0 additions & 42 deletions Collapse file Copy file name to clipboardExpand all lines: apps/web/src/browser/HostedBrowserWebview.tsx +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -236,7 +236,7 @@ export function HostedBrowserWebview(props: { | 236 | 236 | handleResizePointerDown, | 237 | 237 | layout: viewportLayout, | 238 | 238 | } = useBrowserViewportResize({ | 239 | | - tabId: runtimeTabId, | | 239 | + target: { threadRef, tabId, runtimeTabId }, | 240 | 240 | viewport, | 241 | 241 | zoomFactor: layoutZoomFactor, | 242 | 242 | containerSize, | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/browser/ServerBrowserSurface.tsx +1-8Lines changed: 1 addition & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,7 +49,6 @@ export interface ServerBrowserHandle { | 49 | 49 | } | 50 | 50 |
| 51 | 51 | const RESIZE_THROTTLE_MS = 150; | 52 | | -const ACCESS_RETRY_MS = 10_000; | 53 | 52 | // Reuse nearby probes when a slow link would return the tap's answer after it ends. | 54 | 53 | const PROBE_REUSE_PX = 24; | 55 | 54 | const PROBE_REUSE_MS = 10_000; | @@ -161,7 +160,6 @@ export function ServerBrowserSurface(props: { | 161 | 160 | pdfUrl = null, | 162 | 161 | ref, | 163 | 162 | } = props; | 164 | | - const access = usePreviewStreamAccess(environmentId); | 165 | 163 | const canvasRef = useRef<HTMLCanvasElement | null>(null); | 166 | 164 | const inputRef = useRef<HTMLTextAreaElement | null>(null); | 167 | 165 | const keySentRef = useRef(false); | @@ -189,6 +187,7 @@ export function ServerBrowserSurface(props: { | 189 | 187 | }; | 190 | 188 | const unauthorizedRef = useRef(0); | 191 | 189 | const [accessDenied, setAccessDenied] = useState(false); | | 190 | + const access = usePreviewStreamAccess(environmentId, { retry: visible && !accessDenied }); | 192 | 191 | const [hostSetup, setHostSetup] = useState<PreviewStreamHostSetup | null>(null); | 193 | 192 | const pendingMoveRef = useRef<MouseInput | null>(null); | 194 | 193 | const pendingWheelRef = useRef<WheelInput | null>(null); | @@ -458,12 +457,6 @@ export function ServerBrowserSurface(props: { | 458 | 457 | visible, | 459 | 458 | ]); | 460 | 459 |
| 461 | | - useEffect(() => { | 462 | | - if (!visible || accessDenied || access !== null) return; | 463 | | - const timer = setInterval(() => refreshPreviewStreamAccess(environmentId), ACCESS_RETRY_MS); | 464 | | - return () => clearInterval(timer); | 465 | | - }, [access, accessDenied, environmentId, visible]); | 466 | | - | 467 | 460 | useEffect(() => { | 468 | 461 | const canvas = canvasRef.current; | 469 | 462 | if (!canvas) return; | |
Collapse file Copy file name to clipboard +59-17Lines changed: 59 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { describe, expect, it } from "vite-plus/test"; | | 1 | +import { describe, expect, it, vi } from "vite-plus/test"; | 2 | 2 |
| 3 | 3 | import { | 4 | | - commitBrowserViewportChange, | 5 | | - subscribeBrowserViewportChange, | 6 | | -} from "./browserViewportActions"; | | 4 | + EnvironmentId, | | 5 | + type PreviewViewportSetting, | | 6 | + ThreadId, | | 7 | + type PreviewSessionSnapshot, | | 8 | +} from "@t3tools/contracts"; | | 9 | + | | 10 | +const mocks = vi.hoisted(() => ({ | | 11 | + resize: Symbol("preview-resize"), | | 12 | + runAtomCommand: vi.fn(), | | 13 | + updatePreviewServerSnapshot: vi.fn(), | | 14 | +})); | | 15 | + | | 16 | +vi.mock("@t3tools/client-runtime/state/runtime", () => ({ | | 17 | + runAtomCommand: mocks.runAtomCommand, | | 18 | + squashAtomCommandFailure: (result: { readonly error: unknown }) => result.error, | | 19 | +})); | | 20 | +vi.mock("~/rpc/atomRegistry", () => ({ appAtomRegistry: {} })); | | 21 | +vi.mock("~/state/preview", () => ({ previewEnvironment: { resize: mocks.resize } })); | | 22 | +vi.mock("~/previewStateStore", () => ({ | | 23 | + updatePreviewServerSnapshot: mocks.updatePreviewServerSnapshot, | | 24 | +})); | | 25 | +vi.mock("~/components/ui/toast", () => ({ toastManager: { add: vi.fn() } })); | | 26 | + | | 27 | +import { commitBrowserViewportChange, queueBrowserViewportCommit } from "./browserViewportActions"; | 7 | 28 |
| 8 | 29 | describe("browserViewportActions", () => { | 9 | 30 | it("commits viewport changes in order for each tab", async () => { | @@ -16,31 +37,52 @@ describe("browserViewportActions", () => { | 16 | 37 | markFirstStarted = resolve; | 17 | 38 | }); | 18 | 39 | const calls: Array<number> = []; | 19 | | - const unsubscribe = subscribeBrowserViewportChange("tab-serial", async (setting) => { | | 40 | + const resize = async (setting: PreviewViewportSetting) => { | 20 | 41 | if (setting._tag === "fill") return; | 21 | 42 | calls.push(setting.width); | 22 | 43 | if (setting.width === 800) { | 23 | 44 | markFirstStarted?.(); | 24 | 45 | await firstPending; | 25 | 46 | } | 26 | | - }); | | 47 | + }; | 27 | 48 |
| 28 | | - const first = commitBrowserViewportChange("tab-serial", { | 29 | | - _tag: "freeform", | 30 | | - width: 800, | 31 | | - height: 600, | 32 | | - }); | 33 | | - const second = commitBrowserViewportChange("tab-serial", { | 34 | | - _tag: "freeform", | 35 | | - width: 900, | 36 | | - height: 700, | 37 | | - }); | | 49 | + const first = queueBrowserViewportCommit("tab-serial", () => | | 50 | + resize({ _tag: "freeform", width: 800, height: 600 }), | | 51 | + ); | | 52 | + const second = queueBrowserViewportCommit("tab-serial", () => | | 53 | + resize({ _tag: "freeform", width: 900, height: 700 }), | | 54 | + ); | 38 | 55 | await firstStarted; | 39 | 56 | expect(calls).toEqual([800]); | 40 | 57 |
| 41 | 58 | releaseFirst?.(); | 42 | 59 | await Promise.all([first, second]); | 43 | 60 | expect(calls).toEqual([800, 900]); | 44 | | - unsubscribe(); | | 61 | + }); | | 62 | + | | 63 | + it("resizes a tab that no preview panel is showing", async () => { | | 64 | + const threadRef = { | | 65 | + environmentId: EnvironmentId.make("env-1"), | | 66 | + threadId: ThreadId.make("thread-1"), | | 67 | + }; | | 68 | + const snapshot = { tabId: "tab-1" } as PreviewSessionSnapshot; | | 69 | + mocks.runAtomCommand.mockResolvedValueOnce({ _tag: "Success", value: snapshot }); | | 70 | + const viewport: PreviewViewportSetting = { _tag: "freeform", width: 390, height: 844 }; | | 71 | + | | 72 | + await commitBrowserViewportChange( | | 73 | + { threadRef, tabId: "tab-1", runtimeTabId: "runtime-tab-1" }, | | 74 | + viewport, | | 75 | + ); | | 76 | + | | 77 | + expect(mocks.runAtomCommand).toHaveBeenCalledWith( | | 78 | + expect.anything(), | | 79 | + mocks.resize, | | 80 | + { | | 81 | + environmentId: threadRef.environmentId, | | 82 | + input: { threadId: threadRef.threadId, tabId: "tab-1", viewport }, | | 83 | + }, | | 84 | + expect.anything(), | | 85 | + ); | | 86 | + expect(mocks.updatePreviewServerSnapshot).toHaveBeenCalledWith(threadRef, snapshot); | 45 | 87 | }); | 46 | 88 | }); |
|