Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(app): preserve project-root create rename focus (#2611)
Dimitri POSTOLOVfaf3e2d9a6f1
diff --git a/.changeset/root-create-rename-focus.md b/.changeset/root-create-rename-focus.mdnew file mode 100644index 000000000..38c223f61--- /dev/null+++ b/.changeset/root-create-rename-focus.md@@ -0,0 +1,5 @@+---+'@inkeep/open-knowledge': patch+---++`New file`, `New folder`, and template creation from sidebar menus now keep the inline name field focused, so you can rename the item instead of immediately committing its default name.diff --git a/packages/app/src/components/FileSidebar.tsx b/packages/app/src/components/FileSidebar.tsxindex 01d6eb257..c558f43e5 100644--- a/packages/app/src/components/FileSidebar.tsx+++ b/packages/app/src/components/FileSidebar.tsx@@ -12,7 +12,14 @@ import {SquarePen,UnfoldVertical,} from 'lucide-react';-import { type ComponentProps, type FC, type MouseEventHandler, useEffect, useState } from 'react';+import {+ type ComponentProps,+ type FC,+ type MouseEventHandler,+ useEffect,+ useRef,+ useState,+} from 'react';import { ErrorBoundary } from 'react-error-boundary';import { toast } from 'sonner';import { ConflictsSection } from '@/components/ConflictsSection';@@ -360,6 +367,13 @@ function FileSidebarInner({ onOpenSearch }: FileSidebarProps) {const showExpandAll = hasFolders && !allExpanded;const showCollapseAll = hasFolders && !noneExpanded;const showTreeStateSection = showExpandAll || showCollapseAll;+ const suppressCreateMenuFocusRestoreRef = useRef(false);+ const handleCreateMenuCloseAutoFocus = (event: Event) => {+ if (!suppressCreateMenuFocusRestoreRef.current) return;+ suppressCreateMenuFocusRestoreRef.current = false;+ // Radix would otherwise steal focus back from the inline rename input.+ event.preventDefault();+ };// Sidebar-wide context-menu surface. Right-click anywhere inside the// sidebar except interactive controls (toolbar buttons, search-pill button,@@ -384,14 +398,17 @@ function FileSidebarInner({ onOpenSearch }: FileSidebarProps) {// see one set of stable identities across renders.const handleEmptySpaceCreateFile = () => {if (!workspace) return;+ suppressCreateMenuFocusRestoreRef.current = true;tree?.startCreating('file', '');};const handleEmptySpaceSelectTemplate = (templateName: string) => {if (!workspace) return;+ suppressCreateMenuFocusRestoreRef.current = true;tree?.createFromTemplate('', templateName);};const handleEmptySpaceCreateFolder = () => {if (!workspace) return;+ suppressCreateMenuFocusRestoreRef.current = true;tree?.startCreating('folder', '');};const handleEmptySpaceReveal = () => {@@ -932,12 +949,17 @@ function FileSidebarInner({ onOpenSearch }: FileSidebarProps) {// template runs the same inline-rename create flow as New file.<DropdownMenu><ToolbarDropdownTrigger icon={FilePlus} label={t`New from template`} />- <DropdownMenuContent align="end" className="min-w-52">+ <DropdownMenuContent+ align="end"+ className="min-w-52"+ onCloseAutoFocus={handleCreateMenuCloseAutoFocus}+ ><TemplateMenuRowsparentDir={initialCreateDir}- onSelectTemplate={(templateName) =>- tree?.createFromTemplate(initialCreateDir, templateName)- }+ onSelectTemplate={(templateName) => {+ suppressCreateMenuFocusRestoreRef.current = true;+ tree?.createFromTemplate(initialCreateDir, templateName);+ }}ItemComponent={DropdownMenuItem}/></DropdownMenuContent>@@ -1130,7 +1152,7 @@ function FileSidebarInner({ onOpenSearch }: FileSidebarProps) {/></div></ContextMenuTrigger>- <ContextMenuContent className="min-w-52">+ <ContextMenuContent className="min-w-52" onCloseAutoFocus={handleCreateMenuCloseAutoFocus}>{/** Empty-space menu — 4 sections.*diff --git a/packages/app/tests/stress/file-tree-create.e2e.ts b/packages/app/tests/stress/file-tree-create.e2e.tsindex 0f8521523..d1010521d 100644--- a/packages/app/tests/stress/file-tree-create.e2e.ts+++ b/packages/app/tests/stress/file-tree-create.e2e.ts@@ -33,6 +33,30 @@ async function createFolder(baseURL: string, path: string): Promise<void> {throw new Error(`create-folder failed for ${path}: ${res.status} ${await res.text()}`);}+async function putRootTemplate(baseURL: string, name: string, title: string): Promise<void> {+ const res = await fetch(`${baseURL}/api/template`, {+ method: 'PUT',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({+ folder: '',+ name,+ frontmatter: { title },+ body: 'Template body',+ }),+ });++ if (res.ok) return;+ throw new Error(`PUT /api/template failed for ${name}: ${res.status} ${await res.text()}`);+}++async function deleteRootTemplate(baseURL: string, name: string): Promise<void> {+ const query = new URLSearchParams({ folder: '', name });+ const res = await fetch(`${baseURL}/api/template?${query}`, { method: 'DELETE' });++ if (res.ok || res.status === 404) return;+ throw new Error(`DELETE /api/template failed for ${name}: ${res.status} ${await res.text()}`);+}+async function clearVisibleContentEntries(baseURL: string, contentDir: string): Promise<void> {for (const entry of readdirSync(contentDir, { withFileTypes: true })) {if (entry.name.startsWith('.')) continue;@@ -299,6 +323,136 @@ async function installDelayedDesktopSessionBridge(test.describe('FileTree sidebar create', () => {test.describe.configure({ mode: 'serial' });+ for (const scenario of [+ {+ kind: 'file' as const,+ menuItemName: 'New file',+ defaultName: 'Untitled',+ renameInputName: /rename Untitled\.md/i,+ renamedPath: 'zz-root-menu-file',+ renamedTreeItem: 'zz-root-menu-file.md',+ },+ {+ kind: 'folder' as const,+ menuItemName: 'New folder',+ defaultName: 'New Folder',+ renameInputName: /rename New Folder/i,+ renamedPath: 'zz-root-menu-folder',+ renamedTreeItem: 'zz-root-menu-folder',+ },+ ]) {+ test(`project-root context menu keeps new ${scenario.kind} rename focused`, async ({+ page,+ workerServer,+ api,+ }) => {+ await deletePathIfExists(workerServer.baseURL, scenario.kind, scenario.defaultName);+ await deletePathIfExists(workerServer.baseURL, scenario.kind, scenario.renamedPath);++ try {+ await gotoRootAndAwaitSidebar(page);++ const projectRoot = page.locator('[data-sidebar-root-context]');+ await expect(projectRoot).toBeVisible();+ await projectRoot.click({ button: 'right' });+ const contextMenu = page.locator('[data-slot="context-menu-content"]');+ await expect(contextMenu).toBeVisible();+ await page.getByRole('menuitem', { name: scenario.menuItemName, exact: true }).click();++ const renameInput = page.getByRole('textbox', { name: scenario.renameInputName });+ await expect(renameInput).toBeVisible({ timeout: 10_000 });+ await expect(contextMenu).toHaveCount(0);+ await expect(renameInput).toBeFocused();++ await page.keyboard.type(scenario.renamedPath);+ await page.keyboard.press('Enter');++ await expect(sidebarTreeItem(page, scenario.renamedTreeItem)).toBeVisible({+ timeout: 10_000,+ });+ await expect+ .poll(() =>+ existsSync(+ join(+ workerServer.contentDir,+ scenario.kind === 'file' ? `${scenario.renamedPath}.md` : scenario.renamedPath,+ ),+ ),+ )+ .toBe(true);+ } finally {+ await deletePathIfExists(workerServer.baseURL, scenario.kind, scenario.defaultName);+ await deletePathIfExists(workerServer.baseURL, scenario.kind, scenario.renamedPath);+ await restoreRequiredFixtureEntries(workerServer.baseURL, api);+ }+ });+ }++ for (const scenario of [+ {+ surface: 'project-root context menu',+ templateName: 'zz-root-menu-focus-template',+ templateTitle: 'Root menu focus template',+ renamedPath: 'zz-root-menu-template-file',+ menuSlot: 'context-menu-content',+ selectTemplate: async (page: Page, templateTitle: string) => {+ const projectRoot = page.locator('[data-sidebar-root-context]');+ await expect(projectRoot).toBeVisible();+ await projectRoot.click({ button: 'right' });+ await page.getByRole('menuitem', { name: 'New from template', exact: true }).hover();+ await page.getByRole('menuitem', { name: templateTitle, exact: true }).click();+ },+ },+ {+ surface: 'toolbar template menu',+ templateName: 'zz-toolbar-focus-template',+ templateTitle: 'Toolbar focus template',+ renamedPath: 'zz-toolbar-template-file',+ menuSlot: 'dropdown-menu-content',+ selectTemplate: async (page: Page, templateTitle: string) => {+ const trigger = page.getByRole('button', { name: 'New from template', exact: true });+ await expect(trigger).toBeVisible({ timeout: 15_000 });+ await trigger.click();+ await page.getByRole('menuitem', { name: templateTitle, exact: true }).click();+ },+ },+ ]) {+ test(`${scenario.surface} keeps template rename focused`, async ({+ page,+ workerServer,+ api,+ }) => {+ await deletePathIfExists(workerServer.baseURL, 'file', 'Untitled');+ await deletePathIfExists(workerServer.baseURL, 'file', scenario.renamedPath);+ await putRootTemplate(workerServer.baseURL, scenario.templateName, scenario.templateTitle);++ try {+ await gotoRootAndAwaitSidebar(page);+ await scenario.selectTemplate(page, scenario.templateTitle);++ const renameInput = page.getByRole('textbox', { name: /rename Untitled\.md/i });+ await expect(renameInput).toBeVisible({ timeout: 10_000 });+ await expect(page.locator(`[data-slot="${scenario.menuSlot}"]`)).toHaveCount(0);+ await expect(renameInput).toBeFocused();++ await page.keyboard.type(scenario.renamedPath);+ await page.keyboard.press('Enter');++ await expect(sidebarTreeItem(page, `${scenario.renamedPath}.md`)).toBeVisible({+ timeout: 10_000,+ });+ await expect+ .poll(() => existsSync(join(workerServer.contentDir, `${scenario.renamedPath}.md`)))+ .toBe(true);+ } finally {+ await deletePathIfExists(workerServer.baseURL, 'file', 'Untitled');+ await deletePathIfExists(workerServer.baseURL, 'file', scenario.renamedPath);+ await deleteRootTemplate(workerServer.baseURL, scenario.templateName);+ await restoreRequiredFixtureEntries(workerServer.baseURL, api);+ }+ });+ }+test('desktop refresh preserves restored tabs while hash navigation is opening', async ({page,workerServer,