mirror of
https://github.com/harness/drone.git
synced 2025-05-19 02:20:03 +08:00
[File View] Handle file rename case on UI side (#763)
This commit is contained in:
parent
0370e89e29
commit
ecf9d6b049
@ -32,7 +32,7 @@ import { useHistory } from 'react-router-dom'
|
|||||||
import { useGet } from 'restful-react'
|
import { useGet } from 'restful-react'
|
||||||
import { isEqual, noop } from 'lodash-es'
|
import { isEqual, noop } from 'lodash-es'
|
||||||
import { useStrings } from 'framework/strings'
|
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 { PullRequestSection, formatNumber, getErrorMessage, voidFn } from 'utils/Utils'
|
||||||
import { DiffViewer } from 'components/DiffViewer/DiffViewer'
|
import { DiffViewer } from 'components/DiffViewer/DiffViewer'
|
||||||
import { useEventListener } from 'hooks/useEventListener'
|
import { useEventListener } from 'hooks/useEventListener'
|
||||||
@ -176,7 +176,7 @@ export const Changes: React.FC<ChangesProps> = ({
|
|||||||
rawFileViews
|
rawFileViews
|
||||||
?.filter(({ path, sha }) => path && sha) // every entry is expected to have a path and sha - otherwise skip ...
|
?.filter(({ path, sha }) => path && sha) // every entry is expected to have a path and sha - otherwise skip ...
|
||||||
.forEach(({ path, sha, obsolete }) => {
|
.forEach(({ path, sha, obsolete }) => {
|
||||||
out.set(path || '', obsolete ? 'ffffffffffffffffffffffffffffffffffffffff' : sha || '')
|
out.set(path || '', obsolete ? FILE_VIEWED_OBSOLETE_SHA : sha || '')
|
||||||
})
|
})
|
||||||
return out
|
return out
|
||||||
}, [rawFileViews])
|
}, [rawFileViews])
|
||||||
|
@ -61,7 +61,9 @@ import {
|
|||||||
getCommentLineInfo,
|
getCommentLineInfo,
|
||||||
createCommentOppositePlaceHolder,
|
createCommentOppositePlaceHolder,
|
||||||
ViewStyle,
|
ViewStyle,
|
||||||
contentDOMHasData
|
contentDOMHasData,
|
||||||
|
getFileViewedState,
|
||||||
|
FileViewedState
|
||||||
} from './DiffViewerUtils'
|
} from './DiffViewerUtils'
|
||||||
import {
|
import {
|
||||||
CommentAction,
|
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)
|
// file viewed feature is only enabled if no commit range is provided (otherwise component is hidden, too)
|
||||||
const [viewed, setViewed] = useState(
|
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(() => {
|
useEffect(() => {
|
||||||
if (commitRange?.length === 0) {
|
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)
|
const [collapsed, setCollapsed] = useState(viewed)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -711,8 +714,7 @@ export const DiffViewer: React.FC<DiffViewerProps> = ({
|
|||||||
when={
|
when={
|
||||||
!readOnly &&
|
!readOnly &&
|
||||||
commitRange?.length === 0 &&
|
commitRange?.length === 0 &&
|
||||||
diff.fileViews?.get(diff.filePath) !== undefined &&
|
getFileViewedState(diff.filePath, diff.checksumAfter, diff.fileViews) === FileViewedState.CHANGED
|
||||||
diff.fileViews?.get(diff.filePath) !== diff.checksumAfter
|
|
||||||
}>
|
}>
|
||||||
<Container>
|
<Container>
|
||||||
<Text className={css.fileChanged}>{getString('changedSinceLastView')}</Text>
|
<Text className={css.fileChanged}>{getString('changedSinceLastView')}</Text>
|
||||||
|
@ -19,6 +19,7 @@ import HoganJsUtils from 'diff2html/lib/hoganjs-utils'
|
|||||||
import { get } from 'lodash-es'
|
import { get } from 'lodash-es'
|
||||||
import type { CommentItem, SingleConsumerEventStream } from 'components/CommentBox/CommentBox'
|
import type { CommentItem, SingleConsumerEventStream } from 'components/CommentBox/CommentBox'
|
||||||
import type { TypesPullReqActivity } from 'services/code'
|
import type { TypesPullReqActivity } from 'services/code'
|
||||||
|
import { FILE_VIEWED_OBSOLETE_SHA } from 'utils/GitUtils'
|
||||||
|
|
||||||
export enum ViewStyle {
|
export enum ViewStyle {
|
||||||
SIDE_BY_SIDE = 'side-by-side',
|
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
|
||||||
|
}
|
||||||
|
@ -234,6 +234,8 @@ export const normalizeGitRef = (gitRef: string | undefined) => {
|
|||||||
export const REFS_TAGS_PREFIX = 'refs/tags/'
|
export const REFS_TAGS_PREFIX = 'refs/tags/'
|
||||||
export const REFS_BRANCH_PREFIX = 'refs/heads/'
|
export const REFS_BRANCH_PREFIX = 'refs/heads/'
|
||||||
|
|
||||||
|
export const FILE_VIEWED_OBSOLETE_SHA = 'ffffffffffffffffffffffffffffffffffffffff'
|
||||||
|
|
||||||
export function formatTriggers(triggers: EnumWebhookTrigger[]) {
|
export function formatTriggers(triggers: EnumWebhookTrigger[]) {
|
||||||
return triggers.map(trigger => {
|
return triggers.map(trigger => {
|
||||||
return trigger
|
return trigger
|
||||||
|
Loading…
Reference in New Issue
Block a user