diff --git a/services/web/frontend/js/features/history/components/file-tree/history-file-tree-folder-list.tsx b/services/web/frontend/js/features/history/components/file-tree/history-file-tree-folder-list.tsx index 50f3981cb3..5711629943 100644 --- a/services/web/frontend/js/features/history/components/file-tree/history-file-tree-folder-list.tsx +++ b/services/web/frontend/js/features/history/components/file-tree/history-file-tree-folder-list.tsx @@ -6,6 +6,7 @@ import { ReactNode, useCallback } from 'react' import type { HistoryFileTree, HistoryDoc } from '../../utils/file-tree' import { useHistoryContext } from '../../context/history-context' import { FileDiff } from '../../services/types/file' +import { fileFinalPathname } from '../../utils/file-diff' type HistoryFileTreeFolderListProps = { folders: HistoryFileTree[] @@ -69,7 +70,10 @@ function HistoryFileTreeFolderList({ key={doc.pathname} name={doc.name} file={doc} - selected={selection.selectedFile?.pathname === doc.pathname} + selected={ + !!selection.selectedFile && + fileFinalPathname(selection.selectedFile) === doc.pathname + } onClick={handleClick} onKeyDown={handleKeyDown} /> diff --git a/services/web/frontend/js/features/history/utils/auto-select-file.ts b/services/web/frontend/js/features/history/utils/auto-select-file.ts index fa7fda2ee0..54c65e25cf 100644 --- a/services/web/frontend/js/features/history/utils/auto-select-file.ts +++ b/services/web/frontend/js/features/history/utils/auto-select-file.ts @@ -4,6 +4,7 @@ import type { Nullable } from '../../../../../types/utils' import type { FileDiff } from '../services/types/file' import type { FileOperation } from '../services/types/file-operation' import type { LoadedUpdate, Version } from '../services/types/update' +import { fileFinalPathname } from './file-diff' type FileWithOps = { pathname: FileDiff['pathname'] @@ -97,9 +98,10 @@ export function autoSelectFile( if (fileWithMatchingOpType != null) { fileToSelect = - _.find(files, { - pathname: fileWithMatchingOpType.pathname, - }) ?? null + _.find( + files, + file => fileFinalPathname(file) === fileWithMatchingOpType.pathname + ) ?? null break } diff --git a/services/web/frontend/js/features/history/utils/file-diff.ts b/services/web/frontend/js/features/history/utils/file-diff.ts index d70f9a3b8f..072cceb3af 100644 --- a/services/web/frontend/js/features/history/utils/file-diff.ts +++ b/services/web/frontend/js/features/history/utils/file-diff.ts @@ -7,3 +7,7 @@ export function isFileRenamed(fileDiff: FileDiff): fileDiff is FileRenamed { export function isFileRemoved(fileDiff: FileDiff): fileDiff is FileRemoved { return (fileDiff as FileRemoved).operation === 'removed' } + +export function fileFinalPathname(fileDiff: FileDiff) { + return isFileRenamed(fileDiff) ? fileDiff.newPathname : fileDiff.pathname +} diff --git a/services/web/test/frontend/features/history/utils/auto-select-file.test.ts b/services/web/test/frontend/features/history/utils/auto-select-file.test.ts index a58f5324d1..bb53e91dea 100644 --- a/services/web/test/frontend/features/history/utils/auto-select-file.test.ts +++ b/services/web/test/frontend/features/history/utils/auto-select-file.test.ts @@ -3,6 +3,7 @@ import type { FileDiff } from '../../../../../frontend/js/features/history/servi import { autoSelectFile } from '../../../../../frontend/js/features/history/utils/auto-select-file' import type { User } from '../../../../../frontend/js/features/history/services/types/shared' import { LoadedUpdate } from '../../../../../frontend/js/features/history/services/types/update' +import { fileFinalPathname } from '../../../../../frontend/js/features/history/utils/file-diff' describe('autoSelectFile', function () { const historyUsers: User[] = [ @@ -718,5 +719,72 @@ describe('autoSelectFile', function () { expect(pathname).to.equal('certainly_not_main.tex') }) + + it('selects renamed file', function () { + const files: FileDiff[] = [ + { + pathname: 'main.tex', + }, + { + pathname: 'original.bib', + newPathname: 'new.bib', + operation: 'renamed', + }, + ] + + const updates: LoadedUpdate[] = [ + { + fromV: 4, + toV: 7, + meta: { + users: historyUsers, + start_ts: 1680874742389, + end_ts: 1680874755552, + }, + labels: [], + pathnames: [], + project_ops: [ + { + rename: { + pathname: 'original.bib', + newPathname: 'new.bib', + }, + atV: 5, + }, + ], + }, + { + fromV: 0, + toV: 4, + meta: { + users: historyUsers, + start_ts: 1680861975947, + end_ts: 1680861988442, + }, + labels: [], + pathnames: [], + project_ops: [ + { + add: { + pathname: 'original.bib', + }, + atV: 1, + }, + { + add: { + pathname: 'main.tex', + }, + atV: 0, + }, + ], + }, + ] + + const pathname = fileFinalPathname( + autoSelectFile(files, updates[0].toV, comparing, updates) + ) + + expect(pathname).to.equal('new.bib') + }) }) })