auto-select-file.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import _ from 'lodash'
  2. import { getUpdateForVersion } from './history-details'
  3. import type { Nullable } from '../../../../../types/utils'
  4. import type { FileDiff } from '../services/types/file'
  5. import type { FileOperation } from '../services/types/file-operation'
  6. import type { LoadedUpdate, Version } from '../services/types/update'
  7. type FileWithOps = {
  8. pathname: FileDiff['pathname']
  9. operation: FileOperation
  10. }
  11. function getFilesWithOps(
  12. files: FileDiff[],
  13. toV: Version,
  14. comparing: boolean,
  15. updates: LoadedUpdate[]
  16. ): FileWithOps[] {
  17. if (toV && !comparing) {
  18. const filesWithOps: FileWithOps[] = []
  19. const currentUpdate = getUpdateForVersion(toV, updates)
  20. if (currentUpdate) {
  21. for (const pathname of currentUpdate.pathnames) {
  22. filesWithOps.push({
  23. pathname,
  24. operation: 'edited',
  25. })
  26. }
  27. for (const op of currentUpdate.project_ops) {
  28. let fileWithOps: Nullable<FileWithOps> = null
  29. if (op.add) {
  30. fileWithOps = {
  31. pathname: op.add.pathname,
  32. operation: 'added',
  33. }
  34. } else if (op.remove) {
  35. fileWithOps = {
  36. pathname: op.remove.pathname,
  37. operation: 'removed',
  38. }
  39. } else if (op.rename) {
  40. fileWithOps = {
  41. pathname: op.rename.newPathname,
  42. operation: 'renamed',
  43. }
  44. }
  45. if (fileWithOps !== null) {
  46. filesWithOps.push(fileWithOps)
  47. }
  48. }
  49. }
  50. return filesWithOps
  51. } else {
  52. const filesWithOps = _.reduce(
  53. files,
  54. (curFilesWithOps, file) => {
  55. if ('operation' in file) {
  56. curFilesWithOps.push({
  57. pathname: file.pathname,
  58. operation: file.operation,
  59. })
  60. }
  61. return curFilesWithOps
  62. },
  63. <FileWithOps[]>[]
  64. )
  65. return filesWithOps
  66. }
  67. }
  68. const orderedOpTypes: FileOperation[] = [
  69. 'edited',
  70. 'added',
  71. 'renamed',
  72. 'removed',
  73. ]
  74. export function autoSelectFile(
  75. files: FileDiff[],
  76. toV: Version,
  77. comparing: boolean,
  78. updates: LoadedUpdate[]
  79. ): FileDiff {
  80. let fileToSelect: Nullable<FileDiff> = null
  81. const filesWithOps = getFilesWithOps(files, toV, comparing, updates)
  82. for (const opType of orderedOpTypes) {
  83. const fileWithMatchingOpType = _.find(filesWithOps, {
  84. operation: opType,
  85. })
  86. if (fileWithMatchingOpType != null) {
  87. fileToSelect =
  88. _.find(files, {
  89. pathname: fileWithMatchingOpType.pathname,
  90. }) ?? null
  91. break
  92. }
  93. }
  94. if (!fileToSelect) {
  95. const mainFile = _.find(files, function (file) {
  96. return /main\.tex$/.test(file.pathname)
  97. })
  98. if (mainFile) {
  99. fileToSelect = mainFile
  100. } else {
  101. const anyTeXFile = _.find(files, function (file) {
  102. return /\.tex$/.test(file.pathname)
  103. })
  104. if (anyTeXFile) {
  105. fileToSelect = anyTeXFile
  106. } else {
  107. fileToSelect = files[0]
  108. }
  109. }
  110. }
  111. return fileToSelect
  112. }