auto-select-file.ts 3.3 KB

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