fix(library): preserve folder creation order

Keep unrelated folder selections in the order users add them, and make API reads deterministic so web and iOS defaults use the same first folder.

Fixes #1412
This commit is contained in:
neon
2026-09-18 15:58:18 +00:00
parent d22faeaaf4
commit 6cbf8f5c35
6 changed files with 78 additions and 20 deletions
@@ -11,6 +11,10 @@ describe('folder path selection', () => {
expect(consolidateFolderPaths(['/books/fiction', '/audio', '/books', '/books/fantasy'])).toEqual(['/audio', '/books'])
})
it('preserves the order of unrelated folders', () => {
expect(consolidateFolderPaths(['/library/zz', '/library/alpha-long', '/library/m'])).toEqual(['/library/zz', '/library/alpha-long', '/library/m'])
})
it('finds the selected path that already covers a nested folder', () => {
expect(coveringFolderPath('/books/fiction/scifi', ['/books', '/books/fiction'])).toBe('/books/fiction')
})
@@ -15,8 +15,8 @@ export function coveringFolderPath(path: string, candidates: string[]): string |
}
export function consolidateFolderPaths(paths: string[]): string[] {
const unique = [...new Set(paths.map(normalizeFolderPath))].sort((a, b) => a.length - b.length || a.localeCompare(b))
return unique.filter((path, index) => !unique.slice(0, index).some((candidate) => isPathInside(path, candidate)))
const unique = [...new Set(paths.map(normalizeFolderPath))]
return unique.filter((path) => !unique.some((candidate) => candidate !== path && isPathInside(path, candidate)))
}
function isPathInside(path: string, parent: string): boolean {