Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Fix history preview tab styling on replay (#7601)

Nick Gomez656b7a168d21
diff --git a/.changeset/history-preview-reentry.md b/.changeset/history-preview-reentry.md
new file mode 100644
index 000000000..e87f6de68
--- /dev/null
+++ b/.changeset/history-preview-reentry.md
@@ -0,0 +1,5 @@
+---
+'@inkeep/open-knowledge': patch
+---
+
+Going back or forward to a document keeps it in the preview tab instead of pinning it, including right after a reload while the file list is still loading.
diff --git a/packages/app/src/App.dom.test.tsx b/packages/app/src/App.dom.test.tsx
index 1feb5f4f2..2b43faedb 100644
--- a/packages/app/src/App.dom.test.tsx
+++ b/packages/app/src/App.dom.test.tsx
@@ -778,6 +778,97 @@ describe('App runtime wiring', () => {
});
});
+ test('a traversal whose open changed nothing opens as a preview again on the next render', async () => {
+ setHash('#/page-a');
+ const view = renderApp();
+
+ await waitFor(() => {
+ expect(openTargetTransitionMock).toHaveBeenCalledWith(
+ { kind: 'doc', target: 'page-a', docName: 'page-a' },
+ expect.objectContaining({ disposition: 'permanent' }),
+ );
+ });
+
+ pushHashWithoutNavigation('#/page-b');
+ pushHashWithoutNavigation('#/page-c');
+ openTargetTransitionMock.mockClear();
+
+ window.history.back();
+
+ await waitFor(() => {
+ expect(window.location.hash).toBe('#/page-b');
+ expect(openTargetTransitionMock).toHaveBeenCalledWith(
+ { kind: 'doc', target: 'page-b', docName: 'page-b' },
+ expect.objectContaining({ disposition: 'preview' }),
+ );
+ });
+ expect(openTargetTransitionMock).toHaveBeenCalledTimes(1);
+
+ const retryTransitionMock = vi.fn(
+ (
+ _: NavigationTarget,
+ _options: { disposition: 'preview' | 'permanent'; consumeActiveNewTab: boolean },
+ ) => {},
+ );
+ openTargetTransitionMock = retryTransitionMock;
+ view.rerender(<App />);
+
+ await waitFor(() => expect(retryTransitionMock).toHaveBeenCalledTimes(1));
+ expect(retryTransitionMock).toHaveBeenCalledWith(
+ { kind: 'doc', target: 'page-b', docName: 'page-b' },
+ expect.objectContaining({ disposition: 'preview' }),
+ );
+
+ activeTarget = { kind: 'doc', target: 'page-b', docName: 'page-b' };
+ view.rerender(<App />);
+ retryTransitionMock.mockClear();
+
+ window.location.hash = '#/page-c';
+ await waitFor(() => {
+ expect(retryTransitionMock).toHaveBeenCalledWith(
+ { kind: 'doc', target: 'page-c', docName: 'page-c' },
+ expect.objectContaining({ disposition: 'permanent' }),
+ );
+ });
+ });
+
+ test('a traversal deferred while the page list loads opens as a preview once it loads', async () => {
+ setHash('#/page-a');
+ const view = renderApp();
+
+ await waitFor(() => {
+ expect(openTargetTransitionMock).toHaveBeenCalledWith(
+ { kind: 'doc', target: 'page-a', docName: 'page-a' },
+ expect.objectContaining({ disposition: 'permanent' }),
+ );
+ });
+
+ pushHashWithoutNavigation('#/page-b');
+ pushHashWithoutNavigation('#/page-c');
+ loading = true;
+ view.rerender(<App />);
+ openTargetTransitionMock.mockClear();
+
+ const traversalHandled = new Promise<void>((resolve) => {
+ window.addEventListener('hashchange', () => resolve(), { once: true });
+ });
+ window.history.back();
+ await traversalHandled;
+
+ expect(window.location.hash).toBe('#/page-b');
+ expect(openTargetTransitionMock).not.toHaveBeenCalled();
+
+ loading = false;
+ view.rerender(<App />);
+
+ await waitFor(() => {
+ expect(openTargetTransitionMock).toHaveBeenCalledWith(
+ { kind: 'doc', target: 'page-b', docName: 'page-b' },
+ expect.objectContaining({ disposition: 'preview' }),
+ );
+ });
+ });
+
test('direct hash navigation preserves the active tab', async () => {
setHash('#/page-a');
renderApp();
diff --git a/packages/app/src/App.tsx b/packages/app/src/App.tsx
index ef4de39a7..d07245436 100644
--- a/packages/app/src/App.tsx
+++ b/packages/app/src/App.tsx
@@ -244,7 +244,6 @@ function NavigationHandler() {
function onHashChange(event: HashChangeEvent) {
const traversedUrl = historyTraversalUrlRef.current;
- historyTraversalUrlRef.current = null;
const previousHash = event.oldURL === '' ? '' : new URL(event.oldURL).hash;
const workspaceWrote = consumeWorkspaceHashWrite(window.location.hash);
const returnedFromDialog =
diff --git a/packages/app/tests/stress/history-traversal-preview-tab.e2e.ts b/packages/app/tests/stress/history-traversal-preview-tab.e2e.ts
index 1f27b1b98..78557224b 100644
--- a/packages/app/tests/stress/history-traversal-preview-tab.e2e.ts
+++ b/packages/app/tests/stress/history-traversal-preview-tab.e2e.ts
@@ -1,6 +1,6 @@
import { randomUUID } from 'node:crypto';
import type { Locator, Page } from '@playwright/test';
-import { type ApiHelpers, expect, test } from './_helpers';
+import { type ApiHelpers, expect, REQUIRED_FIXTURE_ENTRY_NAMES, test } from './_helpers';
function sidebarTreeItem(page: Page, accessibleLabel: string): Locator {
return page
@@ -44,15 +44,62 @@ async function expectPreviewTab(tab: Locator, isPreview: boolean) {
}
}
+type ReentryWindow = Window & {
+ __repeatHashOnce?: boolean;
+ __wrappedHashListeners?: number;
+ __repeatedHashListeners?: number;
+};
+
+async function installHashChangeReentry(page: Page): Promise<void> {
+ await page.addInitScript(() => {
+ const add = window.addEventListener;
+ const remove = window.removeEventListener;
+ const wrappers = new WeakMap();
+ const appWindow = window as ReentryWindow;
+ let repeatedEvent: Event | null = null;
+ appWindow.__wrappedHashListeners = 0;
+ appWindow.__repeatedHashListeners = 0;
+ window.addEventListener = function (type, listener, options) {
+ if (type !== 'hashchange' || typeof listener !== 'function') {
+ return add.call(this, type, listener, options);
+ }
+ const wrapped = function (event) {
+ if (appWindow.__repeatHashOnce) {
+ appWindow.__repeatHashOnce = false;
+ repeatedEvent = event;
+ }
+ listener.call(this, event);
+ if (event === repeatedEvent) {
+ appWindow.__repeatedHashListeners = (appWindow.__repeatedHashListeners ?? 0) + 1;
+ listener.call(this, event);
+ }
+ };
+ wrappers.set(listener, wrapped);
+ appWindow.__wrappedHashListeners = (appWindow.__wrappedHashListeners ?? 0) + 1;
+ return add.call(this, type, wrapped, options);
+ };
+ window.removeEventListener = function (type, listener, options) {
+ return remove.call(this, type, wrappers.get(listener) ?? listener, options);
+ };
+ });
+}
+
+async function armHashChangeReentry(page: Page): Promise<void> {
+ const wrapped = await page.evaluate(() => (window as ReentryWindow).__wrappedHashListeners ?? 0);
+ expect(wrapped, 'no hashchange listener was wrapped for re-entry').toBeGreaterThan(0);
+ await page.evaluate(() => {
+ (window as ReentryWindow).__repeatHashOnce = true;
+ });
+}
+
async function seedThreeDocs(api: ApiHelpers, page: Page): Promise<[string, string, string]> {
const id = randomUUID().slice(0, 8);
const names: [string, string, string] = [`hist-a-${id}`, `hist-b-${id}`, `hist-c-${id}`];
- for (const name of names) {
- await api.createPage(`${name}.md`);
- await api.replaceDoc(name, `# ${name}\n\nSeed body.\n`);
- }
+ await api.seedDocs(names.map((name) => ({ name, markdown: `# ${name}\n\nSeed body.\n` })));
await page.goto('/');
- await expect(sidebarTreeItem(page, `${names[0]}.md`)).toBeVisible();
+ for (const label of [...REQUIRED_FIXTURE_ENTRY_NAMES, ...names.map((name) => `${name}.md`)]) {
+ await expect(sidebarTreeItem(page, label)).toBeVisible();
+ }
return names;
}
@@ -88,4 +135,32 @@ test.describe('history traversal and the preview tab slot', () => {
await expectOpenTabs(page, [b]);
await expectPreviewTab(editorTab(page, `${b}.md`), true);
});
+
+ test('back to a previewed doc remains preview when the hash handler is re-entered', async ({
+ page,
+ api,
+ }) => {
+ await installHashChangeReentry(page);
+ const [a, b, c] = await seedThreeDocs(api, page);
+
+ await sidebarTreeItem(page, `${a}.md`).click();
+ await expectOpenTabs(page, [a]);
+ await sidebarTreeItem(page, `${b}.md`).click();
+ await expectOpenTabs(page, [b]);
+ await sidebarTreeItem(page, `${c}.md`).click();
+ await expectOpenTabs(page, [c]);
+ await expectPreviewTab(editorTab(page, `${c}.md`), true);
+
+ await armHashChangeReentry(page);
+ await page.goBack();
+ await expect
+ .poll(() => page.evaluate(() => (window as ReentryWindow).__repeatedHashListeners ?? 0), {
+ message: 'the armed hashchange was not delivered twice to its listeners',
+ })
+ .toBeGreaterThan(0);
+ await expectHash(page, `#/${b}`);
+ await expectOpenTabs(page, [b]);
+ await expectPreviewTab(editorTab(page, `${b}.md`), true);
+ await expect(editorTab(page, `${c}.md`)).toHaveCount(0);
+ });
});