overleaf/services/web/frontend/js/features/file-tree/util/count-in-tree.js
June Kelly c07aa9638b Merge pull request #8258 from overleaf/jk-clean-up-too-many-files-errors
[web] Count folders toward entity limit on frontend

GitOrigin-RevId: ef07a61639f0dc516c83e4ed0f2fdc35e8b6d3fa
2022-06-09 08:02:18 +00:00

50 lines
1 KiB
JavaScript

export function countFiles(fileTreeData) {
if (!fileTreeData) {
return 0
}
const value = _countElements(fileTreeData)
const limit = window.ExposedSettings.maxEntitiesPerProject
const status = fileCountStatus(value, limit, Math.ceil(limit / 20))
return { value, status, limit }
}
function fileCountStatus(value, limit, range) {
if (value >= limit) {
return 'error'
}
if (value >= limit - range) {
return 'warning'
}
return 'success'
}
// Copied and adapted from ProjectEntityMongoUpdateHandler
function _countElements(rootFolder) {
function countFolder(folder) {
if (folder == null) {
return 0
}
let total = 0
if (folder.folders) {
total += folder.folders.length
for (const subfolder of folder.folders) {
total += countFolder(subfolder)
}
}
if (folder.docs) {
total += folder.docs.length
}
if (folder.fileRefs) {
total += folder.fileRefs.length
}
return total
}
return countFolder(rootFolder)
}