count-in-tree.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import getMeta from '@/utils/meta'
  2. import { Folder } from '../../../../../types/folder'
  3. type FileCountStatus = 'success' | 'warning' | 'error'
  4. type FileCount = {
  5. value: number
  6. status: FileCountStatus
  7. limit: number
  8. }
  9. export function countFiles(fileTreeData: Folder | undefined): 0 | FileCount {
  10. if (!fileTreeData) {
  11. return 0
  12. }
  13. const value = _countElements(fileTreeData)
  14. const limit = getMeta('ol-ExposedSettings').maxEntitiesPerProject
  15. const status = fileCountStatus(value, limit, Math.ceil(limit / 20))
  16. return { value, status, limit }
  17. }
  18. function fileCountStatus(
  19. value: number,
  20. limit: number,
  21. range: number
  22. ): FileCountStatus {
  23. if (value >= limit) {
  24. return 'error'
  25. }
  26. if (value >= limit - range) {
  27. return 'warning'
  28. }
  29. return 'success'
  30. }
  31. // Copied and adapted from ProjectEntityMongoUpdateHandler
  32. function _countElements(rootFolder: Folder): number {
  33. function countFolder(folder: Folder) {
  34. if (folder == null) {
  35. return 0
  36. }
  37. let total = 0
  38. if (folder.folders) {
  39. total += folder.folders.length
  40. for (const subfolder of folder.folders) {
  41. total += countFolder(subfolder)
  42. }
  43. }
  44. if (folder.docs) {
  45. total += folder.docs.length
  46. }
  47. if (folder.fileRefs) {
  48. total += folder.fileRefs.length
  49. }
  50. return total
  51. }
  52. return countFolder(rootFolder)
  53. }