Merge pull request #13068 from overleaf/td-history-renamed-file-auto-select

History migration: Fix bug that prevents renamed files being automatically selected

GitOrigin-RevId: ce127b218d1eb472fe7b35467a17549f3db4bfb7
This commit is contained in:
June Kelly 2023-05-16 09:17:57 +01:00 committed by Copybot
parent 0aefc76f79
commit 6c2bc2fe8b
4 changed files with 82 additions and 4 deletions

View file

@ -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}
/>

View file

@ -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
}

View file

@ -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
}

View file

@ -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')
})
})
})