@@ -2,10 +2,15 @@ import React, { useState, useCallback } from 'react'
2
2
import styled from '@emotion/styled'
3
3
import {
4
4
Diff as RDiff ,
5
+ DiffProps as RDiffProps ,
5
6
Hunk ,
6
7
markEdits ,
7
8
tokenize ,
8
9
Decoration as DiffDecoration ,
10
+ HunkData ,
11
+ ViewType ,
12
+ DiffType ,
13
+ HunkTokens ,
9
14
} from 'react-diff-view'
10
15
import DiffHeader from './DiffHeader'
11
16
import { getComments } from './DiffComment'
@@ -30,12 +35,15 @@ const More = styled.div`
30
35
padding-left: 4px;
31
36
`
32
37
33
- const Decoration = styled ( DiffDecoration ) `
38
+ const Decoration = styled ( DiffDecoration ) < { theme ?: Theme } > `
34
39
background-color: ${ ( { theme } ) => theme . diff . decorationContentBackground } ;
35
40
color: ${ ( { theme } ) => theme . diff . decorationContent } ;
36
41
`
37
42
38
- const DiffView = styled ( RDiff ) `
43
+ interface DiffViewProps extends RDiffProps {
44
+ theme ?: Theme
45
+ }
46
+ const DiffView = styled ( RDiff ) < DiffViewProps > `
39
47
.diff-gutter-col {
40
48
width: 30px;
41
49
}
@@ -47,6 +55,7 @@ const DiffView = styled(RDiff)`
47
55
48
56
td.diff-gutter .diff-line-normal {
49
57
background-color: ${ ( { theme } ) => theme . gutterInsertBackground } ;
58
+ // background-color: ${ ( { theme } ) => theme . diff . gutterInsertBackground } ;
50
59
border-color: ${ ( { theme } ) => theme . greenBorder } ;
51
60
}
52
61
@@ -144,8 +153,33 @@ const DiffView = styled(RDiff)`
144
153
`
145
154
146
155
// 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
+ }
149
183
150
184
const Diff = ( {
151
185
packageName,
@@ -165,9 +199,9 @@ const Diff = ({
165
199
diffViewStyle,
166
200
appName,
167
201
appPackage,
168
- } ) => {
169
- const [ isDiffCollapsed , setIsDiffCollapsed ] = useState (
170
- isDiffCollapsedByDefault ( { type, hunks } )
202
+ } : DiffProps ) => {
203
+ const [ isDiffCollapsed , setIsDiffCollapsed ] = useState < boolean > (
204
+ isDiffCollapsedByDefault ( { type, hunks } ) || false
171
205
)
172
206
173
207
const [ copyPathPopoverContent , setCopyPathPopoverContent ] = useState (
@@ -185,7 +219,7 @@ const Diff = ({
185
219
}
186
220
187
221
const getHunksWithAppName = useCallback (
188
- ( originalHunks ) => {
222
+ ( originalHunks : HunkData [ ] ) => {
189
223
if ( ! appName && ! appPackage ) {
190
224
// No patching of rn-diff-purge output required.
191
225
return originalHunks
@@ -214,7 +248,6 @@ const Diff = ({
214
248
newPath,
215
249
fromVersion,
216
250
toVersion,
217
- appName,
218
251
} )
219
252
220
253
return (
@@ -256,26 +289,30 @@ const Diff = ({
256
289
optimizeSelection = { true }
257
290
selectedChanges = { selectedChanges }
258
291
>
259
- { ( originalHunks ) => {
292
+ { ( originalHunks : HunkData [ ] ) => {
260
293
const updatedHunks = getHunksWithAppName ( originalHunks )
261
294
262
295
const options = {
263
296
enhancers : [ markEdits ( updatedHunks ) ] ,
264
297
}
265
298
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
+ )
279
316
} }
280
317
</ DiffView >
281
318
) }
@@ -287,7 +324,7 @@ const Diff = ({
287
324
Return true if passing `nextProps` to render would return
288
325
the same result as passing prevProps to render, otherwise return false
289
326
*/
290
- const arePropsEqual = ( prevProps , nextProps ) =>
327
+ const arePropsEqual = ( prevProps : DiffProps , nextProps : DiffProps ) =>
291
328
prevProps . isDiffCompleted === nextProps . isDiffCompleted &&
292
329
prevProps . areAllCollapsed === nextProps . areAllCollapsed &&
293
330
prevProps . diffViewStyle === nextProps . diffViewStyle &&
0 commit comments