-
Notifications
You must be signed in to change notification settings - Fork 16
/
repo-detail.tsx
306 lines (289 loc) · 10.3 KB
/
repo-detail.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
import React from "react";
import { RouteComponentProps } from "react-router-dom";
import { GoThreeBars, GoX } from "react-icons/go";
import { BsArrowRightShort } from "react-icons/bs";
import {
useDisclosureState,
Disclosure,
DisclosureContent,
} from "reakit/Disclosure";
import formatDistance from "date-fns/formatDistance";
import { useQueryParam, StringParam } from "use-query-params";
import toast, { Toaster } from "react-hot-toast";
import { ErrorState } from "./error-state";
import Bug from "../bug.svg";
import {
BookmarkIcon,
CommitIcon,
LinkExternalIcon,
RepoIcon,
} from "@primer/octicons-react";
import { Title } from "react-head";
import { useCommits, useGetFiles } from "../hooks";
import { Repo } from "../types";
import { JSONDetail } from "./json-detail-container";
import { parseFlatCommitMessage } from "../lib";
import { Picker } from "./picker";
import { FilePicker } from "./file-picker";
import { DisplayCommit } from "./display-commit";
import truncate from "lodash/truncate";
interface RepoDetailProps extends RouteComponentProps<Repo> {}
export function RepoDetail(props: RepoDetailProps) {
const { match } = props;
const { owner, name } = match.params;
const [filename, setFilename] = useQueryParam("filename", StringParam);
const [selectedSha, setSelectedSha] = useQueryParam("sha", StringParam);
const disclosure = useDisclosureState();
const {
data: files,
status: filesStatus,
error: filesError,
} = useGetFiles(
{ owner, name },
{
onSuccess: (data) => {
if (!data.length) return;
setFilename(filename || data[0], "replaceIn");
},
}
);
// Hook for fetching commits, once we've determined this is a Flat repo.
const { data: commits = [] } = useCommits(
{ owner, name, filename },
{
enabled: Boolean(filename),
onSuccess: (commits) => {
const mostRecentCommitSha = commits[0].sha;
if (commits.length > 0) {
if (selectedSha) {
if (commits.some((commit) => commit.sha === selectedSha)) {
// noop
} else {
toast.error(
"Hmm, we couldn't find a commit by that SHA. Reverting to the most recent commit.",
{
duration: 4000,
}
);
setSelectedSha(mostRecentCommitSha, "replaceIn");
}
} else {
setSelectedSha(mostRecentCommitSha, "replaceIn");
}
}
},
}
);
const repoUrl = `https://github.com/${owner}/${name}`;
const parsedCommit = selectedSha
? parseFlatCommitMessage(
commits?.find((commit) => commit.sha === selectedSha)?.commit.message ||
"",
filename || ""
)
: null;
const dataSource = parsedCommit?.file?.source;
const selectedShaIndex = commits.findIndex((d) => d.sha === selectedSha);
const selectedShaPrevious =
selectedShaIndex !== -1
? (commits[selectedShaIndex + 1] || {}).sha
: undefined;
const controls = (
<div className="lg:flex space-y-4 lg:space-y-0 lg:space-x-4">
<div className="space-y-2">
<p className="text-xs font-medium text-indigo-200">Repository</p>
<div className="font-mono text-sm text-white">
<a
className="hover:underline focus:underline bg-indigo-700 hover:bg-indigo-800 focus:bg-indigo-800 h-9 rounded text-white inline-flex items-center px-2 focus:outline-none focus:ring-2 focus:ring-indigo-400"
target="_blank"
rel="noopener noreferrer"
href={repoUrl}
>
<div className="flex items-center space-x-2">
<RepoIcon />
<div
className="overflow-ellipsis whitespace-nowrap overflow-hidden text-xs"
style={{
maxWidth: "min(30em, 100% - 1em)",
}}
>
{owner}/{name}
</div>
<div className="opacity-50">
<LinkExternalIcon />
</div>
</div>
</a>
</div>
</div>
{!!(files || []).length && (
<div className="space-y-2">
<p className="text-xs font-medium text-indigo-200">Data File</p>
<FilePicker
value={filename || ""}
placeholder="Select a file"
onChange={(newFilename) => {
setFilename(newFilename);
}}
items={files || []}
itemRenderer={(item) => (
<span className="font-mono text-xs">{item}</span>
)}
/>
</div>
)}
{Boolean(filename) && (
<div className="space-y-2">
<p className="text-xs font-medium text-indigo-200">Commit</p>
{commits && (
<Picker<string>
label="Choose a commit"
placeholder="Select a SHA"
onChange={setSelectedSha}
value={selectedSha || ""}
items={commits.map((commit) => commit.sha)}
disclosureClass="appearance-none bg-indigo-700 hover:bg-indigo-800 focus:bg-indigo-800 h-9 px-2 rounded text-white text-xs focus:outline-none focus:ring-2 focus:ring-indigo-400 w-full lg:max-w-md"
itemRenderer={(sha) => {
const commit = commits.find((commit) => commit.sha === sha);
return (
<div className="flex flex-col space-y-1 text-xs">
<DisplayCommit
message={commit?.commit.message}
author={commit?.commit.author?.email}
filename={filename}
/>
<div className="flex items-center space-x-2">
<div className="flex items-center space-x-2">
<p className="text-gray-600">
{formatDistance(
new Date(commit?.commit.author?.date || ""),
new Date(),
{ addSuffix: true }
)}
</p>
</div>
</div>
</div>
);
}}
selectedItemRenderer={(sha) => (
<div className="flex items-center space-x-2">
<CommitIcon size={16} />
<div className="flex-1 truncate">
<DisplayCommit
message={
commits.find((commit) => commit.sha === sha)?.commit
.message
}
author={
commits.find((commit) => commit.sha === sha)?.commit
.author?.email
}
filename={filename}
/>
</div>
</div>
)}
/>
)}
</div>
)}
{!!dataSource && (
<div className="space-y-2 min-w-0">
<p className="text-xs font-medium text-indigo-200">Data source</p>
<a
className="font-mono hover:underline focus:underline bg-indigo-700 hover:bg-indigo-800 focus:bg-indigo-800 h-9 rounded text-white flex items-center px-2 focus:outline-none focus:ring-2 focus:ring-indigo-400"
href={dataSource}
target="_blank"
rel="noopener noreferrer"
>
<div className="flex items-center space-x-2 min-w-0">
<div className="flex-none">
<BookmarkIcon />
</div>
<div className="overflow-ellipsis whitespace-nowrap overflow-hidden text-xs">
{dataSource}
</div>
<div className="opacity-50">
<LinkExternalIcon />
</div>
</div>
</a>
</div>
)}
</div>
);
return (
<React.Fragment>
<Title>
{owner}/{name} – Flat
</Title>
<Toaster position="bottom-left" />
<div className="p-4 bg-indigo-600">
<header className="flex justify-between items-center">
<div className="text-indigo-100 font-light text-sm">
<strong className="font-bold">Flat Viewer</strong> a simple tool for
exploring flat data files in GitHub repositories.
</div>
<Disclosure
{...disclosure}
className="rounded-none focus:outline-none focus:ring lg:hidden text-white text-xl"
>
{disclosure.visible ? <GoX /> : <GoThreeBars />}
</Disclosure>
</header>
<DisclosureContent
className="lg:hidden overflow-visible mt-4"
{...disclosure}
>
{controls}
</DisclosureContent>
<Disclosure
{...disclosure}
className="rounded-none focus:outline-none focus:ring lg:hidden text-white text-xl"
>
{!disclosure.visible && (
<div className="flex items-center lg:hidden mt-4 text-white text-xs">
<span className="">{truncate(`${owner}/${name}`)}</span>
{Boolean(filename) && (
<>
<span className="mx-1">
<BsArrowRightShort />
</span>{" "}
{truncate(filename || "")}
</>
)}
</div>
)}
</Disclosure>
<div className="controls mt-4">{controls}</div>
</div>
<React.Fragment>
{selectedSha && Boolean(filename) && filesStatus !== "error" && (
<JSONDetail
key={selectedSha}
filename={filename || ""}
owner={owner as string}
name={name as string}
previousSha={selectedShaPrevious}
sha={selectedSha}
/>
)}
</React.Fragment>
{match &&
!(files || []).length &&
filesStatus !== "loading" &&
!selectedSha && (
<ErrorState img={Bug} alt="Error icon">
{files
? "Hmm, we couldn't find any files in that repo"
: // @ts-ignore
filesError && filesError?.message === "Error: Rate limit exceeded"
? // @ts-ignore
filesError?.message
: "Hmm, are you sure that's a public GitHub repo?"}
</ErrorState>
)}
</React.Fragment>
);
}