Skip to content

Commit 3ae6e99

Browse files
fix(web): recording a visit no longer replays the thread's older titles
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d7fc08f commit 3ae6e99

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

‎apps/web/src/browserHistoryStore.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@ describe("useBrowserHistoryStore", () => {
178178
).toBe("New");
179179
});
180180

181+
it("keeps another thread's newer title when this thread records a different visit", () => {
182+
const otherThreadRef = { ...threadRef, threadId: ThreadId.make("thread-2") };
183+
const store = useBrowserHistoryStore.getState();
184+
store.registerThreadProject(threadRef, "proj-a");
185+
store.registerThreadProject(otherThreadRef, "proj-a");
186+
recordVisitForThread(threadRef, "http://a.test/", 1);
187+
settle("http://a.test/", "Old", "tab_a", threadRef);
188+
settle("http://a.test/", "New", "tab_a", otherThreadRef);
189+
recordVisitForThread(threadRef, "http://b.test/", 2);
190+
expect(
191+
useBrowserHistoryStore
192+
.getState()
193+
.byProjectKey["proj-a"]?.find((e) => e.url === "http://a.test/")?.title,
194+
).toBe("New");
195+
});
196+
181197
it("sets a title against a settled url that differs from the stored one only by a trailing slash", () => {
182198
useBrowserHistoryStore.getState().registerThreadProject(threadRef, "proj-a");
183199
recordVisitForThread(threadRef, "http://a.test/community", 1);

‎apps/web/src/browserHistoryStore.ts‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2-
import type { ScopedThreadRef } from "@t3tools/contracts";
2+
import type { PreviewSessionSnapshot, ScopedThreadRef } from "@t3tools/contracts";
33
import { create } from "zustand";
44
import { createJSONStorage, persist } from "zustand/middleware";
55
import { useShallow } from "zustand/react/shallow";
@@ -261,7 +261,10 @@ export const useBrowserHistoryStore = create<BrowserHistoryStoreState>()(
261261
insertOrdered: true,
262262
environmentHostname: visit.environmentHostname,
263263
});
264-
recordPreviewTitles(ref, readThreadPreviewState(ref));
264+
if (pendingVisits)
265+
recordPreviewTitles(ref, readThreadPreviewState(ref), {
266+
urls: pendingVisits.map((visit) => visit.url),
267+
});
265268
},
266269
}),
267270
{
@@ -329,32 +332,42 @@ export function recordVisitForThread(ref: ScopedThreadRef, url: string, at?: num
329332
}
330333
state.recordVisit(projectKey, url, visitAt, { environmentHostname });
331334
// A page can settle before the address bar records its visit.
332-
recordPreviewTitles(ref, readThreadPreviewState(ref));
335+
recordPreviewTitles(ref, readThreadPreviewState(ref), { urls: [url] });
333336
}
334337

335338
/**
336-
* Titles only update entries already in the thread's project history, so they
337-
* are applied both when a page settles and when its visit is recorded.
339+
* Titles only update entries already in history, so they apply on settle and on
340+
* visit. Callers pass only what just happened: history is shared across a
341+
* project's threads, so replaying an older settle would clobber a newer title.
338342
*/
339343
function recordPreviewTitles(
340344
ref: ScopedThreadRef,
341345
preview: ThreadPreviewState,
342-
previousSessions?: ThreadPreviewState["sessions"],
346+
only:
347+
| { readonly urls: ReadonlyArray<string> }
348+
| { readonly changedSince: ThreadPreviewState["sessions"] },
343349
): void {
344350
const state = useBrowserHistoryStore.getState();
345351
const projectKey = state.projectKeyByThreadKey[scopedThreadKey(ref)];
346352
if (!projectKey || !state.byProjectKey[projectKey]?.length) return;
347353
const connection = readPreparedConnection(ref.environmentId);
348354
const environmentHostname = connection ? new URL(connection.httpBaseUrl).hostname : null;
355+
const titleKey = (url: string) => {
356+
const normalized = normalizeHistoryUrl(url);
357+
return normalized ? titleLookupKey(normalized, environmentHostname) : null;
358+
};
359+
const visitKeys = "urls" in only ? new Set(only.urls.map(titleKey)) : null;
360+
const include = (session: PreviewSessionSnapshot, url: string) =>
361+
"changedSince" in only
362+
? only.changedSince[session.tabId] !== session
363+
: (visitKeys?.has(titleKey(url)) ?? false);
349364
// Tabs can share a URL; applying oldest first lets the newest settle win.
350-
// Unchanged sessions are skipped so a stale title cannot overwrite a newer
351-
// one another thread in the same project recorded since.
352-
const sessions = Object.values(preview.sessions)
353-
.filter((session) => previousSessions?.[session.tabId] !== session)
354-
.toSorted((a, b) => a.updatedAt.localeCompare(b.updatedAt));
365+
const sessions = Object.values(preview.sessions).toSorted((a, b) =>
366+
a.updatedAt.localeCompare(b.updatedAt),
367+
);
355368
for (const session of sessions) {
356369
const status = session.navStatus;
357-
if (status._tag !== "Success" || !status.title) continue;
370+
if (status._tag !== "Success" || !status.title || !include(session, status.url)) continue;
358371
state.setTitleForUrl(projectKey, status.url, status.title, environmentHostname);
359372
}
360373
}
@@ -373,7 +386,7 @@ export function startPreviewTitleRecording(): () => void {
373386
if (next.sessions === previousSessions) return;
374387
const changedSince = previousSessions;
375388
previousSessions = next.sessions;
376-
recordPreviewTitles(threadRef, next, changedSince);
389+
recordPreviewTitles(threadRef, next, { changedSince });
377390
}),
378391
);
379392
}

0 commit comments

Comments
 (0)