count-in-tree.js 765 B

12345678910111213141516171819202122232425262728293031323334353637
  1. export function countFiles(fileTreeData) {
  2. if (!fileTreeData) {
  3. return 0
  4. }
  5. const files = filesInFolder(fileTreeData)
  6. // count all the non-deleted entities
  7. const value = files.filter(item => !item.deleted).length
  8. const limit = window.ExposedSettings.maxEntitiesPerProject
  9. const status = fileCountStatus(value, limit, Math.ceil(limit / 20))
  10. return { value, status, limit }
  11. }
  12. function filesInFolder({ docs, folders, fileRefs }) {
  13. const files = [...docs, ...fileRefs]
  14. for (const folder of folders) {
  15. files.push(...filesInFolder(folder))
  16. }
  17. return files
  18. }
  19. function fileCountStatus(value, limit, range) {
  20. if (value >= limit) {
  21. return 'error'
  22. }
  23. if (value >= limit - range) {
  24. return 'warning'
  25. }
  26. return 'success'
  27. }