From ec83e9b8395445028a9fd2439019b555d2d9fcc5 Mon Sep 17 00:00:00 2001 From: lucaronin Date: Thu, 30 Apr 2026 00:18:33 +0200 Subject: [PATCH] feat: show vault root in folder tree --- docs/ABSTRACTIONS.md | 6 +- docs/ARCHITECTURE.md | 2 +- src/App.tsx | 2 +- src/components/FolderTree.test.tsx | 92 +++++++++ src/components/FolderTree.tsx | 167 ++++++++++++---- src/components/Sidebar.tsx | 178 +++++++++++------- .../folder-tree/FolderContextMenu.tsx | 43 +++-- src/components/folder-tree/FolderItemRow.tsx | 2 +- src/components/folder-tree/FolderTreeRow.tsx | 14 +- .../folder-tree/useFolderTreeDisclosure.ts | 5 +- src/hooks/commands/navigationCommands.ts | 8 +- src/hooks/folder-actions/useFolderDelete.ts | 2 +- src/hooks/folder-actions/useFolderRename.ts | 2 +- src/hooks/useCommandRegistry.test.ts | 42 +++-- src/types.ts | 2 +- src/utils/noteListHelpers.test.ts | 13 ++ src/utils/noteListHelpers.ts | 21 ++- 17 files changed, 442 insertions(+), 159 deletions(-) diff --git a/docs/ABSTRACTIONS.md b/docs/ABSTRACTIONS.md index 32660724..64bc6523 100644 --- a/docs/ABSTRACTIONS.md +++ b/docs/ABSTRACTIONS.md @@ -301,14 +301,14 @@ type SidebarFilter = 'all' | 'archived' | 'changes' | 'pulse' type SidebarSelection = | { kind: 'filter'; filter: SidebarFilter } | { kind: 'sectionGroup'; type: string } // e.g. type: 'Project' - | { kind: 'folder'; path: string } + | { kind: 'folder'; path: string; rootPath?: string } | { kind: 'entity'; entry: VaultEntry } // Neighborhood source note | { kind: 'view'; filename: string } ``` `SidebarSelection.kind === 'folder'` is a first-class navigation target, not just a visual highlight. -- `FolderTree` keeps the folder interaction surface decomposed into `FolderTreeRow`, `FolderNameInput`, `FolderContextMenu`, and disclosure/context-menu hooks so nested row rendering, inline rename, and right-click actions stay isolated. Non-mutating reveal/copy-path menu items stay callback-driven from `App` so filesystem convenience actions do not leak into folder mutation hooks. +- `FolderTree` keeps the folder interaction surface decomposed into `FolderTreeRow`, `FolderNameInput`, `FolderContextMenu`, and disclosure/context-menu hooks so nested row rendering, inline rename, and right-click actions stay isolated. The UI wraps backend folder nodes in a synthetic vault-root row with `path: ""` and `rootPath` set to the opened vault so root-level files can be listed without turning the vault root into a mutable folder. Non-mutating reveal/copy-path menu items stay callback-driven from `App` so filesystem convenience actions do not leak into folder mutation hooks. - `useFolderActions()` composes `useFolderRename()` and `useFolderDelete()` to keep folder mutations selection-aware while the rest of `App.tsx` only wires the resulting callbacks into `Sidebar` and the command registry. - `useNoteRetargeting()` is the shared retargeting abstraction for note drops and command-palette actions. It owns the "can drop here?" checks, updates `type:` via frontmatter when a note lands on a type section, and delegates folder moves through the same crash-safe rename pipeline used by the backend rename commands. - A successful folder rename reloads the folder tree plus vault entries, rewrites any affected folder-scoped tabs, and updates `SidebarSelection` to the new relative path when the renamed folder stays selected. @@ -352,7 +352,7 @@ The renderer uses `viewOrdering` helpers to convert drag or command-palette move 5. Sorts by `modified_at` descending 6. Skips unparseable files with a warning log -The folder tree hides the legacy `type/` directory, since those type documents already appear through the Types sidebar section. Default vault folders such as `attachments/` and `views/` remain visible alongside user-created folders. +The folder tree hides the legacy `type/` directory, since those type documents already appear through the Types sidebar section. Default vault folders such as `attachments/` and `views/` remain visible alongside user-created folders under the synthetic vault-root row. Command-facing vault content is filtered through `vault::filter_gitignored_entries`, `vault::filter_gitignored_folders`, and `vault::filter_gitignored_paths` when the app setting `hide_gitignored_files` is enabled. The cache still stores the complete scan; `list_vault`, `reload_vault`, `list_vault_folders`, and search apply the visibility filter at the boundary before React consumes entries. The filter batches paths through `git check-ignore --no-index --stdin`, so negated and specific `.gitignore` patterns follow Git semantics as closely as the app can reasonably support. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 72ccb5f4..e15d2bf4 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -201,7 +201,7 @@ flowchart TD └──────────────────────────────────────────────────────────────┘ ``` -- **Sidebar** (150-400px, resizable): Top-level filters (All Notes, Changes, Pulse), saved Views, collapsible type-based section groups, and a dedicated folder tree. The folder tree shows user-created folders plus default vault folders such as `attachments/` and `views/`; only the dedicated `type/` directory stays hidden because note types already have their own sidebar section. Saved Views persist a top-level YAML `order` field in each view file and use the same ordered-list mental model as Types: pointer users can drag the existing view row, while keyboard users can use command-palette move actions. The folder tree supports inline folder creation and rename, exposes a right-click menu for rename/delete plus filesystem reveal/copy-path actions, and auto-expands ancestor folders when the current selection or rename target is nested. Type sections and folder rows also act as note drop targets: dropping a note on a type updates its `type:` frontmatter, while dropping it on a folder runs the same crash-safe move path as the command palette flow. Each type can have a custom icon, color, sort, and visibility set via its `type: Type` document; new type documents created by Tolaria are written at the vault root. +- **Sidebar** (150-400px, resizable): Top-level filters (All Notes, Changes, Pulse), saved Views, collapsible type-based section groups, and a dedicated folder tree. The folder tree starts with a vault-root row labeled from the opened vault path, shows root-level files when selected, and nests user-created folders plus default vault folders such as `attachments/` and `views/` underneath it; only the dedicated `type/` directory stays hidden because note types already have their own sidebar section. Saved Views persist a top-level YAML `order` field in each view file and use the same ordered-list mental model as Types: pointer users can drag the existing view row, while keyboard users can use command-palette move actions. The folder tree supports inline folder creation and rename, exposes a right-click menu for rename/delete plus filesystem reveal/copy-path actions on mutable folders, and auto-expands ancestor folders when the current selection or rename target is nested. Type sections and folder rows also act as note drop targets: dropping a note on a type updates its `type:` frontmatter, while dropping it on a folder runs the same crash-safe move path as the command palette flow. Each type can have a custom icon, color, sort, and visibility set via its `type: Type` document; new type documents created by Tolaria are written at the vault root. - **Note List / Pulse View** (200-500px, resizable): When a section group, filter, or saved view is selected, shows filtered notes with snippets, modified dates, status indicators, and per-context note-list controls. When `selection.kind === 'entity'`, the same pane enters **Neighborhood** mode: the source note is pinned at the top as a normal active row, outgoing relationship groups render first, inverse/backlink groups follow, empty groups stay visible with `0`, and duplicates across groups are allowed when multiple relationships are true. Plain click / `Enter` open the focused note without replacing the current Neighborhood, while Cmd/Ctrl-click and Cmd/Ctrl-`Enter` pivot the pane into the clicked note's Neighborhood. Folder-backed lists also show non-Markdown files: previewable image and PDF binaries get file indicators and open in the editor pane, while unsupported binaries remain muted instead of auto-launching an external app. Saved views reuse the same sort and visible-column controls as the built-in lists, and those changes persist back into the view `.yml` definition (`sort`, `listPropertiesDisplay`). When Pulse filter is active, shows `PulseView` — a chronological git activity feed grouped by day. - **Editor** (flex, fills remaining space): Single note open at a time (no tabs — see ADR-0003). Breadcrumb bar with word count and rich-editor width toggle, BlockNote rich text editor with wikilink support, Markdown-compatible inline/display math rendering, first-class Mermaid diagram blocks, markdown-safe formatting controls, and schema-backed fenced code block highlighting via `@blocknote/code-block`. Can toggle to diff view (modified files), raw CodeMirror view, or a wide rich-editor reading surface with preserved side margins; raw CodeMirror remains full-width and unaffected by note width mode. Binary image and PDF files render through `FilePreview` as ordinary vault files using Tauri asset URLs; external-open actions call `open_vault_file_external` so the target is validated against the active vault before the native default app opens it. Unsupported/broken binaries show explicit fallback states and keyboard focus returns to the note list on `Escape`. Decomposed into `Editor` (orchestrator), `EditorContent`, `FilePreview`, `EditorRightPanel`, `SingleEditorView`, with hooks `useDiffMode`, `useEditorFocus`, and `useEditorSave`, plus the `useRawMode`/`RawEditorView` pair for markdown source editing. Rich BlockNote input and raw CodeMirror input both route typed `->`, `<-`, and `<->` through the shared `src/utils/arrowLigatures.ts` resolver so arrow ligatures stay consistent across mode switches while escaped ASCII sequences remain literal. Navigation history (Cmd+[/]) replaces tabs. - **Inspector / AI Agent** (200-500px or 40px collapsed): Toggles between Inspector (frontmatter, relationships, instances, backlinks, git history) and AI Agent panel (the selected CLI agent with tool execution). The Sparkle icon in the breadcrumb bar toggles between them. Per-note `icon` is a suggested Inspector property and the command palette's "Set Note Icon" action opens that field directly. When viewing a Type note, the Inspector shows an **Instances** section listing all notes of that type (sorted by modified_at desc, capped at 50). diff --git a/src/App.tsx b/src/App.tsx index 221d59d1..9a50276d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1622,7 +1622,7 @@ function App() { {sidebarVisible && ( <>
- +
diff --git a/src/components/FolderTree.test.tsx b/src/components/FolderTree.test.tsx index e95cd06a..2b94c07d 100644 --- a/src/components/FolderTree.test.tsx +++ b/src/components/FolderTree.test.tsx @@ -19,6 +19,7 @@ const mockFolders: FolderNode[] = [ ] const defaultSelection: SidebarSelection = { kind: 'filter', filter: 'all' } +const vaultRootPath = '/Users/luca/Laputa' describe('FolderTree', () => { it('renders nothing when folders is empty', () => { @@ -36,6 +37,53 @@ describe('FolderTree', () => { expect(screen.getByText('journal')).toBeInTheDocument() }) + it('renders the vault root as the top-level folder when a vault path is available', () => { + render( + , + ) + + expect(screen.getByText('Laputa')).toBeInTheDocument() + expect(screen.getByRole('button', { name: 'Laputa' })).toBeInTheDocument() + expect(screen.getByText('projects')).toBeInTheDocument() + expect(screen.getByText('areas')).toBeInTheDocument() + expect(screen.getByText('journal')).toBeInTheDocument() + }) + + it('renders the vault root even when the vault has no subfolders', () => { + render( + , + ) + + expect(screen.getByText('Laputa')).toBeInTheDocument() + }) + + it('lets the vault root collapse and expand its nested folders', () => { + render( + , + ) + + fireEvent.click(screen.getByLabelText('Collapse Laputa')) + expect(screen.queryByText('projects')).not.toBeInTheDocument() + + fireEvent.click(screen.getByLabelText('Expand Laputa')) + expect(screen.getByText('projects')).toBeInTheDocument() + }) + it('expands children when clicking the folder chevron', () => { render() expect(screen.queryByText('laputa')).not.toBeInTheDocument() @@ -51,6 +99,21 @@ describe('FolderTree', () => { expect(onSelect).toHaveBeenCalledWith({ kind: 'folder', path: 'projects' }) }) + it('selects the vault root with the root path attached', () => { + const onSelect = vi.fn() + render( + , + ) + + fireEvent.click(screen.getByTestId('folder-row:')) + expect(onSelect).toHaveBeenCalledWith({ kind: 'folder', path: '', rootPath: vaultRootPath }) + }) + it('expands children when single-clicking a folder row with children', () => { vi.useFakeTimers() function FolderTreeHarness() { @@ -248,4 +311,33 @@ describe('FolderTree', () => { fireEvent.click(screen.getByTestId('copy-folder-path-menu-item')) expect(onCopyFolderPath).toHaveBeenCalledWith('projects') }) + + it('keeps destructive folder actions off the vault root row and menu', () => { + render( + , + ) + + expect(screen.queryByTestId('rename-folder-btn:')).not.toBeInTheDocument() + expect(screen.queryByTestId('delete-folder-btn:')).not.toBeInTheDocument() + + fireEvent.contextMenu(screen.getByText('Laputa')) + + expect(screen.getByTestId('reveal-folder-menu-item')).toBeInTheDocument() + expect(screen.getByTestId('copy-folder-path-menu-item')).toBeInTheDocument() + expect(screen.queryByText('Rename folder...')).not.toBeInTheDocument() + expect(screen.queryByTestId('delete-folder-menu-item')).not.toBeInTheDocument() + }) }) diff --git a/src/components/FolderTree.tsx b/src/components/FolderTree.tsx index 5bd63e24..fff3ea1c 100644 --- a/src/components/FolderTree.tsx +++ b/src/components/FolderTree.tsx @@ -1,6 +1,8 @@ import { memo, + type MouseEvent as ReactMouseEvent, useCallback, + useMemo, } from 'react' import { Plus, @@ -30,6 +32,53 @@ interface FolderTreeProps { collapsed?: boolean locale?: AppLocale onToggle?: () => void + vaultRootPath?: string +} + +interface FolderTreeBodyProps extends Pick< + FolderTreeProps, + | 'locale' + | 'onCancelRenameFolder' + | 'onDeleteFolder' + | 'onRenameFolder' + | 'onSelect' + | 'onStartRenameFolder' + | 'renamingFolderPath' + | 'selection' +> { + displayedExpanded: Record + displayedFolders: FolderNode[] + isCreating: boolean + onCancelCreateFolder: () => void + onCreateFolderSubmit: (value: string) => Promise + rootPath?: string + sectionCollapsed: boolean + toggleFolder: (path: string) => void + onOpenMenu: (node: FolderNode, event: ReactMouseEvent) => void +} + +function vaultRootLabel(vaultRootPath: string, locale: AppLocale): string { + const trimmed = vaultRootPath.trim().replace(/[\\/]+$/g, '') + return trimmed.split(/[\\/]/).filter(Boolean).pop() || translate(locale, 'status.vault.default') +} + +function buildRootNode(folders: FolderNode[], vaultRootPath: string | undefined, locale: AppLocale): FolderNode | null { + if (!vaultRootPath?.trim()) return null + return { + name: vaultRootLabel(vaultRootPath, locale), + path: '', + children: folders, + } +} + +function useDisplayedFolders(folders: FolderNode[], expanded: Record, vaultRootPath: string | undefined, locale: AppLocale) { + return useMemo(() => { + const rootNode = buildRootNode(folders, vaultRootPath, locale) + return { + displayedExpanded: rootNode ? { '': true, ...expanded } : expanded, + displayedFolders: rootNode ? [rootNode] : folders, + } + }, [expanded, folders, locale, vaultRootPath]) } export const FolderTree = memo(function FolderTree({ @@ -46,6 +95,7 @@ export const FolderTree = memo(function FolderTree({ collapsed: externalCollapsed, locale = 'en', onToggle, + vaultRootPath, }: FolderTreeProps) { const { closeCreateForm, @@ -93,7 +143,9 @@ export const FolderTree = memo(function FolderTree({ openCreateForm() }, [closeContextMenu, openCreateForm]) - if (folders.length === 0 && !isCreating) return null + const { displayedExpanded, displayedFolders } = useDisplayedFolders(folders, expanded, vaultRootPath, locale) + + if (displayedFolders.length === 0 && !isCreating) return null return (
@@ -102,41 +154,25 @@ export const FolderTree = memo(function FolderTree({ )} - {!sectionCollapsed && ( -
- {folders.map((node) => ( - - ))} - {isCreating && ( -
- -
- )} -
- )} + + {displayedFolders.map((node) => ( + + ))} + {isCreating && ( +
+ +
+ )} +
+ ) +} + function CreateFolderButton({ locale, onCreate, diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 925a35ae..7f14c478 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -57,6 +57,7 @@ interface SidebarProps { renamingFolderPath?: string | null onStartRenameFolder?: (folderPath: string) => void onCancelRenameFolder?: () => void + vaultRootPath?: string showInbox?: boolean inboxCount?: number locale?: AppLocale @@ -84,6 +85,7 @@ interface SidebarNavigationProps extends Pick< | 'renamingFolderPath' | 'onStartRenameFolder' | 'onCancelRenameFolder' + | 'vaultRootPath' | 'showInbox' | 'inboxCount' | 'onCreateNewType' @@ -166,6 +168,7 @@ type SidebarFoldersNavigationProps = Pick< | 'renamingFolderPath' | 'onStartRenameFolder' | 'onCancelRenameFolder' + | 'vaultRootPath' | 'groupCollapsed' | 'toggleGroup' | 'locale' @@ -313,6 +316,7 @@ function SidebarFoldersNavigation({ renamingFolderPath, onStartRenameFolder, onCancelRenameFolder, + vaultRootPath, groupCollapsed, toggleGroup, locale, @@ -343,6 +347,7 @@ function SidebarFoldersNavigation({ collapsed={groupCollapsed.folders} locale={locale} onToggle={() => toggleGroup('folders')} + vaultRootPath={vaultRootPath} /> ) } @@ -366,6 +371,7 @@ function SidebarNavigation({ renamingFolderPath, onStartRenameFolder, onCancelRenameFolder, + vaultRootPath, showInbox = true, inboxCount = 0, locale = 'en', @@ -458,6 +464,7 @@ function SidebarNavigation({ renamingFolderPath={renamingFolderPath} onStartRenameFolder={onStartRenameFolder} onCancelRenameFolder={onCancelRenameFolder} + vaultRootPath={vaultRootPath} groupCollapsed={groupCollapsed} toggleGroup={toggleGroup} locale={locale} @@ -473,7 +480,7 @@ function useSidebarDndSensors() { ) } -export const Sidebar = memo(function Sidebar({ +function useSidebarRuntime({ entries, selection, onSelect, @@ -482,27 +489,7 @@ export const Sidebar = memo(function Sidebar({ onReorderSections, onRenameSection, onToggleTypeVisibility, - onSelectFavorite, - onReorderFavorites, - views = [], - onCreateView, - onEditView, - onDeleteView, - onReorderViews, - folders = [], - onCreateFolder, - onRenameFolder, - onDeleteFolder, - folderFileActions, - renamingFolderPath, - onStartRenameFolder, - onCancelRenameFolder, - showInbox = true, - inboxCount = 0, locale = 'en', - onCollapse, - onCreateNewType, - loading = false, }: SidebarProps) { const { typeEntryMap, allSectionGroups, visibleSections, sectionIds } = useSidebarSections(entries) const { activeCount, archivedCount } = useEntryCounts(entries) @@ -526,7 +513,6 @@ export const Sidebar = memo(function Sidebar({ const reordered = computeReorder(sectionIds, active.id as string, over.id as string) if (reordered) onReorderSections?.(reordered.map((typeName, order) => ({ typeName, order }))) }, [sectionIds, onReorderSections]) - const viewActions = { onCreateView, onEditView, onDeleteView, onReorderViews } const sectionProps: SidebarSectionProps = { entries, @@ -540,60 +526,112 @@ export const Sidebar = memo(function Sidebar({ onRenameCancel: typeInteractions.cancelRename, } + return { + activeCount, + allSectionGroups, + archivedCount, + groupCollapsed, + handleDragEnd, + isSectionVisible, + sectionIds, + sectionProps, + sensors, + toggleGroup, + toggleVisibility, + typeEntryMap, + typeInteractions, + visibleSections, + } +} + +function SidebarRuntimeNavigation({ + props, + runtime, +}: { + props: SidebarProps + runtime: ReturnType +}) { return ( -