Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Keep sidebar-opened files in the preview tab (#7650)

Nick Gomez662b2fd3d8a2
diff --git a/.changeset/preview-hash-disposition.md b/.changeset/preview-hash-disposition.md
new file mode 100644
index 000000000..aa768d8af
--- /dev/null
+++ b/.changeset/preview-hash-disposition.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Opening a file or skill from the sidebar keeps it in the preview tab instead of sometimes pinning it.
diff --git a/packages/app/src/App.dom.test.tsx b/packages/app/src/App.dom.test.tsx
index 2b43faedb..9dfe8e8f6 100644
--- a/packages/app/src/App.dom.test.tsx
+++ b/packages/app/src/App.dom.test.tsx
@@ -3,9 +3,9 @@ import type { ReactNode } from 'react';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { TooltipProvider } from '@/components/ui/tooltip';
import {
+ clearHashWithoutNavigation,
hashFromDocName,
pushHashWithoutNavigation,
- replaceHashWithoutNavigation,
} from '@/lib/doc-hash';
import { matchesKeyboardShortcut, type ShortcutEventLike } from '@/lib/keyboard-shortcuts';
import {
@@ -717,8 +717,8 @@ describe('App runtime wiring', () => {
);
});
- pushHashWithoutNavigation('#/page-b');
- pushHashWithoutNavigation('#/page-c');
+ pushHashWithoutNavigation('#/page-b', 'permanent');
+ pushHashWithoutNavigation('#/page-c', 'permanent');
openTargetTransitionMock.mockClear();
window.history.back();
@@ -789,8 +789,8 @@ describe('App runtime wiring', () => {
);
});
- pushHashWithoutNavigation('#/page-b');
- pushHashWithoutNavigation('#/page-c');
+ pushHashWithoutNavigation('#/page-b', 'permanent');
+ pushHashWithoutNavigation('#/page-c', 'permanent');
openTargetTransitionMock.mockClear();
window.history.back();
@@ -843,8 +843,8 @@ describe('App runtime wiring', () => {
);
});
- pushHashWithoutNavigation('#/page-b');
- pushHashWithoutNavigation('#/page-c');
+ pushHashWithoutNavigation('#/page-b', 'permanent');
+ pushHashWithoutNavigation('#/page-c', 'permanent');
loading = true;
view.rerender(<App />);
openTargetTransitionMock.mockClear();
@@ -909,6 +909,43 @@ describe('App runtime wiring', () => {
});
});
+ test('keeps a sidebar preview disposition while the active workspace publication lags', async () => {
+ activeTarget = { kind: 'doc', target: 'page-a', docName: 'page-a' };
+ openTabs = ['page-a'];
+ setHash('#/page-a');
+ const view = renderApp();
+
+ await Promise.resolve();
+ openTargetTransitionMock.mockClear();
+
+ pushHashWithoutNavigation('#/page-b', 'preview');
+ openTabs = ['page-a', 'page-b'];
+ view.rerender(<App />);
+
+ await waitFor(() => {
+ expect(openTargetTransitionMock).toHaveBeenCalledWith(
+ { kind: 'doc', target: 'page-b', docName: 'page-b' },
+ expect.objectContaining({ disposition: 'preview' }),
+ );
+ });
+ });
+
+ test('opens a stored preview hash entry as a permanent tab while preview tabs are disabled', async () => {
+ mergedConfig = { editor: { previewTabs: false } };
+ activeTarget = { kind: 'doc', target: 'page-a', docName: 'page-a' };
+ openTabs = ['page-a', 'page-b'];
+ setHash('#/page-a');
+ pushHashWithoutNavigation('#/page-b', 'preview');
+ renderApp();
+
+ await waitFor(() => {
+ expect(openTargetTransitionMock).toHaveBeenCalledWith(
+ { kind: 'doc', target: 'page-b', docName: 'page-b' },
+ expect.objectContaining({ disposition: 'permanent' }),
+ );
+ });
+ });
+
async function renderOnPageA() {
setHash('#/page-a');
renderApp();
@@ -957,7 +994,7 @@ describe('App runtime wiring', () => {
window.location.hash = '#install-claude-desktop';
await waitFor(() => expect(window.location.hash).toBe('#install-claude-desktop'));
- replaceHashWithoutNavigation('');
+ clearHashWithoutNavigation();
window.location.hash = '#/page-b';
await waitFor(() => {
@@ -970,11 +1007,11 @@ describe('App runtime wiring', () => {
test('the first Back after the install dialog closes is a deliberate open', async () => {
await renderOnPageA();
- pushHashWithoutNavigation('#/page-b');
+ pushHashWithoutNavigation('#/page-b', 'permanent');
window.location.hash = '#install-claude-desktop';
await waitFor(() => expect(window.location.hash).toBe('#install-claude-desktop'));
- replaceHashWithoutNavigation('');
+ clearHashWithoutNavigation();
window.history.back();
await waitFor(() => {
diff --git a/packages/app/src/App.history-traversal.dom.test.tsx b/packages/app/src/App.history-traversal.dom.test.tsx
index fa6cc85e3..0ec856118 100644
--- a/packages/app/src/App.history-traversal.dom.test.tsx
+++ b/packages/app/src/App.history-traversal.dom.test.tsx
@@ -10,6 +10,7 @@ import {
transitionEditorWorkspace,
} from '@/editor/editor-panes';
import { tabIdForNavigationTarget } from '@/editor/editor-tabs';
+import { previewOpenDisposition } from '@/editor/preview-open-disposition';
import {
hashFromDocName,
hashFromSkillPreview,
@@ -92,21 +93,15 @@ function applyOpen(target: ResolvedNavigationTarget, options: OpenOptions = {})
}
function clickDocRow(docName: string, previewTabsEnabled: boolean) {
- applyOpen(
- { kind: 'doc', target: docName, docName },
- {
- disposition: previewTabsEnabled ? 'preview' : 'permanent',
- consumeActiveNewTab: true,
- },
- );
- pushHashWithoutNavigation(hashFromDocName(docName));
+ const disposition = previewOpenDisposition(previewTabsEnabled);
+ applyOpen({ kind: 'doc', target: docName, docName }, { disposition, consumeActiveNewTab: true });
+ pushHashWithoutNavigation(hashFromDocName(docName), disposition);
}
function clickSkillPreviewRow(preview: SkillPreviewHashTarget, previewTabsEnabled: boolean) {
- applyOpen(previewTarget(preview), {
- tabBehavior: previewTabsEnabled ? 'replace-active' : 'append',
- });
- pushHashWithoutNavigation(hashFromSkillPreview(preview));
+ const disposition = previewOpenDisposition(previewTabsEnabled);
+ applyOpen(previewTarget(preview), { disposition });
+ pushHashWithoutNavigation(hashFromSkillPreview(preview), disposition);
}
let pages = new Set<string>();
diff --git a/packages/app/src/App.tsx b/packages/app/src/App.tsx
index d07245436..750069fed 100644
--- a/packages/app/src/App.tsx
+++ b/packages/app/src/App.tsx
@@ -65,12 +65,13 @@ import { ConfigProvider, useConfigContext } from '@/lib/config-provider';
import { createPageRequest, nextUntitledDocName, openCreatedPage } from '@/lib/create-page-request';
import {
assetPathFromHash,
+ clearHashWithoutNavigation,
docNameFromHash,
hashFromDocName,
isContentRootHash,
isManagedHashHistoryState,
markCurrentHashHistoryEntry,
- replaceHashWithoutNavigation,
+ readManagedHashHistoryEntry,
skillFileFromHash,
skillPreviewFromHash,
skillsFromHash,
@@ -224,7 +225,7 @@ function NavigationHandler() {
window.location.hash !== '' &&
!isAuxiliaryDialogHash(window.location.hash)
) {
- replaceHashWithoutNavigation('');
+ clearHashWithoutNavigation();
}
}, []);
@@ -274,9 +275,10 @@ function NavigationHandler() {
}
openTargetTransition(target, {
deliberate,
- disposition: isHistoryTraversal
- ? previewOpenDisposition(previewTabsEnabled)
- : 'permanent',
+ disposition:
+ isHistoryTraversal || !previewTabsEnabled
+ ? previewOpenDisposition(previewTabsEnabled)
+ : (readManagedHashHistoryEntry(window.history.state)?.disposition ?? 'permanent'),
consumeActiveNewTab: true,
});
};
@@ -417,7 +419,7 @@ function InstallInClaudeDesktopTrigger() {
function handleOpenChange(next: boolean) {
setOpen(next);
if (!next && window.location.hash === INSTALL_DIALOG_HASH) {
- replaceHashWithoutNavigation('');
+ clearHashWithoutNavigation();
}
}
diff --git a/packages/app/src/components/FileTree.preview-tabs.dom.test.tsx b/packages/app/src/components/FileTree.preview-tabs.dom.test.tsx
index e5fd2a557..472a92498 100644
--- a/packages/app/src/components/FileTree.preview-tabs.dom.test.tsx
+++ b/packages/app/src/components/FileTree.preview-tabs.dom.test.tsx
@@ -301,6 +301,7 @@ vi.doMock('@/editor/preview-tab-promotion', () => ({
}));
const { FileTree } = await import('./FileTree');
+const { readManagedHashHistoryEntry } = await import('@/lib/doc-hash');
describe('FileTree preview-tab activation', () => {
let consoleWarnSpy: ReturnType<typeof vi.spyOn>;
@@ -343,6 +344,7 @@ describe('FileTree preview-tab activation', () => {
),
);
expect(window.location.hash).toBe('#/note');
+ expect(readManagedHashHistoryEntry(window.history.state)).toEqual({ disposition });
expect(notifySidebarFileSelectedMock).toHaveBeenCalledTimes(1);
fireEvent.click(screen.getByRole('treeitem', { name: 'docs/' }));
@@ -355,6 +357,7 @@ describe('FileTree preview-tab activation', () => {
),
);
expect(window.location.hash).toBe('#/docs/');
+ expect(readManagedHashHistoryEntry(window.history.state)).toEqual({ disposition });
expect(notifySidebarFileSelectedMock).toHaveBeenCalledTimes(2);
},
);
diff --git a/packages/app/src/components/FileTree.tsx b/packages/app/src/components/FileTree.tsx
index 7f0e2bffe..49eda5b8d 100644
--- a/packages/app/src/components/FileTree.tsx
+++ b/packages/app/src/components/FileTree.tsx
@@ -815,8 +815,9 @@ export function FileTree({ ref }: { ref?: Ref<FileTreeHandle | null> }) {
const showOnlyMarkdownFiles = merged?.appearance?.sidebar?.showOnlyMarkdownFiles ?? false;
const showOkFolders = merged?.appearance?.sidebar?.showOkFolders ?? false;
const previewTabsEnabled = merged?.editor?.previewTabs ?? true;
+ const previewDisposition = previewOpenDisposition(previewTabsEnabled);
const previewOpenOptions = {
- disposition: previewOpenDisposition(previewTabsEnabled),
+ disposition: previewDisposition,
consumeActiveNewTab: true,
} satisfies OpenTargetOptions;
const couldNotReachServerTitle = t`Could not reach server`;
@@ -879,14 +880,14 @@ export function FileTree({ ref }: { ref?: Ref<FileTreeHandle | null> }) {
if (target.registerPage) addPage(target.docName);
const resolved = navigationTargetForDocument(target.docName, target.size);
openTarget(resolved, previewOpenOptions);
- pushHashWithoutNavigation(hashFromDocName(target.docName));
+ pushHashWithoutNavigation(hashFromDocName(target.docName), previewDisposition);
if (resolved.kind === 'doc') editorDocName = resolved.docName;
} else if (target.kind === 'folder') {
openTarget(
{ kind: 'folder', target: target.folderPath, folderPath: target.folderPath },
previewOpenOptions,
);
- pushHashWithoutNavigation(hashFromFolderPath(target.folderPath));
+ pushHashWithoutNavigation(hashFromFolderPath(target.folderPath), previewDisposition);
} else {
const currentEntries = target.entries ?? documentsRef.current;
const entry = currentEntries.find(
@@ -902,7 +903,7 @@ export function FileTree({ ref }: { ref?: Ref<FileTreeHandle | null> }) {
},
previewOpenOptions,
);
- pushHashWithoutNavigation(hashFromAssetPath(target.assetPath));
+ pushHashWithoutNavigation(hashFromAssetPath(target.assetPath), previewDisposition);
}
notifySidebarFileSelected(editorDocName);
}
@@ -928,7 +929,7 @@ export function FileTree({ ref }: { ref?: Ref<FileTreeHandle | null> }) {
},
previewOpenOptions,
);
- pushHashWithoutNavigation(action.hash);
+ pushHashWithoutNavigation(action.hash, previewDisposition);
notifySidebarFileSelected();
return 'non-doc';
}
@@ -945,7 +946,7 @@ export function FileTree({ ref }: { ref?: Ref<FileTreeHandle | null> }) {
});
if (okTarget?.kind === 'asset') {
openTarget(okTarget, previewOpenOptions);
- pushHashWithoutNavigation(hashFromAssetPath(okTarget.assetPath));
+ pushHashWithoutNavigation(hashFromAssetPath(okTarget.assetPath), previewDisposition);
notifySidebarFileSelected();
return 'non-doc';
}
diff --git a/packages/app/src/components/ManagedArtifactProperties.tsx b/packages/app/src/components/ManagedArtifactProperties.tsx
index 91dbcf58c..11c320d87 100644
--- a/packages/app/src/components/ManagedArtifactProperties.tsx
+++ b/packages/app/src/components/ManagedArtifactProperties.tsx
@@ -69,8 +69,8 @@ export function useManagedArtifactRetarget(): (
if (fromDocName === toDocName) return;
const dest = { kind: 'doc', target: toDocName, docName: toDocName } as const;
if (activeDocName === fromDocName || opts?.sourceWasActive === true) {
- openTarget(dest, { tabBehavior: 'replace-active' });
- replaceHashWithoutNavigation(hashFromDocName(toDocName));
+ openTarget(dest, { tabBehavior: 'replace-active', disposition: 'preview' });
+ replaceHashWithoutNavigation(hashFromDocName(toDocName), 'preview');
} else if (openTabs.some((id) => docNameForTabId(id) === fromDocName)) {
openTarget(dest);
closeDocument(fromDocName);
diff --git a/packages/app/src/components/SkillsSidebarSection.tsx b/packages/app/src/components/SkillsSidebarSection.tsx
index 58dbfd55b..27f1d116c 100644
--- a/packages/app/src/components/SkillsSidebarSection.tsx
+++ b/packages/app/src/components/SkillsSidebarSection.tsx
@@ -16,6 +16,7 @@ import { SidebarGroup, SidebarGroupContent } from '@/components/ui/sidebar';
import { Spinner } from '@/components/ui/spinner';
import { useDocumentContext } from '@/editor/DocumentContext';
import { findLocalSkillPreviewTabId } from '@/editor/editor-tabs';
+import { previewOpenDisposition } from '@/editor/preview-open-disposition';
import { useCreateBlankSkill } from '@/hooks/use-create-blank-skill';
import { useOpenSkill } from '@/hooks/use-open-skill';
import { useOpenSkillForEdit } from '@/hooks/use-open-skill-for-edit';
@@ -54,8 +55,8 @@ export function SkillsSidebarSection({ dockExpanded = false }: { dockExpanded?:
const { openTarget, activeDocName, activeTarget, openTabs, activateTab } = useDocumentContext();
const { merged, userBinding, userSynced, projectLocalBinding, projectLocalSynced } =
useConfigContext();
- const tabBehavior: 'append' | 'replace-active' =
- (merged?.editor?.previewTabs ?? true) ? 'replace-active' : 'append';
+ const previewTabsEnabled = merged?.editor?.previewTabs ?? true;
+ const tabDisposition = previewOpenDisposition(previewTabsEnabled);
const showSkillGroups = merged?.appearance?.sidebar?.showSkillGroups ?? true;
const pinnedByScope = {
project: readPins(merged, 'project'),
@@ -189,12 +190,12 @@ export function SkillsSidebarSection({ dockExpanded = false }: { dockExpanded?:
}
const openSkillDoc = (docName: string) => {
- openTarget({ kind: 'doc', target: docName, docName }, { tabBehavior });
- pushHashWithoutNavigation(hashFromDocName(docName));
+ openTarget({ kind: 'doc', target: docName, docName }, { disposition: tabDisposition });
+ pushHashWithoutNavigation(hashFromDocName(docName), tabDisposition);
};
const openSkillMd = (skill: SkillsListEntry) => {
openSkill(skill.scope, skill.name, {
- replaceActive: tabBehavior === 'replace-active',
+ replaceActive: previewTabsEnabled,
...(skill.path ? { path: skill.path } : {}),
...(skill.hostQualifier !== undefined ? { host: skill.hostQualifier } : {}),
});
@@ -210,7 +211,7 @@ export function SkillsSidebarSection({ dockExpanded = false }: { dockExpanded?:
);
if (existing) {
activateTab(existing);
- pushHashWithoutNavigation(hashFromSkillPreview(target));
+ pushHashWithoutNavigation(hashFromSkillPreview(target), tabDisposition);
return;
}
}
@@ -225,9 +226,9 @@ export function SkillsSidebarSection({ dockExpanded = false }: { dockExpanded?:
level: target.level,
path: target.path,
},
- { tabBehavior },
+ { disposition: tabDisposition },
);
- pushHashWithoutNavigation(hashFromSkillPreview(target));
+ pushHashWithoutNavigation(hashFromSkillPreview(target), tabDisposition);
};
const openManaged = (skill: SkillsListEntry) => {
openPreviewReplacing({
@@ -259,7 +260,7 @@ export function SkillsSidebarSection({ dockExpanded = false }: { dockExpanded?:
path: filePath,
...(host ? { host } : {}),
},
- { tabBehavior },
+ { disposition: tabDisposition },
);
pushHashWithoutNavigation(
hashFromSkillFile({
@@ -268,6 +269,7 @@ export function SkillsSidebarSection({ dockExpanded = false }: { dockExpanded?:
path: filePath,
...(host ? { host } : {}),
}),
+ tabDisposition,
);
};
const isPreviewActiveFor = (name: string, scope: SkillScope, path?: string): boolean =>
@@ -321,8 +323,7 @@ export function SkillsSidebarSection({ dockExpanded = false }: { dockExpanded?:
});
return;
}
- const replaceActive = tabBehavior === 'replace-active';
- void openSkillForEdit(s.name, source, { replaceActive, rel }).then((r) => {
+ void openSkillForEdit(s.name, source, { replaceActive: previewTabsEnabled, rel }).then((r) => {
if (!r.ok) toast.error(t`Couldn't open ${s.name} for editing: ${r.error}`);
});
};
diff --git a/packages/app/src/editor/sidebar-drop.ts b/packages/app/src/editor/sidebar-drop.ts
index cd07bdb05..d4940fd36 100644
--- a/packages/app/src/editor/sidebar-drop.ts
+++ b/packages/app/src/editor/sidebar-drop.ts
@@ -14,5 +14,5 @@ export function openSidebarDropPayload(
): void {
const navigation = navigationForSidebarDragPayload(payload);
openTarget(navigation.target, { disposition: 'permanent', consumeActiveNewTab });
- pushHashWithoutNavigation(navigation.hash);
+ pushHashWithoutNavigation(navigation.hash, 'permanent');
}
diff --git a/packages/app/src/hooks/use-open-skill-for-edit.dom.test.tsx b/packages/app/src/hooks/use-open-skill-for-edit.dom.test.tsx
new file mode 100644
index 000000000..58ea304e1
--- /dev/null
+++ b/packages/app/src/hooks/use-open-skill-for-edit.dom.test.tsx
@@ -0,0 +1,43 @@
+import { renderHook } from '@testing-library/react';
+import { afterEach, describe, expect, test, vi } from 'vitest';
+import { readManagedHashHistoryEntry } from '@/lib/doc-hash';
+
+const openTarget = vi.fn();
+vi.doMock('@/editor/DocumentContext', () => ({
+ useDocumentContext: () => ({ openTarget }),
+}));
+
+const editExternalSkill = vi.fn();
+vi.doMock('@/lib/skills-api', () => ({ editExternalSkill }));
+
+const { useOpenSkillForEdit } = await import('./use-open-skill-for-edit');
+
+afterEach(() => {
+ openTarget.mockClear();
+ editExternalSkill.mockReset();
+ window.history.replaceState(null, '', '/');
+});
+
+describe('useOpenSkillForEdit navigation disposition', () => {
+ test('an active-tab replacement keeps its preview disposition in the hash metadata', async () => {
+ window.history.replaceState(null, '', '/app#/old-skill');
+ editExternalSkill.mockResolvedValue({ ok: true, docName: 'external-skill/SKILL' });
+ const { result } = renderHook(() => useOpenSkillForEdit());
+
+ await expect(
+ result.current('external-skill', '/workspace', { replaceActive: true }),
+ ).resolves.toEqual({ ok: true, docName: 'external-skill/SKILL' });
+
+ expect(openTarget).toHaveBeenCalledWith(
+ {
+ kind: 'doc',
+ target: 'external-skill/SKILL',
+ docName: 'external-skill/SKILL',
+ },
+ { tabBehavior: 'replace-active', disposition: 'preview' },
+ );
+ expect(readManagedHashHistoryEntry(window.history.state)).toEqual(
+ expect.objectContaining({ disposition: 'preview' }),
+ );
+ });
+});
diff --git a/packages/app/src/hooks/use-open-skill-for-edit.ts b/packages/app/src/hooks/use-open-skill-for-edit.ts
index c93719ccf..ad0769005 100644
--- a/packages/app/src/hooks/use-open-skill-for-edit.ts
+++ b/packages/app/src/hooks/use-open-skill-for-edit.ts
@@ -13,11 +13,12 @@ export function useOpenSkillForEdit(): (
const res = await editExternalSkill({ name, home });
if (!res.ok) return res;
const docName = opts?.rel ? externalSkillFileLiveDocName(name, opts.rel) : res.docName;
+ const disposition = opts?.replaceActive ? 'preview' : 'permanent';
openTarget(
{ kind: 'doc', target: docName, docName },
- opts?.replaceActive ? { tabBehavior: 'replace-active' } : undefined,
+ opts?.replaceActive ? { tabBehavior: 'replace-active', disposition } : { disposition },
);
- replaceHashWithoutNavigation(hashFromDocName(docName));
+ replaceHashWithoutNavigation(hashFromDocName(docName), disposition);
return { ok: true, docName };
};
}
diff --git a/packages/app/src/hooks/use-open-skill.dom.test.tsx b/packages/app/src/hooks/use-open-skill.dom.test.tsx
index 59e0f79d3..a31847f76 100644
--- a/packages/app/src/hooks/use-open-skill.dom.test.tsx
+++ b/packages/app/src/hooks/use-open-skill.dom.test.tsx
@@ -51,7 +51,7 @@ describe('useOpenSkill gitignored gate', () => {
await vi.waitFor(() => {
expect(openTarget).toHaveBeenCalledWith(
expect.objectContaining({ docName: '.claude/skills/hidden/SKILL' }),
- undefined,
+ { disposition: 'permanent' },
);
});
expect(requestSkillTrackPrompt).not.toHaveBeenCalled();
@@ -74,7 +74,7 @@ describe('useOpenSkill gitignored gate', () => {
expect(requestSkillTrackPrompt).not.toHaveBeenCalled();
expect(openTarget).toHaveBeenCalledWith(
expect.objectContaining({ docName: '.claude/skills/hidden/SKILL' }),
- undefined,
+ { disposition: 'permanent' },
);
});
});
@@ -109,7 +109,7 @@ describe('useOpenSkill symlinked-bundle routing', () => {
expect(openTarget).toHaveBeenCalledWith(
expect.objectContaining({ docName: '.claude/skills/hidden/SKILL' }),
- undefined,
+ { disposition: 'permanent' },
);
});
diff --git a/packages/app/src/hooks/use-open-skill.ts b/packages/app/src/hooks/use-open-skill.ts
index 0b3122d36..51d7a1f42 100644
--- a/packages/app/src/hooks/use-open-skill.ts
+++ b/packages/app/src/hooks/use-open-skill.ts
@@ -3,6 +3,7 @@ import type { SkillScope } from '@inkeep/open-knowledge-core/schemas/api';
import { t } from '@lingui/core/macro';
import { toast } from 'sonner';
import { useDocumentContext } from '@/editor/DocumentContext';
+import type { TabOpenDisposition } from '@/editor/editor-panes';
import { useSkills, whenSkillsListContains } from '@/hooks/use-skills';
import {
hashFromDocName,
@@ -36,13 +37,11 @@ export function useOpenSkill(): (
beginSkillWrite(scope, name);
void whenSkillsListContains(scope, name).then(() => endSkillWrite(scope, name));
}
- openTarget(
- { kind: 'doc', target: docName, docName },
- opts?.replaceActive ? { tabBehavior: 'replace-active' } : undefined,
- );
+ const disposition: TabOpenDisposition = opts?.replaceActive ? 'preview' : 'permanent';
+ openTarget({ kind: 'doc', target: docName, docName }, { disposition });
const hash = hashFromDocName(docName);
- if (opts?.replaceHistory) replaceHashWithoutNavigation(hash);
- else pushHashWithoutNavigation(hash);
+ if (opts?.replaceHistory) replaceHashWithoutNavigation(hash, disposition);
+ else pushHashWithoutNavigation(hash, disposition);
};
const entry =
skillsState.status === 'ready'
diff --git a/packages/app/src/hooks/use-rename-skill.dom.test.tsx b/packages/app/src/hooks/use-rename-skill.dom.test.tsx
index 34b025bba..81815a137 100644
--- a/packages/app/src/hooks/use-rename-skill.dom.test.tsx
+++ b/packages/app/src/hooks/use-rename-skill.dom.test.tsx
@@ -1,5 +1,6 @@
import { renderHook, waitFor } from '@testing-library/react';
import { describe, expect, test, vi } from 'vitest';
+import { readManagedHashHistoryEntry } from '@/lib/doc-hash';
const openTarget = vi.fn();
const closeDocument = vi.fn();
@@ -91,4 +92,25 @@ describe('useRenameSkill busy-mark bracketing', () => {
});
expect(whenSkillsListContains).toHaveBeenCalledWith('project', 'new-name');
});
+
+ test('retargeting the active skill keeps the replacement navigation preview', async () => {
+ openTarget.mockReset();
+ window.history.replaceState(null, '', '/app#/old-name');
+ moveSkill.mockResolvedValue({ ok: true, committed: false, to: '.claude/skills/new-name' });
+ const { result } = renderHook(() => useRenameSkill());
+
+ await result.current({ scope: 'project', name: 'old-name' }, 'new-name');
+
+ expect(openTarget).toHaveBeenCalledWith(
+ {
+ kind: 'doc',
+ target: '.claude/skills/new-name/SKILL',
+ docName: '.claude/skills/new-name/SKILL',
+ },
+ { tabBehavior: 'replace-active', disposition: 'preview' },
+ );
+ expect(readManagedHashHistoryEntry(window.history.state)).toEqual(
+ expect.objectContaining({ disposition: 'preview' }),
+ );
+ });
});
diff --git a/packages/app/src/lib/doc-hash.test.ts b/packages/app/src/lib/doc-hash.test.ts
index daa8e625d..01ede9755 100644
--- a/packages/app/src/lib/doc-hash.test.ts
+++ b/packages/app/src/lib/doc-hash.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, test, vi } from 'vitest';
import {
anchorFromHash,
assetPathFromHash,
+ clearHashWithoutNavigation,
docNameFromHash,
encodeShareTargetForHash,
encodeSkillPreviewSegments,
@@ -17,7 +18,7 @@ import {
isSameHash,
markCurrentHashHistoryEntry,
pushHashWithoutNavigation,
- replaceHashWithoutNavigation,
+ readManagedHashHistoryEntry,
type SkillFileHashTarget,
type SkillPreviewHashTarget,
selectedPathForSkillPreview,
@@ -324,7 +325,7 @@ describe('pushHashWithoutNavigation', () => {
test('pushes a distinct hash while preserving the current path and query', () => {
const { pushState, restore } = installWindow('#/old');
try {
- pushHashWithoutNavigation('#/new');
+ pushHashWithoutNavigation('#/new', 'permanent');
} finally {
restore();
}
@@ -338,10 +339,44 @@ describe('pushHashWithoutNavigation', () => {
expect(isManagedHashHistoryState(pushState.mock.calls[0]?.[0])).toBe(true);
});
+ test('stores the disposition while preserving unrelated state', () => {
+ const { pushState, restore } = installWindow('#/old');
+ try {
+ pushHashWithoutNavigation('#/preview', 'preview');
+ const firstState = pushState.mock.calls[0]?.[0];
+ Object.defineProperty(window.history, 'state', {
+ configurable: true,
+ value: firstState,
+ });
+ Object.defineProperty(window.location, 'hash', {
+ configurable: true,
+ value: '#/preview',
+ });
+ pushHashWithoutNavigation('#/permanent', 'permanent');
+ const firstEntry = readManagedHashHistoryEntry(firstState);
+ const secondEntry = readManagedHashHistoryEntry(pushState.mock.calls[1]?.[0]);
+ expect(firstEntry?.disposition).toBe('preview');
+ expect(secondEntry?.disposition).toBe('permanent');
+ expect(pushState.mock.calls[1]?.[0]).toEqual(expect.objectContaining({ preserved: 'value' }));
+ } finally {
+ restore();
+ }
+ });
+
+ test('ignores incomplete managed metadata', () => {
+ expect(readManagedHashHistoryEntry({ __okHashHistoryEntry: true })).toBeNull();
+ expect(
+ readManagedHashHistoryEntry({
+ __okHashHistoryEntry: true,
+ __okHashHistoryDisposition: 'other',
+ }),
+ ).toBeNull();
+ });
+
test('does not push when the hash is already current', () => {
const { pushState, restore } = installWindow('#/current');
try {
- pushHashWithoutNavigation('#/current');
+ pushHashWithoutNavigation('#/current', 'permanent');
} finally {
restore();
}
@@ -381,10 +416,10 @@ describe('pushHashWithoutNavigation', () => {
expect(replaceState).not.toHaveBeenCalled();
});
- test('replaces the current hash while preserving managed history state', () => {
+ test('clears the current hash while preserving managed history state', () => {
const { replaceState, restore } = installWindow('#/current');
try {
- replaceHashWithoutNavigation('');
+ clearHashWithoutNavigation();
} finally {
restore();
}
diff --git a/packages/app/src/lib/doc-hash.ts b/packages/app/src/lib/doc-hash.ts
index 9bdb04e08..db3ad389c 100644
--- a/packages/app/src/lib/doc-hash.ts
+++ b/packages/app/src/lib/doc-hash.ts
@@ -1,5 +1,6 @@
import { MANAGED_ARTIFACT_SCOPES } from '@inkeep/open-knowledge-core/constants/cc1';
import type { SkillScope } from '@inkeep/open-knowledge-core/schemas/api';
+import type { TabOpenDisposition } from '@/editor/editor-panes';
function isSkillScope(value: string): value is SkillScope {
return (MANAGED_ARTIFACT_SCOPES as readonly string[]).includes(value);
@@ -66,10 +67,24 @@ function decodeHashForComparison(hash: string): string {
}
const MANAGED_HASH_HISTORY_STATE_KEY = '__okHashHistoryEntry';
+const MANAGED_HASH_HISTORY_DISPOSITION_KEY = '__okHashHistoryDisposition';
-function managedHashHistoryState(state: unknown): Record<string, unknown> {
+export interface ManagedHashHistoryEntry {
+ disposition: TabOpenDisposition;
+}
+
+function managedHashHistoryState(
+ state: unknown,
+ entry?: ManagedHashHistoryEntry,
+): Record<string, unknown> {
const preservedState = typeof state === 'object' && state !== null ? state : {};
- return { ...preservedState, [MANAGED_HASH_HISTORY_STATE_KEY]: true };
+ const unmanagedState = { ...(preservedState as Record<string, unknown>) };
+ delete unmanagedState[MANAGED_HASH_HISTORY_DISPOSITION_KEY];
+ return {
+ ...unmanagedState,
+ [MANAGED_HASH_HISTORY_STATE_KEY]: true,
+ ...(entry ? { [MANAGED_HASH_HISTORY_DISPOSITION_KEY]: entry.disposition } : {}),
+ };
}
export function isManagedHashHistoryState(state: unknown): boolean {
@@ -80,25 +95,40 @@ export function isManagedHashHistoryState(state: unknown): boolean {
);
}
+export function readManagedHashHistoryEntry(state: unknown): ManagedHashHistoryEntry | null {
+ if (!isManagedHashHistoryState(state)) return null;
+ const disposition = (state as Record<string, unknown>)[MANAGED_HASH_HISTORY_DISPOSITION_KEY];
+ if (disposition !== 'preview' && disposition !== 'permanent') return null;
+ return { disposition };
+}
+
export function markCurrentHashHistoryEntry(): void {
if (isManagedHashHistoryState(window.history.state)) return;
window.history.replaceState(managedHashHistoryState(window.history.state), '');
}
-export function replaceHashWithoutNavigation(hash: string): void {
+function replaceManagedHashEntry(hash: string, entry?: ManagedHashHistoryEntry): void {
const { pathname, search } = window.location;
window.history.replaceState(
- managedHashHistoryState(window.history.state),
+ managedHashHistoryState(window.history.state, entry),
'',
`${pathname}${search}${hash}`,
);
}
-export function pushHashWithoutNavigation(hash: string): void {
+export function replaceHashWithoutNavigation(hash: string, disposition: TabOpenDisposition): void {
+ replaceManagedHashEntry(hash, { disposition });
+}
+
+export function clearHashWithoutNavigation(): void {
+ replaceManagedHashEntry('');
+}
+
+export function pushHashWithoutNavigation(hash: string, disposition: TabOpenDisposition): void {
if (isSameHash(window.location.hash, hash)) return;
const { pathname, search } = window.location;
window.history.pushState(
- managedHashHistoryState(window.history.state),
+ managedHashHistoryState(window.history.state, { disposition }),
'',
`${pathname}${search}${hash}`,
);