mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
0648b8aa6c
* Add strikethrough to deleted file tree item in history file tree * Add "restore file" button on toolbar if the selected file have a `removed` operation. * Implement "restore file" functionality on removed file: - Refactor the `Selection` object in history context value since we need the `deletedAtV` data which currently is not passed through the state. - Refactor and clean up file tree type system to support passing through the whole `FileDiff` object for getting the `deletedAtV` data which only appear on `removed` operation - Implement `postJSON` with file restoration API and pass it on restore file onClick handler at toolbar * Implement loading behaviour while restoring file is inflight: - Add `loadingRestoreFile` to `LoadingState` - Change restore file button to `Restoring...` when in loading state. * Refactor: - Rename `DiffOperation` to `FileOperation` - Extract `isFileRemoved` and `isFileRenamed` to its own file - Extract `Toolbar` components into small files GitOrigin-RevId: 2e32ebd2165f73fc6533ff282a9c084162efd682
135 lines
3.1 KiB
TypeScript
135 lines
3.1 KiB
TypeScript
import _ from 'lodash'
|
|
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'
|
|
|
|
function getUpdateForVersion(
|
|
version: Version,
|
|
updates: LoadedUpdate[]
|
|
): Nullable<LoadedUpdate> {
|
|
return updates.filter(update => update.toV === version)?.[0] ?? null
|
|
}
|
|
|
|
type FileWithOps = {
|
|
pathname: FileDiff['pathname']
|
|
operation: FileOperation
|
|
}
|
|
|
|
function getFilesWithOps(
|
|
files: FileDiff[],
|
|
toV: Version,
|
|
comparing: boolean,
|
|
updates: LoadedUpdate[]
|
|
): FileWithOps[] {
|
|
if (toV && !comparing) {
|
|
const filesWithOps: FileWithOps[] = []
|
|
const currentUpdate = getUpdateForVersion(toV, updates)
|
|
|
|
if (currentUpdate !== null) {
|
|
for (const pathname of currentUpdate.pathnames) {
|
|
filesWithOps.push({
|
|
pathname,
|
|
operation: 'edited',
|
|
})
|
|
}
|
|
|
|
for (const op of currentUpdate.project_ops) {
|
|
let fileWithOps: Nullable<FileWithOps> = null
|
|
|
|
if (op.add) {
|
|
fileWithOps = {
|
|
pathname: op.add.pathname,
|
|
operation: 'added',
|
|
}
|
|
} else if (op.remove) {
|
|
fileWithOps = {
|
|
pathname: op.remove.pathname,
|
|
operation: 'removed',
|
|
}
|
|
} else if (op.rename) {
|
|
fileWithOps = {
|
|
pathname: op.rename.newPathname,
|
|
operation: 'renamed',
|
|
}
|
|
}
|
|
|
|
if (fileWithOps !== null) {
|
|
filesWithOps.push(fileWithOps)
|
|
}
|
|
}
|
|
}
|
|
|
|
return filesWithOps
|
|
} else {
|
|
const filesWithOps = _.reduce(
|
|
files,
|
|
(curFilesWithOps, file) => {
|
|
if ('operation' in file) {
|
|
curFilesWithOps.push({
|
|
pathname: file.pathname,
|
|
operation: file.operation,
|
|
})
|
|
}
|
|
return curFilesWithOps
|
|
},
|
|
<FileWithOps[]>[]
|
|
)
|
|
|
|
return filesWithOps
|
|
}
|
|
}
|
|
|
|
const orderedOpTypes: FileOperation[] = [
|
|
'edited',
|
|
'added',
|
|
'renamed',
|
|
'removed',
|
|
]
|
|
|
|
export function autoSelectFile(
|
|
files: FileDiff[],
|
|
toV: Version,
|
|
comparing: boolean,
|
|
updates: LoadedUpdate[]
|
|
): FileDiff {
|
|
let fileToSelect: Nullable<FileDiff> = null
|
|
|
|
const filesWithOps = getFilesWithOps(files, toV, comparing, updates)
|
|
for (const opType of orderedOpTypes) {
|
|
const fileWithMatchingOpType = _.find(filesWithOps, {
|
|
operation: opType,
|
|
})
|
|
|
|
if (fileWithMatchingOpType != null) {
|
|
fileToSelect =
|
|
_.find(files, {
|
|
pathname: fileWithMatchingOpType.pathname,
|
|
}) ?? null
|
|
|
|
break
|
|
}
|
|
}
|
|
|
|
if (!fileToSelect) {
|
|
const mainFile = _.find(files, function (file) {
|
|
return /main\.tex$/.test(file.pathname)
|
|
})
|
|
|
|
if (mainFile) {
|
|
fileToSelect = mainFile
|
|
} else {
|
|
const anyTeXFile = _.find(files, function (file) {
|
|
return /\.tex$/.test(file.pathname)
|
|
})
|
|
|
|
if (anyTeXFile) {
|
|
fileToSelect = anyTeXFile
|
|
} else {
|
|
fileToSelect = files[0]
|
|
}
|
|
}
|
|
}
|
|
|
|
return fileToSelect
|
|
}
|