-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
202 lines (191 loc) · 6.65 KB
/
Copy pathindex.js
File metadata and controls
202 lines (191 loc) · 6.65 KB
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
import {useRef, useState} from "react"
import {Body, Button, Container, FileContentContainer, FooterButtons, Header, RightSideBar} from "./style"
import useDiff from "./useDiff"
import MetaData from "./MetaData"
import FilePreview from "./FilePreview"
import Toast from "./Toast"
import FileSelector from "./FileSelector"
import DIFFKeysCardViewer from "./DIFFKeysCardViewer"
const inititalState = {
fileOne: {
name: "",
file: null,
show: false,
content: null,
},
fileTwo: {
name: "",
file: null,
show: false,
content: null,
},
}
function JSONDiffChecker() {
const [selectedFiles, setSelectedFile] = useState({...inititalState})
const toastRef = useRef(null)
const fileOnePreviewRef = useRef(null)
const fileTwoPreviewRef = useRef(null)
const {compareContent, missingKeys, clearComparison, metaData, isLoading, clearFileData} = useDiff()
const handleFileChange = ({fileNumber, fileData, inputFrom, url}) => {
try {
if (inputFrom === "file") {
// console.log("handleFileChange", fileData)
if (fileData.type !== "application/json") {
toastRef.current.showToast("Please select a json file")
return
}
if (fileData.size > 1500000) {
toastRef.current.showToast("Please select a file less than 1.5MB")
return
}
const reader = new FileReader()
reader.readAsText(fileData, "UTF-8")
reader.onload = (e) => {
try {
console.log(typeof e.target.result, JSON.parse(e.target.result))
setSelectedFile({
...selectedFiles,
[fileNumber]: {
...selectedFiles[fileNumber],
file: fileData,
name: fileData?.name,
content: JSON.parse(e.target.result),
show: true,
usingLink: false,
},
})
} catch (error) {
toastRef.current.showToast("Not a Valid JSON File")
}
}
} else if (inputFrom === "link") {
// console.log("handleFileChange", fileData)
setSelectedFile({
...selectedFiles,
[fileNumber]: {
...selectedFiles[fileNumber],
file: fileData,
name: fileData?.name,
content: JSON.parse(fileData?.data),
show: true,
usingLink: true,
url,
},
})
}
} catch (error) {
console.log(error)
toastRef.current.showToast("😓 Something went south. Please try again")
}
}
const onCompare = () => {
console.log("compare")
if (!selectedFiles.fileOne.file || !selectedFiles.fileTwo.file) {
toastRef.current.showToast("Please select two files to compare")
return
}
const {totalCount, error} = compareContent(selectedFiles)
if (error) {
toastRef.current.showToast(error)
return
}
toastRef.current.showToast(`Total keys missmatched: ${totalCount}`)
}
const clearFile = (e) => {
e.stopPropagation()
const name = e.target.getAttribute("name")
console.log(name)
clearFileData(name)
setSelectedFile({
...selectedFiles,
[name]: {
name: "",
file: null,
show: false,
content: null,
},
})
}
const clearAll = () => {
setSelectedFile({...inititalState})
clearComparison()
}
const copyDiffAsText = () => {
let text = ""
text += `Total keys missmatched: ${missingKeys.totalCount}\n\n`
text += `Extra Keys in file "${selectedFiles.fileOne.name}": ${missingKeys.compOne.length}\n`
text += missingKeys.compOne.map((item) => item.keyValue).join("\n")
text += "\n\n-------------------------------------------------- \n\n"
text += `Extra Keys in file "${selectedFiles.fileTwo.name}": ${missingKeys.compTwo.length}\n`
text += missingKeys.compTwo.map((item) => item.keyValue).join("\n")
navigator.clipboard.writeText(text)
toastRef.current.showToast("Copied to clipboard")
console.log(text)
}
const onKeyRowClick = (item) => {
if ([selectedFiles.fileOne.name, selectedFiles.fileOne.url].includes(item.fileName)) {
fileOnePreviewRef.current.scrollToVisible(item.keyValue)
}
if ([selectedFiles.fileTwo.name, selectedFiles.fileTwo.url].includes(item.fileName)) {
fileTwoPreviewRef.current.scrollToVisible(item.keyValue)
}
}
return (
<>
<Container>
<Header>
<h1>JSON Diff Checker</h1>
<p>This is a JSON Diff Checker. It takes two JSON files and compares</p>
</Header>
<Body>
<FileContentContainer>
<FooterButtons>
<Button onClick={clearAll}>Clear</Button>
<Button onClick={onCompare}>{isLoading ? "Comparing ..." : "Compare"}</Button>
</FooterButtons>
<div className="main">
<div className="fileViewColumn">
<FileSelector fileNumber="fileOne" file={selectedFiles.fileOne.file} onChange={handleFileChange} />
{selectedFiles.fileOne.file && (
<FilePreview
name="fileOne"
onClearClick={clearFile}
ref={fileOnePreviewRef}
flattenObject={metaData.fileOne.flattenObject}
file={selectedFiles.fileOne}
missingKeys={missingKeys.compOne}
/>
)}
</div>
<div className="fileViewColumn">
<FileSelector fileNumber="fileTwo" file={selectedFiles.fileTwo.file} onChange={handleFileChange} />
{selectedFiles.fileTwo.file && (
<FilePreview
name="fileTwo"
onClearClick={clearFile}
ref={fileTwoPreviewRef}
flattenObject={metaData.fileTwo.flattenObject}
file={selectedFiles.fileTwo}
missingKeys={missingKeys.compTwo}
/>
)}
</div>
</div>
</FileContentContainer>
<RightSideBar>
{missingKeys.totalCount > 0 && (
<MetaData metaData={metaData} missingKeys={missingKeys} selectedFiles={selectedFiles} />
)}
{missingKeys.totalCount > 0 && <Button onClick={copyDiffAsText}>Copy Diff as text</Button>}
{missingKeys.totalCount > 0 && (
<DIFFKeysCardViewer onRowClick={onKeyRowClick} selectedFiles={selectedFiles} missingKeys={missingKeys} />
)}
</RightSideBar>
</Body>
</Container>
<Toast ref={toastRef} />
{/* <Footer /> */}
</>
)
}
export default JSONDiffChecker