-
Notifications
You must be signed in to change notification settings - Fork 0
/
VersionDrawer.tsx
351 lines (328 loc) · 14.5 KB
/
VersionDrawer.tsx
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import React, { useState, useEffect, useRef } from "react"
import { useDisclosure } from "@chakra-ui/react"
import { useRouter } from "next/router"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import {
faClockRotateLeft,
faCodeCommit,
faFile,
faCirclePlus,
faCircleMinus,
faMagnifyingGlass,
faTimesCircle,
} from "@fortawesome/free-solid-svg-icons"
import {
Alert,
AlertDescription,
AlertIcon,
AlertTitle,
Box,
Code,
Collapse,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerHeader,
DrawerOverlay,
Flex,
HStack,
IconButton,
Input,
InputGroup,
InputRightElement,
Text,
useColorModeValue,
VStack,
} from "@chakra-ui/react"
const CommitCard = ({ commit, message, date, author, diff, currentVersion, hoverBackgroundColor }) => {
// This line is used by the dev script build_all_commits.sh so needs to remain here
// This line also shouldn't be duplicated anywhere in this file, otherwise the script will break
// TODO: Find a better way to do this
const router = useRouter()
// DON'T MODIFY THIS FUNCTION
// It's used by the dev script build_all_commits.sh
// TODO: Find a better way to do this
const handleClick = () => {
router.push(`/${commit}${router.asPath}`)
}
return (
<Box
borderRadius="lg"
padding="3"
width="100%"
border={currentVersion == commit ? hoverBackgroundColor : ""}
borderWidth={currentVersion == commit ? "3px" : "1px"}
borderColor={currentVersion == commit ? "green" : ""}
cursor={currentVersion == commit ? "default" : "pointer"}
onClick={currentVersion == commit ? undefined : handleClick}
_hover={{
bg: currentVersion == commit ? "" : hoverBackgroundColor,
}}
>
{currentVersion == commit && (
<Text align={"center"} fontWeight="bold" borderRadius={"10px"} bg="green" mb={3}>
👀 Currently viewing
</Text>
)}
<Flex alignItems="center" pb={1} gap={3} justifyContent={"space-between"}>
<Flex alignItems="center">
<Box as="span" pr={2}>
<FontAwesomeIcon icon={faCodeCommit} />
</Box>
<Code fontWeight="bold">{commit}</Code>
</Flex>
<DiffStats diff={diff} />
</Flex>
<Text fontWeight="bold" fontSize="lg" pb={1}>
{message}
</Text>
<Flex justifyContent={"space-between"}>
<Text fontSize="sm" color="gray.500">
{date.split(" (")[0]}
<br />({date.split(" (")[1].slice(0, -1)})
</Text>
<Text fontSize="sm" color="gray.500">
{author}
</Text>
</Flex>
</Box>
)
}
const DiffStats = ({ diff }) => {
const regex = /(\d+)\sfiles\schanged,\s(\d+)\sinsertions\(\+\),\s(\d+)\sdeletions\(-\)/
const match = diff.match(regex)
const filesChanged = match ? parseInt(match[1], 10) : 0
const insertions = match ? parseInt(match[2], 10) : 0
const deletions = match ? parseInt(match[3], 10) : 0
return (
<HStack spacing={3}>
<HStack>
<Text fontSize="small">{filesChanged}</Text>
<FontAwesomeIcon icon={faFile} size={"sm"} />
</HStack>
<HStack>
<Text fontSize="small" color="green.500">
{insertions}
</Text>
<FontAwesomeIcon icon={faCirclePlus} size={"sm"} color="green" />
</HStack>
<HStack>
<Text fontSize="small" color="red.500">
{deletions}
</Text>
<FontAwesomeIcon icon={faCircleMinus} size={"sm"} color="red" />
</HStack>
</HStack>
)
}
export default function VersionDrawer({ windowSize }) {
const isSSR = typeof window === "undefined"
const { isOpen, onOpen, onClose } = useDisclosure()
const btnRef = React.useRef()
const router_VersionDrawer = useRouter()
useEffect(() => {
const handleRouteChange = (url, { shallow }) => {
if (!shallow) {
window.location.reload()
}
}
router_VersionDrawer.events.on("routeChangeComplete", handleRouteChange)
return () => {
router_VersionDrawer.events.off("routeChangeComplete", handleRouteChange)
}
}, [router_VersionDrawer.events])
const [commitHashes, setCommitHashes] = useState([])
const [currentVersion, setCurrentVersion] = useState("latest")
const searchInputRef = useRef(null)
const [showSearch, setShowSearch] = useState(() => {
if (!isSSR) {
return window.localStorage.getItem("commitSearch") ? true : false
}
return ""
})
const [searchText, setSearchText] = useState(() => {
if (!isSSR) {
return window.localStorage.getItem("commitSearch") || ""
}
return ""
})
useEffect(() => {
if (showSearch) {
searchInputRef.current?.focus()
}
}, [showSearch])
const hoverBackgroundColor = useColorModeValue("gray.100", "gray.700")
const buttonBackgroundColorLatest = useColorModeValue("gray.100", "#1B2236")
const buttonBackgroundHoverLatest = useColorModeValue("gray.200", "gray.700")
const buttonBackgroundColorActive = useColorModeValue("green.300", "green.500")
const buttonBackgroundHoverActive = useColorModeValue("green.400", "green.600")
useEffect(() => {
const fetchData = async () => {
const response = await fetch("/api/commits")
const data = await response.json()
setCommitHashes(data)
const pathMatch = router_VersionDrawer.asPath.substring(1, 8)
// If the pathMatch is empty, then you're on the latest version
// The regular expression is needed in case there is additional params in the URL
if (!pathMatch || !/^[a-f0-9]{7}/.test(pathMatch)) {
setCurrentVersion("latest")
} else {
setCurrentVersion(pathMatch)
}
}
fetchData()
}, [router_VersionDrawer.asPath])
useEffect(() => {
if (!isSSR) {
if (searchText) {
window.localStorage.setItem("commitSearch", searchText)
} else {
window.localStorage.removeItem("commitSearch")
}
}
}, [searchText, isSSR])
return (
<>
<Box
as="button"
position="fixed"
bottom="0"
right="0"
width="50px"
height={windowSize.width > 1500 ? "100vh" : "50px"}
borderTopLeftRadius={windowSize.width > 1500 ? "30px" : "20px"}
borderBottomLeftRadius={windowSize.width > 1500 ? "30px" : "0px"}
bg={currentVersion == "latest" ? buttonBackgroundColorLatest : buttonBackgroundColorActive}
_hover={{
backgroundColor: currentVersion == "latest" ? buttonBackgroundHoverLatest : buttonBackgroundHoverActive,
cursor: "pointer",
}}
onClick={onOpen}
zIndex="modal"
>
<FontAwesomeIcon icon={faClockRotateLeft} size={"xl"} />
</Box>
<Drawer isOpen={isOpen} placement="right" onClose={onClose} finalFocusRef={btnRef}>
<DrawerOverlay />
<DrawerContent bg={useColorModeValue("white", "#131827")} borderLeftRadius="30px" minW="320px">
<DrawerCloseButton mt={2} mr={2} />
<DrawerHeader>
<Flex>
<Code px={3} cursor={"default"} fontSize={"xl"} borderRadius={6}>
Select commit
</Code>
<Box
borderWidth="1px"
borderRadius="lg"
px={3}
cursor="pointer"
marginLeft={3}
_hover={{
bg: useColorModeValue("gray.100", "gray.700"),
}}
onClick={() => {
setSearchText("")
setShowSearch(!showSearch)
}}
>
<FontAwesomeIcon icon={faMagnifyingGlass} size="sm" />
</Box>
</Flex>
<Collapse in={Boolean(showSearch)}>
<InputGroup mt={3} borderRadius="lg">
<Input
ref={searchInputRef}
placeholder="Search commit messages..."
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
/>
{searchText && (
<InputRightElement>
<IconButton
icon={<FontAwesomeIcon icon={faTimesCircle} />}
variant="ghost"
onClick={() => setSearchText("")}
aria-label="Clear search"
/>
</InputRightElement>
)}
</InputGroup>
</Collapse>
</DrawerHeader>
<DrawerBody>
<VStack spacing={4}>
<Box
borderRadius="lg"
paddingX="3"
paddingY="2"
border={currentVersion == "latest" ? hoverBackgroundColor : ""}
borderWidth={currentVersion == "latest" ? "3px" : "1px"}
borderColor={currentVersion == "latest" ? "green" : ""}
cursor={currentVersion == "latest" ? "default" : "pointer"}
width="100%"
_hover={{
bg: currentVersion == "latest" ? "" : hoverBackgroundColor,
}}
onClick={async () => {
if (currentVersion !== "latest") {
const pathAfterCommit = router_VersionDrawer.asPath.substring(8)
await router_VersionDrawer.push(`${pathAfterCommit}`)
router_VersionDrawer.reload()
}
}}
>
<Text fontWeight="bold" fontSize="lg">
{currentVersion == "latest" ? "👀 \u00A0 Viewing latest version" : "⭐️ \u00A0 Latest version"}
</Text>
</Box>
{commitHashes
.filter(({ message }) => message.toLowerCase().includes(searchText.toLowerCase()))
.map(({ hash, message, date, author, diff }) => (
<CommitCard
key={hash}
commit={hash}
message={message}
date={date}
author={author}
diff={diff}
currentVersion={currentVersion}
hoverBackgroundColor={hoverBackgroundColor}
/>
))}
{commitHashes.toString() &&
commitHashes.filter(({ message }) => message.toLowerCase().includes(searchText.toLowerCase())).toString() == "" && (
<VStack spacing={0}>
<Text fontSize={"xx-large"}>🧐</Text>
<Text>No commits found for search query</Text>
</VStack>
)}
{!commitHashes.toString() && (
<VStack spacing={5}>
<VStack spacing={0}>
<Text fontSize={"xx-large"}>🚧</Text>
<Text>No commits returned from API</Text>
</VStack>
<Alert status="info" flexDirection="column" alignItems="flex-start" borderRadius={15}>
<HStack pb={4}>
<AlertIcon />
<AlertTitle fontSize="lg">Server Info</AlertTitle>
</HStack>
<AlertDescription>
<Text>
Run <Code>yarn commits</Code> to build all commit versions before starting the dev server.
</Text>
<Text pt={3}>
Run <Code>yarn dev-commits</Code> to build all commit versions and automatically start the dev server.
</Text>
</AlertDescription>
</Alert>
</VStack>
)}
</VStack>
</DrawerBody>
</DrawerContent>
</Drawer>
</>
)
}