overleaf/services/web/frontend/js/features/file-tree/util/is-name-unique-in-folder.ts
David d7357b4d62 Merge pull request #19400 from overleaf/dp-duplicate-file-folder-name
Improvements to handling of file/folder upload conflicts

GitOrigin-RevId: 526edf30dfbaec7ee1e03ffd156365f09be25e86
2024-07-25 08:05:08 +00:00

42 lines
1 KiB
TypeScript

import { findInTree } from '../util/find-in-tree'
import { Folder } from '../../../../../types/folder'
import { Doc } from '../../../../../types/doc'
import { FileRef } from '../../../../../types/file-ref'
export function isNameUniqueInFolder(
tree: Folder,
parentFolderId: string,
name: string
): boolean {
return !(
findFileByNameInFolder(tree, parentFolderId, name) ||
findFolderByNameInFolder(tree, parentFolderId, name)
)
}
export function findFileByNameInFolder(
tree: Folder,
parentFolderId: string,
name: string
): Doc | FileRef | undefined {
if (tree._id !== parentFolderId) {
tree = findInTree(tree, parentFolderId).entity
}
return (
tree.docs.find(entity => entity.name === name) ||
tree.fileRefs.find(entity => entity.name === name)
)
}
export function findFolderByNameInFolder(
tree: Folder,
parentFolderId: string,
name: string
): Folder | undefined {
if (tree._id !== parentFolderId) {
tree = findInTree(tree, parentFolderId).entity
}
return tree.folders.find(entity => entity.name === name)
}