|
1 | 1 | import type { ElementType } from 'react' |
| 2 | +import { folderAncestorChain } from '@/lib/folders/tree' |
2 | 3 | import type { |
3 | 4 | BreadcrumbEditing, |
4 | 5 | BreadcrumbItem, |
5 | 6 | DropdownOption, |
6 | 7 | } from '@/app/workspace/[workspaceId]/components/resource/components/resource-header' |
7 | | -import type { WorkflowFolder } from '@/stores/folders/types' |
8 | 8 |
|
9 | | -export interface FolderBreadcrumbItemsOptions { |
| 9 | +/** |
| 10 | + * The only thing a breadcrumb trail needs from a folder tree node. Structural rather than |
| 11 | + * `WorkflowFolder` so the Files tree builds its trail through this same code path instead of |
| 12 | + * forking one: file folders live in the same `folder` table, but they are served by their own |
| 13 | + * routes and carry their own row type (see the `servedFolderResourceTypeSchema` docs in |
| 14 | + * `@/lib/api/contracts/folders` for why that split exists and is staying). |
| 15 | + */ |
| 16 | +export interface BreadcrumbFolder { |
| 17 | + id: string |
| 18 | + name: string |
| 19 | + parentId: string | null |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Root-first ancestor chain for a breadcrumb trail, that folder last. Empty at the workspace |
| 24 | + * root — and empty for a chain that does not reach the root, where {@link folderAncestorChain} |
| 25 | + * would hand back the part it walked. |
| 26 | + * |
| 27 | + * A header falls back to the root title rather than rendering a trail that silently skips a |
| 28 | + * level: a partial path is not a shorter path, it is a wrong one, claiming the deepest folder |
| 29 | + * it resolved sits at the workspace root. A chain is complete exactly when its first element |
| 30 | + * has no parent, which also rejects a cycle (the DB permits one between constraint checks). |
| 31 | + * |
| 32 | + * `folderById` must therefore be the complete tree — see `FolderAncestors.foldersResolved`; |
| 33 | + * a partially loaded map reads as an orphan and collapses. |
| 34 | + */ |
| 35 | +export function breadcrumbFolderChain<T extends BreadcrumbFolder>( |
| 36 | + folderId: string | null | undefined, |
| 37 | + folderById: ReadonlyMap<string, T> |
| 38 | +): T[] { |
| 39 | + const chain = folderAncestorChain(folderId, (id) => folderById.get(id)) |
| 40 | + return chain.length === 0 || chain[0].parentId === null ? chain : EMPTY_CHAIN |
| 41 | +} |
| 42 | + |
| 43 | +const EMPTY_CHAIN: never[] = [] |
| 44 | + |
| 45 | +interface FolderBreadcrumbItemsBase { |
10 | 46 | /** Root crumb label — the page's own name ("Knowledge Base", "Tables"). */ |
11 | 47 | rootLabel: string |
12 | 48 | rootIcon?: ElementType |
13 | | - /** Root-first ancestor chain of the open folder, from `useFolderNavigation`. */ |
14 | | - breadcrumbs: WorkflowFolder[] |
| 49 | + /** Root-first ancestor chain, from {@link folderAncestorChain}. */ |
| 50 | + breadcrumbs: BreadcrumbFolder[] |
15 | 51 | /** Called with the folder to open, or `null` for the workspace root. */ |
16 | 52 | onNavigate: (folderId: string | null) => void |
| 53 | +} |
| 54 | + |
| 55 | +/** A list page: the deepest folder is where you are, so its crumb carries the rename and menu. */ |
| 56 | +interface FolderListBreadcrumbOptions extends FolderBreadcrumbItemsBase { |
17 | 57 | /** Menu attached to the open folder's crumb (rename, delete, …). */ |
18 | 58 | currentFolderActions?: DropdownOption[] |
19 | 59 | /** Inline rename bound to the open folder's crumb. */ |
20 | 60 | currentFolderEditing?: BreadcrumbEditing |
| 61 | + trailing?: never |
21 | 62 | } |
22 | 63 |
|
| 64 | +/** A detail page: the open resource is where you are, so every folder crumb navigates. */ |
| 65 | +interface FolderDetailBreadcrumbOptions extends FolderBreadcrumbItemsBase { |
| 66 | + /** |
| 67 | + * Crumbs appended after the folder trail — the resource open on a detail page, plus |
| 68 | + * anything nested under it (a knowledge base's document, that document's chunk). |
| 69 | + */ |
| 70 | + trailing: BreadcrumbItem[] |
| 71 | + currentFolderActions?: never |
| 72 | + currentFolderEditing?: never |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * The two modes are disjoint by construction rather than by convention: an open-folder rename |
| 77 | + * or menu acts on the folder you are inside, which on a detail page you are not. Expressed as |
| 78 | + * a union so passing both is a compile error instead of a handler that silently never fires. |
| 79 | + */ |
| 80 | +export type FolderBreadcrumbItemsOptions = |
| 81 | + | FolderListBreadcrumbOptions |
| 82 | + | FolderDetailBreadcrumbOptions |
| 83 | + |
| 84 | +const NO_TRAILING_CRUMBS: BreadcrumbItem[] = [] |
| 85 | + |
23 | 86 | /** |
24 | 87 | * Converts a folder ancestor chain into the `BreadcrumbItem[]` that `Resource.Header` |
25 | | - * renders. |
| 88 | + * renders, for a list page (`Tables / Reports`) or a detail page (`Tables / Reports / Q3`). |
26 | 89 | * |
27 | 90 | * A plain builder rather than a component: `Resource.Header` already owns every piece of |
28 | 91 | * breadcrumb chrome — the root-crumb "Path" popover, segment width allocation, overflow |
29 | 92 | * tooltips, and the rule that a single-element trail renders as a plain page title. A |
30 | 93 | * sibling crumb component would have to fork all of it, which is exactly what this shared |
31 | 94 | * directory exists to prevent. |
32 | 95 | * |
33 | | - * The trail always starts with the root crumb, so at the workspace root the result has |
| 96 | + * The trail always starts with the root crumb, so a list page at the workspace root returns |
34 | 97 | * length 1 and the header renders the page title unchanged. |
35 | 98 | */ |
36 | | -export function folderBreadcrumbItems({ |
37 | | - rootLabel, |
38 | | - rootIcon, |
39 | | - breadcrumbs, |
40 | | - onNavigate, |
41 | | - currentFolderActions, |
42 | | - currentFolderEditing, |
43 | | -}: FolderBreadcrumbItemsOptions): BreadcrumbItem[] { |
| 99 | +export function folderBreadcrumbItems(options: FolderBreadcrumbItemsOptions): BreadcrumbItem[] { |
| 100 | + const { rootLabel, rootIcon, breadcrumbs, onNavigate } = options |
| 101 | + const trailing = options.trailing ?? NO_TRAILING_CRUMBS |
| 102 | + |
44 | 103 | const items: BreadcrumbItem[] = [ |
45 | 104 | { label: rootLabel, icon: rootIcon, onClick: () => onNavigate(null) }, |
46 | 105 | ] |
47 | 106 |
|
48 | 107 | breadcrumbs.forEach((folder, index) => { |
49 | | - const isCurrent = index === breadcrumbs.length - 1 |
50 | | - /** The open folder is where you already are, so its crumb is not a navigation target. */ |
| 108 | + /** Where you already are — and on a detail page that is a trailing crumb, not a folder. */ |
| 109 | + const isOpenFolder = trailing.length === 0 && index === breadcrumbs.length - 1 |
51 | 110 | items.push({ |
52 | 111 | label: folder.name, |
53 | | - onClick: isCurrent ? undefined : () => onNavigate(folder.id), |
54 | | - dropdownItems: isCurrent && currentFolderActions?.length ? currentFolderActions : undefined, |
55 | | - editing: isCurrent ? currentFolderEditing : undefined, |
| 112 | + onClick: isOpenFolder ? undefined : () => onNavigate(folder.id), |
| 113 | + dropdownItems: |
| 114 | + isOpenFolder && options.currentFolderActions?.length |
| 115 | + ? options.currentFolderActions |
| 116 | + : undefined, |
| 117 | + editing: isOpenFolder ? options.currentFolderEditing : undefined, |
56 | 118 | }) |
57 | 119 | }) |
58 | 120 |
|
| 121 | + items.push(...trailing) |
| 122 | + |
59 | 123 | return items |
60 | 124 | } |
0 commit comments