Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions apps/web/src/components/preview/usePreviewBridge.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DesktopPreviewTabState } from "@t3tools/contracts";
import { ThreadId, type DesktopPreviewTabState } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";

import { createCaptureActivity, projectDesktopState } from "./usePreviewBridge";
import { buildReportInput, createCaptureActivity, projectDesktopState } from "./usePreviewBridge";

const favicon = {
dataUrl: "data:image/png;base64,AAAA",
Expand All @@ -30,6 +30,41 @@ function state(navStatus: DesktopPreviewTabState["navStatus"]): DesktopPreviewTa
};
}

describe("buildReportInput", () => {
it("reports title changes at the same URL without repeating unrelated desktop updates", () => {
const threadId = ThreadId.make("thread-1");
const tabId = "tab-1";
const url = "http://localhost:3000/app";
let lastReport = {
lastReportedUrl: null as string | null,
lastReportedKind: null as DesktopPreviewTabState["navStatus"]["kind"] | null,
lastReportedTitle: null as string | null,
};
const report = (nextState: DesktopPreviewTabState) => {
const result = buildReportInput({ threadId, tabId, state: nextState, ...lastReport });
if (result) {
lastReport = result;
}
return result?.input.navStatus ?? null;
};

const loaded = state({ kind: "Success", url, title: "Original title" });
expect(report(loaded)).toEqual({ _tag: "Success", url, title: "Original title" });
expect(report(loaded)).toBeNull();

const retitled = state({ kind: "Success", url, title: "Updated title" });
expect(report(retitled)).toEqual({ _tag: "Success", url, title: "Updated title" });
expect(
report({
...retitled,
audible: true,
controller: "human",
updatedAt: "2026-08-09T00:00:01.000Z",
}),
).toBeNull();
});
});

describe("projectDesktopState", () => {
it("shows a retained icon only while the current document has the captured origin", () => {
expect(
Expand Down
15 changes: 12 additions & 3 deletions apps/web/src/components/preview/usePreviewBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function usePreviewBridge(input: {

const lastReportedUrl = useRef<string | null>(null);
const lastReportedKind = useRef<DesktopPreviewTabState["navStatus"]["kind"] | null>(null);
const lastReportedTitle = useRef<string | null>(null);
const lastDesktopNavStatus = useRef<DesktopPreviewTabState["navStatus"] | null>(null);
const captureActivity = useRef<ReturnType<typeof createCaptureActivity> | null>(null);
const handleStateChange = useEffectEvent(
Expand All @@ -74,10 +75,12 @@ export function usePreviewBridge(input: {
state,
lastReportedUrl: lastReportedUrl.current,
lastReportedKind: lastReportedKind.current,
lastReportedTitle: lastReportedTitle.current,
});
if (!reported || serverDriven) return;
lastReportedUrl.current = reported.lastReportedUrl;
lastReportedKind.current = reported.lastReportedKind;
lastReportedTitle.current = reported.lastReportedTitle;
void reportStatus({
environmentId: stableThreadRef.environmentId,
input: reported.input,
Expand All @@ -89,6 +92,7 @@ export function usePreviewBridge(input: {
if (!previewBridge || typeof window === "undefined") return;
lastReportedUrl.current = null;
lastReportedKind.current = null;
lastReportedTitle.current = null;
lastDesktopNavStatus.current = null;
const activity = createCaptureActivity(runtimeTabId);
captureActivity.current = activity;
Expand Down Expand Up @@ -134,25 +138,28 @@ export function projectDesktopState(state: DesktopPreviewTabState): DesktopPrevi
};
}

function buildReportInput(args: {
export function buildReportInput(args: {
readonly threadId: ThreadId;
readonly tabId: string;
readonly state: DesktopPreviewTabState;
readonly lastReportedUrl: string | null;
readonly lastReportedKind: DesktopPreviewTabState["navStatus"]["kind"] | null;
readonly lastReportedTitle: string | null;
}): {
readonly input: PreviewReportStatusInput;
readonly lastReportedUrl: string;
readonly lastReportedKind: DesktopPreviewTabState["navStatus"]["kind"];
readonly lastReportedTitle: string;
} | null {
const { threadId, tabId, state, lastReportedUrl, lastReportedKind } = args;
const { threadId, tabId, state, lastReportedUrl, lastReportedKind, lastReportedTitle } = args;
const status = state.navStatus;
if (status.kind === "Idle") return null;

const sameAsLast =
status.kind !== "LoadFailed" &&
status.kind === lastReportedKind &&
status.url === lastReportedUrl;
status.url === lastReportedUrl &&
status.title === lastReportedTitle;
if (sameAsLast) return null;

const base = {
Expand All @@ -175,6 +182,7 @@ function buildReportInput(args: {
},
lastReportedUrl: status.url,
lastReportedKind: "LoadFailed",
lastReportedTitle: status.title,
};
}
return {
Expand All @@ -184,5 +192,6 @@ function buildReportInput(args: {
},
lastReportedUrl: status.url,
lastReportedKind: status.kind,
lastReportedTitle: status.title,
};
}
Loading