overleaf/services/web/frontend/js/features/file-tree/util/count-in-tree.js
Timothée Alby 24bd557485 Merge pull request #6210 from overleaf/ta-file-tree-data-in-context
Remove rootFolder from EditorContext

GitOrigin-RevId: 827b046046265ad2418eb91d5a69eae02aa8b5a4
2022-01-11 09:04:56 +00:00

37 lines
765 B
JavaScript

export function countFiles(fileTreeData) {
if (!fileTreeData) {
return 0
}
const files = filesInFolder(fileTreeData)
// count all the non-deleted entities
const value = files.filter(item => !item.deleted).length
const limit = window.ExposedSettings.maxEntitiesPerProject
const status = fileCountStatus(value, limit, Math.ceil(limit / 20))
return { value, status, limit }
}
function filesInFolder({ docs, folders, fileRefs }) {
const files = [...docs, ...fileRefs]
for (const folder of folders) {
files.push(...filesInFolder(folder))
}
return files
}
function fileCountStatus(value, limit, range) {
if (value >= limit) {
return 'error'
}
if (value >= limit - range) {
return 'warning'
}
return 'success'
}