feat: real recursive folder tree in the sidebar

The Folders section in the LeftSidebar previously rendered the flat
list of source roots — actual subdirectories were invisible. Now it
shows the full nested tree, click any node to filter, drop targets
work at every depth.

Backend
- New GET /folders/tree returning a list of root nodes (one per
  active SourceRoot). Each node is { id, name, path, photo_count,
  children: [...] } with children sorted alphabetically at every
  level. Walks Folder rows whose source_root_id matches and whose
  path is at or beneath the source root, then attaches them by
  parent path so partial scans don't break the tree.
- The source root's display label is overlaid on the root folder
  node so the top-level entry reads as "Library" instead of
  "/photos".
- list_photos folder_id filter now does descendant matching: when
  a Folder id is given, it includes the folder itself and every
  Folder whose path is a sep-prefixed descendant. Matches the
  Lightroom mental model: clicking "Library" or any parent folder
  shows everything beneath it. The existing source-root-id branch
  is unchanged.

Frontend
- New types/api.ts FolderTreeNode interface and sourceFolders.tree()
  helper.
- New hooks/useFolderTreeQuery.ts with a 30s staleTime and a
  findFolderInTree() walker for id-based name lookups.
- LeftSidebar drops the flat foldersData list and uses the tree
  query. folderNodeToTreeItem recursively maps backend nodes into
  the existing TreeItem shape; renderTreeItem already knew how to
  recurse into children, so the tree just works at any depth.
  Drop targets, drag-to-move, drag-to-copy, double-click rename,
  and active-state highlighting all carry over to nested folders.
- The renameMutation now also invalidates ['folders', 'tree'] so a
  source-root rename refreshes the tree label immediately.
- ActiveFilterChips switches to the tree query and uses the new
  findFolderInTree walker so the chip label resolves correctly for
  sub-folder filters too — not just top-level source roots.
- The "Scan all folders" button visibility now keys off the tree
  length instead of the flat folders length.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-08 11:36:01 +02:00
parent bb7c2b12d6
commit 914eb58ac5
6 changed files with 169 additions and 33 deletions

View File

@@ -71,15 +71,17 @@ async def list_photos(
if date_to:
filters.append(Photo.taken_at <= date_to)
# Folder filter — the sidebar exposes "source roots" (top-level scan
# paths) under the same UI affordance as folders, so the same param has
# to accept either a folder id or a source root id. If the value matches
# a source root, expand to every folder under that root and use IN.
# Folder filter. The sidebar can pass either a SourceRoot id or a
# Folder id; both should include descendants so clicking a parent
# folder shows everything under it (Lightroom semantics).
if folder_id:
sr_check = await db.execute(
select(SourceRoot.id).where(SourceRoot.id == folder_id)
select(SourceRoot).where(SourceRoot.id == folder_id)
)
if sr_check.scalar_one_or_none() is not None:
sr_row = sr_check.scalar_one_or_none()
if sr_row is not None:
# Source root → all folders under it (any depth).
child_folders = await db.execute(
select(Folder.id).where(Folder.source_root_id == folder_id)
)
@@ -87,11 +89,25 @@ async def list_photos(
if child_ids:
filters.append(Photo.folder_id.in_(child_ids))
else:
# Source root with no folder rows yet — match nothing rather
# than returning the entire library.
filters.append(Photo.id == '__no_match__')
else:
filters.append(Photo.folder_id == folder_id)
# Folder id → that folder + every descendant by path prefix.
target_check = await db.execute(
select(Folder).where(Folder.id == folder_id)
)
target = target_check.scalar_one_or_none()
if target is None:
filters.append(Photo.id == '__no_match__')
else:
target_path = os.path.normpath(target.path).rstrip(os.sep)
desc_result = await db.execute(
select(Folder.id).where(
(Folder.path == target_path)
| (Folder.path.like(target_path + os.sep + '%'))
)
)
desc_ids = [row[0] for row in desc_result.all()]
filters.append(Photo.folder_id.in_(desc_ids))
# Media type filter
if media_type: