-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathDiffViewer.js
205 lines (180 loc) · 5.4 KB
/
DiffViewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import React, { useState, useEffect, useRef } from 'react'
import styled from '@emotion/styled'
import { Alert } from 'antd'
import { motion, AnimatePresence, AnimateSharedLayout } from 'framer-motion'
import { withChangeSelect } from 'react-diff-view'
import 'react-diff-view/style/index.css'
import { getTransitionDuration } from '../../utils'
import DiffSection from './Diff/DiffSection'
import DiffLoading from './Diff/DiffLoading'
import UsefulContentSection from './UsefulContentSection'
import BinaryDownload from './BinaryDownload'
import ViewStyleOptions from './Diff/DiffViewStyleOptions'
import CompletedFilesCounter from './CompletedFilesCounter'
import { useFetchDiff } from '../../hooks/fetch-diff'
const Container = styled.div`
width: 90%;
`
const TopContainer = styled.div`
display: flex;
position: relative;
border-width: 1px;
margin-top: 16px;
flex-direction: row;
justify-content: flex-end;
`
const getDiffKey = ({ oldRevision, newRevision }) =>
`${oldRevision}${newRevision}`
const scrollToRef = ref => ref.current.scrollIntoView({ behavior: 'smooth' })
const DiffViewer = ({
packageName,
fromVersion,
toVersion,
shouldShowDiff,
selectedChanges,
onToggleChangeSelection,
appName
}) => {
const { isLoading, isDone, diff } = useFetchDiff({
shouldShowDiff,
packageName,
fromVersion,
toVersion
})
const [completedDiffs, setCompletedDiffs] = useState([])
const [isGoToDoneClicked, setIsGoToDoneClicked] = useState(false)
const donePopoverPossibleOpts = {
done: {
content: 'Scroll to Done section',
cursorType: 's-resize'
},
top: {
content: 'Scroll to Top',
cursorType: 'n-resize'
}
}
const [donePopoverOpts, setDonePopoverOpts] = useState(
donePopoverPossibleOpts.done
)
const doneTitleRef = useRef(null)
const scrollToDone = () => scrollToRef(doneTitleRef)
const scrollToTop = () => window.scrollTo({ top: 0, behavior: 'smooth' })
const handleCompletedFilesCounterClick = () => {
setIsGoToDoneClicked(!isGoToDoneClicked)
if (isGoToDoneClicked) {
setDonePopoverOpts(donePopoverPossibleOpts.done)
scrollToTop()
} else {
setDonePopoverOpts(donePopoverPossibleOpts.top)
scrollToDone()
}
}
const handleCompleteDiff = diffKey => {
if (completedDiffs.includes(diffKey)) {
return setCompletedDiffs(prevCompletedDiffs =>
prevCompletedDiffs.filter(completedDiff => completedDiff !== diffKey)
)
}
setCompletedDiffs(prevCompletedDiffs => [...prevCompletedDiffs, diffKey])
}
const renderUpgradeDoneMessage = ({ diff, completedDiffs }) =>
diff.length === completedDiffs.length && (
<Alert
style={{ marginTop: 16 }}
message="Your upgrade is done 🎉🎉"
type="success"
showIcon
closable
/>
)
const resetCompletedDiffs = () => setCompletedDiffs([])
const [diffViewStyle, setViewStyle] = useState(
localStorage.getItem('viewStyle') || 'split'
)
const handleViewStyleChange = newViewStyle => {
setViewStyle(newViewStyle)
localStorage.setItem('viewStyle', newViewStyle)
}
useEffect(() => {
if (!isDone) {
resetCompletedDiffs()
}
}, [isDone])
if (!shouldShowDiff) {
return null
}
if (isLoading) {
return (
<Container>
<AnimatePresence>
<DiffLoading />
</AnimatePresence>
</Container>
)
}
const diffSectionProps = {
diff,
getDiffKey: getDiffKey,
completedDiffs: completedDiffs,
fromVersion: fromVersion,
toVersion: toVersion,
handleCompleteDiff: handleCompleteDiff,
selectedChanges: selectedChanges,
onToggleChangeSelection: onToggleChangeSelection
}
return (
<Container>
<AnimateSharedLayout>
<motion.div
initial={{ opacity: 0, translateY: 75 }}
animate={{ opacity: 1, translateY: 0 }}
transition={{ duration: getTransitionDuration(0.5) }}
>
<UsefulContentSection
isLoading={isLoading}
packageName={packageName}
fromVersion={fromVersion}
toVersion={toVersion}
/>
<TopContainer>
<BinaryDownload
diff={diff}
fromVersion={fromVersion}
toVersion={toVersion}
appName={appName}
packageName={packageName}
/>
<ViewStyleOptions
handleViewStyleChange={handleViewStyleChange}
diffViewStyle={diffViewStyle}
/>
</TopContainer>
<DiffSection
{...diffSectionProps}
packageName={packageName}
isDoneSection={false}
diffViewStyle={diffViewStyle}
appName={appName}
/>
{renderUpgradeDoneMessage({ diff, completedDiffs })}
<DiffSection
{...diffSectionProps}
packageName={packageName}
isDoneSection={true}
title="Done"
appName={appName}
doneTitleRef={doneTitleRef}
/>
</motion.div>
</AnimateSharedLayout>
<CompletedFilesCounter
completed={completedDiffs.length}
total={diff.length}
onClick={handleCompletedFilesCounterClick}
popoverContent={donePopoverOpts.content}
popoverCursorType={donePopoverOpts.cursorType}
/>
</Container>
)
}
export default withChangeSelect({ multiple: true })(DiffViewer)