[File View] Handle file rename case on UI side (#763)

This commit is contained in:
Johannes Batzill 2023-11-03 06:40:45 +00:00 committed by Harness
parent 0370e89e29
commit ecf9d6b049
4 changed files with 38 additions and 8 deletions

View File

@ -32,7 +32,7 @@ import { useHistory } from 'react-router-dom'
import { useGet } from 'restful-react'
import { isEqual, noop } from 'lodash-es'
import { useStrings } from 'framework/strings'
import { normalizeGitRef, type GitInfoProps } from 'utils/GitUtils'
import { normalizeGitRef, type GitInfoProps, FILE_VIEWED_OBSOLETE_SHA } from 'utils/GitUtils'
import { PullRequestSection, formatNumber, getErrorMessage, voidFn } from 'utils/Utils'
import { DiffViewer } from 'components/DiffViewer/DiffViewer'
import { useEventListener } from 'hooks/useEventListener'
@ -176,7 +176,7 @@ export const Changes: React.FC<ChangesProps> = ({
rawFileViews
?.filter(({ path, sha }) => path && sha) // every entry is expected to have a path and sha - otherwise skip ...
.forEach(({ path, sha, obsolete }) => {
out.set(path || '', obsolete ? 'ffffffffffffffffffffffffffffffffffffffff' : sha || '')
out.set(path || '', obsolete ? FILE_VIEWED_OBSOLETE_SHA : sha || '')
})
return out
}, [rawFileViews])

View File

@ -61,7 +61,9 @@ import {
getCommentLineInfo,
createCommentOppositePlaceHolder,
ViewStyle,
contentDOMHasData
contentDOMHasData,
getFileViewedState,
FileViewedState
} from './DiffViewerUtils'
import {
CommentAction,
@ -114,13 +116,14 @@ export const DiffViewer: React.FC<DiffViewerProps> = ({
// file viewed feature is only enabled if no commit range is provided (otherwise component is hidden, too)
const [viewed, setViewed] = useState(
commitRange?.length === 0 && diff.fileViews?.get(diff.filePath) === diff.checksumAfter
commitRange?.length === 0 &&
getFileViewedState(diff.filePath, diff.checksumAfter, diff.fileViews) === FileViewedState.VIEWED
)
useEffect(() => {
if (commitRange?.length === 0) {
setViewed(diff.fileViews?.get(diff.filePath) === diff.checksumAfter)
setViewed(getFileViewedState(diff.filePath, diff.checksumAfter, diff.fileViews) === FileViewedState.VIEWED)
}
}, [diff.fileViews, diff.filePath, diff.checksumAfter, commitRange])
}, [setViewed, diff.fileViews, diff.filePath, diff.checksumAfter, commitRange])
const [collapsed, setCollapsed] = useState(viewed)
useEffect(() => {
@ -711,8 +714,7 @@ export const DiffViewer: React.FC<DiffViewerProps> = ({
when={
!readOnly &&
commitRange?.length === 0 &&
diff.fileViews?.get(diff.filePath) !== undefined &&
diff.fileViews?.get(diff.filePath) !== diff.checksumAfter
getFileViewedState(diff.filePath, diff.checksumAfter, diff.fileViews) === FileViewedState.CHANGED
}>
<Container>
<Text className={css.fileChanged}>{getString('changedSinceLastView')}</Text>

View File

@ -19,6 +19,7 @@ import HoganJsUtils from 'diff2html/lib/hoganjs-utils'
import { get } from 'lodash-es'
import type { CommentItem, SingleConsumerEventStream } from 'components/CommentBox/CommentBox'
import type { TypesPullReqActivity } from 'services/code'
import { FILE_VIEWED_OBSOLETE_SHA } from 'utils/GitUtils'
export enum ViewStyle {
SIDE_BY_SIDE = 'side-by-side',
@ -257,3 +258,28 @@ export function activitiesToDiffCommentItems(
}) || []
)
}
export enum FileViewedState {
NOT_VIEWED,
VIEWED,
CHANGED
}
export function getFileViewedState(
filePath: string,
fileSha: string | undefined,
views: Map<string, string> | undefined
): FileViewedState {
if (!views || !views.has(filePath)) {
return FileViewedState.NOT_VIEWED
}
const viewedSHA = views.get(filePath)
// this case is only expected in case of pure rename - but we'll also use it as fallback.
if (fileSha === undefined || fileSha === '') {
return viewedSHA === FILE_VIEWED_OBSOLETE_SHA ? FileViewedState.CHANGED : FileViewedState.VIEWED
}
return viewedSHA === fileSha ? FileViewedState.VIEWED : FileViewedState.CHANGED
}

View File

@ -234,6 +234,8 @@ export const normalizeGitRef = (gitRef: string | undefined) => {
export const REFS_TAGS_PREFIX = 'refs/tags/'
export const REFS_BRANCH_PREFIX = 'refs/heads/'
export const FILE_VIEWED_OBSOLETE_SHA = 'ffffffffffffffffffffffffffffffffffffffff'
export function formatTriggers(triggers: EnumWebhookTrigger[]) {
return triggers.map(trigger => {
return trigger