Skip to content

Commit 2551049

Browse files
committed
Fix some style issues after the update of react-diff-view
1 parent 18ec904 commit 2551049

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed

src/components/common/BinaryDownload.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const BinaryList: React.FC<BinaryListProps> = ({
7474
}
7575

7676
interface BinaryDownloadProps {
77-
diff: any[]
77+
diff: File[]
7878
fromVersion: string
7979
toVersion: string
8080
appName: string

src/components/common/Diff/Diff.tsx

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useCallback } from 'react'
1+
import React, { useState, useCallback, Fragment } from 'react'
22
import styled from '@emotion/styled'
33
import {
44
Diff as RDiff,
@@ -11,11 +11,13 @@ import {
1111
ViewType,
1212
DiffType,
1313
HunkTokens,
14+
TokenNode,
1415
} from 'react-diff-view'
1516
import DiffHeader from './DiffHeader'
1617
import { getComments } from './DiffComment'
1718
import { replaceAppDetails } from '../../../utils'
1819
import type { Theme } from '../../../theme'
20+
import type { DefaultRenderToken } from 'react-diff-view/types/context'
1921

2022
const copyPathPopoverContentOpts = {
2123
default: 'Click to copy file path',
@@ -54,8 +56,7 @@ const DiffView = styled(RDiff)<DiffViewProps>`
5456
}
5557
5658
td.diff-gutter .diff-line-normal {
57-
background-color: ${({ theme }) => theme.gutterInsertBackground};
58-
// background-color: ${({ theme }) => theme.diff.gutterInsertBackground};
59+
background-color: ${({ theme }) => theme.diff.gutterInsertBackground};
5960
border-color: ${({ theme }) => theme.greenBorder};
6061
}
6162
@@ -80,26 +81,25 @@ const DiffView = styled(RDiff)<DiffViewProps>`
8081
8182
// From diff global
8283
.diff {
83-
background-color: ${({ theme }) => theme.diff.backgroundColor};
84-
color: ${({ theme }) => theme.diff.text};
84+
background-color: ${({ theme }) => theme.background};
85+
color: ${({ theme }) => theme.text};
8586
tab-size: 4;
8687
hyphens: none;
8788
}
8889
8990
.diff::selection {
90-
background-color: ${({ theme }) => theme.diff.selectionMackground};
91+
background-color: ${({ theme }) => theme.diff.selectionBackground};
9192
}
9293
9394
.diff-decoration {
9495
line-height: 2;
9596
font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier,
9697
monospace;
97-
background-color: ${({ theme }) => theme.diff.decorationBackground};
9898
}
9999
100100
.diff-decoration-content {
101101
padding-left: 0.5em;
102-
background-color: ${({ theme }) => theme.diff.contentBackground};
102+
background-color: ${({ theme }) => theme.diff.decorationContentBackground};
103103
color: ${({ theme }) => theme.diff.decorationContent};
104104
}
105105
@@ -161,6 +161,27 @@ const isDiffCollapsedByDefault = ({
161161
hunks: HunkData[]
162162
}) => (type === 'delete' || hunks.length > 5 ? true : undefined)
163163

164+
const renderToken = (
165+
token: TokenNode,
166+
renderDefault: DefaultRenderToken,
167+
index: number
168+
) => {
169+
switch (token.type) {
170+
case 'space':
171+
console.log(token)
172+
return (
173+
<span key={index} className="space">
174+
{token.children &&
175+
token.children.map((token, index) =>
176+
renderToken(token, renderDefault, index)
177+
)}
178+
</span>
179+
)
180+
default:
181+
return renderDefault(token, index)
182+
}
183+
}
184+
164185
interface DiffProps {
165186
packageName: string
166187
oldPath: string
@@ -250,6 +271,15 @@ const Diff = ({
250271
toVersion,
251272
})
252273

274+
const updatedHunks = React.useMemo(() => getHunksWithAppName(hunks), [hunks])
275+
const tokens: HunkTokens = React.useMemo(
276+
() =>
277+
tokenize(hunks, {
278+
enhancers: [markEdits(updatedHunks)],
279+
}),
280+
[hunks, updatedHunks]
281+
)
282+
253283
return (
254284
<Container>
255285
<DiffHeader
@@ -285,35 +315,28 @@ const Diff = ({
285315
viewType={diffViewStyle}
286316
diffType={type}
287317
hunks={hunks}
318+
renderToken={renderToken}
319+
tokens={tokens}
288320
widgets={diffComments}
289321
optimizeSelection={true}
290322
selectedChanges={selectedChanges}
291323
>
292-
{(originalHunks: HunkData[]) => {
293-
const updatedHunks = getHunksWithAppName(originalHunks)
294-
295-
const options = {
296-
enhancers: [markEdits(updatedHunks)],
297-
}
298-
299-
const tokens: HunkTokens = tokenize(updatedHunks, options)
300-
301-
return (
302-
<>
324+
{(hunks: HunkData[]) =>
325+
hunks.map((hunk) => (
326+
<Fragment key={hunk.content}>
303327
{updatedHunks.map((hunk) => [
304328
<Decoration key={'decoration-' + hunk.content}>
305329
<More>{hunk.content}</More>
306330
</Decoration>,
307331
<Hunk
308332
key={hunk.content}
309333
hunk={hunk}
310-
tokens={tokens}
311334
gutterEvents={{ onClick: onToggleChangeSelection }}
312335
/>,
313336
])}
314-
</>
315-
)
316-
}}
337+
</Fragment>
338+
))
339+
}
317340
</DiffView>
318341
)}
319342
</Container>

src/components/common/Diff/DiffSection.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ const Title = styled(Typography.Title)`
1212
margin-top: 0.5em;
1313
`
1414

15+
interface DiffSectionProps {
16+
packageName: string
17+
diff: any
18+
getDiffKey: (diffFile: any) => string
19+
title: string
20+
completedDiffs: string[]
21+
isDoneSection: boolean
22+
fromVersion: string
23+
toVersion: string
24+
handleCompleteDiff: (diffKey: string) => void
25+
selectedChanges: string[]
26+
onToggleChangeSelection: (diffKey: string, isSelected: boolean) => void
27+
diffViewStyle: string
28+
appName: string
29+
appPackage: string
30+
doneTitleRef: React.RefObject<HTMLHeadingElement>
31+
}
32+
1533
const DiffSection = ({
1634
packageName,
1735
diff,
@@ -28,7 +46,7 @@ const DiffSection = ({
2846
appName,
2947
appPackage,
3048
doneTitleRef,
31-
}) => {
49+
}: DiffSectionProps) => {
3250
const [areAllCollapsed, setAllCollapsed] = useState(undefined)
3351

3452
const getIsUpgradingFrom61To62 = useCallback(() => {

src/hooks/fetch-diff.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const useFetchDiff = ({
2424
}: UseFetchDiffProps) => {
2525
const [isLoading, setIsLoading] = useState(true)
2626
const [isDone, setIsDone] = useState(false)
27-
const [diff, setDiff] = useState<File[] | undefined>(undefined)
27+
const [diff, setDiff] = useState<File[]>([])
2828

2929
useEffect(() => {
3030
const fetchDiff = async () => {

0 commit comments

Comments
 (0)