Files
windmill/frontend/src/lib/components/breadcrumbPath.ts
T
Guilhem LemouelandClaude Opus 5 f2768de00c fix: keep the folders a half-typed path already names
The trailing-separator guard gave up on `f/demo/` entirely, which dropped the
whole trail to the kind chip and brought `f/demo` back on the first character of
the name. The levels the path does name come back now; only the leaf waits.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-01 00:27:21 -07:00

31 lines
1.3 KiB
TypeScript

/** A directory level of an item's path, with the full path down to it. */
export type PathDir = { name: string; fullPath: string }
/**
* Splits `f/demo/weather_report` into the levels a breadcrumb walks: the scope (`f/<folder>` or
* `u/<user>`), then each subfolder, then the item itself. Each level carries the cumulative path,
* which is what the picker opens and highlights.
*
* Returns undefined before there is even a scope. The leaf is undefined while the path names a
* folder but no item yet (`f/demo/`) — the levels it does name still come back.
*/
export function splitItemPath(
path: string
): { dirs: PathDir[]; leaf: PathDir | undefined } | undefined {
const parts = path.split('/')
if (parts.length < 3) return undefined
const scope = parts.slice(0, 2).join('/')
const slug = parts.slice(2)
const dirs: PathDir[] = [{ name: scope, fullPath: scope }]
let acc = scope
for (let i = 0; i < slug.length - 1; i++) {
acc = `${acc}/${slug[i]}`
dirs.push({ name: slug[i], fullPath: acc })
}
// A leaf with an empty name draws a blank segment. The editor binds the path live, so the
// breadcrumb sees every keystroke: the folders stay put while the name is being typed rather
// than the whole trail dropping out and coming back.
const name = slug[slug.length - 1]
return { dirs, leaf: name ? { name, fullPath: path } : undefined }
}