Skip to content

Commit 18ec904

Browse files
committed
Updates to the Diff component
1 parent 94471fc commit 18ec904

File tree

2 files changed

+81
-32
lines changed

2 files changed

+81
-32
lines changed

src/components/common/Diff/Diff.tsx

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import React, { useState, useCallback } from 'react'
22
import styled from '@emotion/styled'
33
import {
44
Diff as RDiff,
5+
DiffProps as RDiffProps,
56
Hunk,
67
markEdits,
78
tokenize,
89
Decoration as DiffDecoration,
10+
HunkData,
11+
ViewType,
12+
DiffType,
13+
HunkTokens,
914
} from 'react-diff-view'
1015
import DiffHeader from './DiffHeader'
1116
import { getComments } from './DiffComment'
@@ -30,12 +35,15 @@ const More = styled.div`
3035
padding-left: 4px;
3136
`
3237

33-
const Decoration = styled(DiffDecoration)`
38+
const Decoration = styled(DiffDecoration)<{ theme?: Theme }>`
3439
background-color: ${({ theme }) => theme.diff.decorationContentBackground};
3540
color: ${({ theme }) => theme.diff.decorationContent};
3641
`
3742

38-
const DiffView = styled(RDiff)`
43+
interface DiffViewProps extends RDiffProps {
44+
theme?: Theme
45+
}
46+
const DiffView = styled(RDiff)<DiffViewProps>`
3947
.diff-gutter-col {
4048
width: 30px;
4149
}
@@ -47,6 +55,7 @@ const DiffView = styled(RDiff)`
4755
4856
td.diff-gutter .diff-line-normal {
4957
background-color: ${({ theme }) => theme.gutterInsertBackground};
58+
// background-color: ${({ theme }) => theme.diff.gutterInsertBackground};
5059
border-color: ${({ theme }) => theme.greenBorder};
5160
}
5261
@@ -144,8 +153,33 @@ const DiffView = styled(RDiff)`
144153
`
145154

146155
// Diff will be collapsed by default if the file has been deleted or has more than 5 hunks
147-
const isDiffCollapsedByDefault = ({ type, hunks }) =>
148-
type === 'delete' || hunks.length > 5 ? true : undefined
156+
const isDiffCollapsedByDefault = ({
157+
type,
158+
hunks,
159+
}: {
160+
type: DiffType
161+
hunks: HunkData[]
162+
}) => (type === 'delete' || hunks.length > 5 ? true : undefined)
163+
164+
interface DiffProps {
165+
packageName: string
166+
oldPath: string
167+
newPath: string
168+
type: DiffType
169+
hunks: HunkData[]
170+
fromVersion: string
171+
toVersion: string
172+
diffKey: string
173+
isDiffCompleted: boolean
174+
onCompleteDiff: (diffKey: string) => void
175+
selectedChanges: string[]
176+
onToggleChangeSelection: (change: string) => void
177+
areAllCollapsed?: boolean
178+
setAllCollapsed: (collapse: boolean | undefined) => void
179+
diffViewStyle: ViewType
180+
appName: string
181+
appPackage: string
182+
}
149183

150184
const Diff = ({
151185
packageName,
@@ -165,9 +199,9 @@ const Diff = ({
165199
diffViewStyle,
166200
appName,
167201
appPackage,
168-
}) => {
169-
const [isDiffCollapsed, setIsDiffCollapsed] = useState(
170-
isDiffCollapsedByDefault({ type, hunks })
202+
}: DiffProps) => {
203+
const [isDiffCollapsed, setIsDiffCollapsed] = useState<boolean>(
204+
isDiffCollapsedByDefault({ type, hunks }) || false
171205
)
172206

173207
const [copyPathPopoverContent, setCopyPathPopoverContent] = useState(
@@ -185,7 +219,7 @@ const Diff = ({
185219
}
186220

187221
const getHunksWithAppName = useCallback(
188-
(originalHunks) => {
222+
(originalHunks: HunkData[]) => {
189223
if (!appName && !appPackage) {
190224
// No patching of rn-diff-purge output required.
191225
return originalHunks
@@ -214,7 +248,6 @@ const Diff = ({
214248
newPath,
215249
fromVersion,
216250
toVersion,
217-
appName,
218251
})
219252

220253
return (
@@ -256,26 +289,30 @@ const Diff = ({
256289
optimizeSelection={true}
257290
selectedChanges={selectedChanges}
258291
>
259-
{(originalHunks) => {
292+
{(originalHunks: HunkData[]) => {
260293
const updatedHunks = getHunksWithAppName(originalHunks)
261294

262295
const options = {
263296
enhancers: [markEdits(updatedHunks)],
264297
}
265298

266-
const tokens = tokenize(updatedHunks, options)
267-
268-
return updatedHunks.map((hunk) => [
269-
<Decoration key={'decoration-' + hunk.content}>
270-
<More>{hunk.content}</More>
271-
</Decoration>,
272-
<Hunk
273-
key={hunk.content}
274-
hunk={hunk}
275-
tokens={tokens}
276-
gutterEvents={{ onClick: onToggleChangeSelection }}
277-
/>,
278-
])
299+
const tokens: HunkTokens = tokenize(updatedHunks, options)
300+
301+
return (
302+
<>
303+
{updatedHunks.map((hunk) => [
304+
<Decoration key={'decoration-' + hunk.content}>
305+
<More>{hunk.content}</More>
306+
</Decoration>,
307+
<Hunk
308+
key={hunk.content}
309+
hunk={hunk}
310+
tokens={tokens}
311+
gutterEvents={{ onClick: onToggleChangeSelection }}
312+
/>,
313+
])}
314+
</>
315+
)
279316
}}
280317
</DiffView>
281318
)}
@@ -287,7 +324,7 @@ const Diff = ({
287324
Return true if passing `nextProps` to render would return
288325
the same result as passing prevProps to render, otherwise return false
289326
*/
290-
const arePropsEqual = (prevProps, nextProps) =>
327+
const arePropsEqual = (prevProps: DiffProps, nextProps: DiffProps) =>
291328
prevProps.isDiffCompleted === nextProps.isDiffCompleted &&
292329
prevProps.areAllCollapsed === nextProps.areAllCollapsed &&
293330
prevProps.diffViewStyle === nextProps.diffViewStyle &&

src/components/common/Diff/DiffComment.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import React, { useState } from 'react'
22
import styled from '@emotion/styled'
3-
import { HTMLMotionProps, MotionProps, motion } from 'framer-motion'
3+
import { HTMLMotionProps, motion } from 'framer-motion'
44
import { removeAppPathPrefix, getVersionsContentInDiff } from '../../../utils'
55
import Markdown from '../Markdown'
66
import type { Theme } from '../../../theme'
7+
import { LineChangeT, ReleaseCommentT, ReleaseT } from '../../../releases/types'
8+
79
interface ContainerProps
810
extends React.PropsWithChildren<HTMLMotionProps<'div'>> {
911
isCommentOpen: boolean
10-
lineChangeType: 'add' | 'delete'
12+
lineChangeType: LineChangeT
1113
theme?: Theme
1214
}
1315

@@ -43,6 +45,7 @@ const Container = styled(
4345
const colorMap = {
4446
add: theme.diff.codeInsertBackground,
4547
delete: theme.diff.codeDeleteBackground,
48+
neutral: undefined,
4649
}
4750
4851
return colorMap[lineChangeType] || theme.background
@@ -113,7 +116,7 @@ const getLineNumberWithType = ({
113116
lineChangeType,
114117
lineNumber,
115118
}: {
116-
lineChangeType: 'add' | 'delete' | 'neutral'
119+
lineChangeType: LineChangeT
117120
lineNumber: number
118121
}) => `${LINE_CHANGE_TYPES[lineChangeType.toUpperCase()]}${lineNumber}`
119122

@@ -135,15 +138,18 @@ const getComments = ({
135138
fromVersion,
136139
toVersion,
137140
}).filter(
138-
({ comments }) =>
141+
({ comments }: ReleaseT) =>
139142
comments &&
140143
comments.length > 0 &&
141144
comments.some(({ fileName }) => fileName === newPathSanitized)
142145
)
143146

144-
return versionsInDiff.reduce((allComments, version) => {
145-
const comments = version.comments.reduce(
146-
(versionComments, { fileName, lineChangeType, lineNumber, content }) => {
147+
return versionsInDiff.reduce((allComments, version: ReleaseT) => {
148+
const comments = version.comments?.reduce(
149+
(
150+
versionComments,
151+
{ fileName, lineChangeType, lineNumber, content }: ReleaseCommentT
152+
) => {
147153
if (fileName !== newPathSanitized) {
148154
return versionComments
149155
}
@@ -165,7 +171,13 @@ const getComments = ({
165171
}, {})
166172
}
167173

168-
const DiffComment = ({ content, lineChangeType }) => {
174+
const DiffComment = ({
175+
content,
176+
lineChangeType,
177+
}: {
178+
content: any
179+
lineChangeType: LineChangeT
180+
}) => {
169181
const [isCommentOpen, setIsCommentOpen] = useState(true)
170182

171183
return (

0 commit comments

Comments
 (0)