-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathPackageSidebar.tsx
242 lines (234 loc) · 7.19 KB
/
PackageSidebar.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
import { Copy } from '@datacamp/waffles/icon';
import { Select } from '@datacamp/waffles/select';
import { useToast } from '@datacamp/waffles/toast';
import { format } from 'date-fns';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { FaGithub, FaHome, FaUser } from 'react-icons/fa';
import useHasCollaborator from '../helpers/hooks/useHasCollaborator';
import { copyTextToClipboard } from '../lib/utils';
import MonthlyDownloadsChart from './MonthlyDownloadsChart';
import SidebarHeader from './SidebarHeader';
import SidebarValue from './SidebarValue';
type PackageSidebarProps = {
downloadsLastMonth: number;
githubUrl: string;
homeUrl: string;
lastPublished: Date;
license: string;
linkToCurrentVersion: string;
maintainer: {
api_uri: string;
created_at: string;
email: string;
gravatar_url: string;
id: number;
name: string;
updated_at: string;
uri: string;
};
monthlyDownloads: Array<{ downloads: number; month: string }>;
packageName: string;
repository: {
forks: number;
issues: number;
pullRequests: number;
stars: number;
};
showInstallCode: boolean;
version: string;
versionsArray: string[];
};
export default function PackageSidebar({
downloadsLastMonth,
githubUrl,
homeUrl,
lastPublished,
license,
linkToCurrentVersion,
maintainer,
monthlyDownloads,
packageName,
repository,
showInstallCode,
version,
versionsArray,
}: PackageSidebarProps) {
const router = useRouter();
const { toast } = useToast();
function handleCopyLink() {
copyTextToClipboard(linkToCurrentVersion);
toast({ title: 'Copied to clipboard!', variant: 'success' });
}
function handleChangeVersion(event: React.ChangeEvent<HTMLSelectElement>) {
const selectedVersion = event.target.value;
router.push(`/packages/${packageName}/versions/${selectedVersion}`);
}
const availableAuthor = useHasCollaborator(maintainer.name);
return (
<div className="space-y-6">
<div>
<SidebarHeader>Copy Link</SidebarHeader>
<div className="relative mt-4 dark:text-dc-navy">
<button
aria-label="copy link to clipboard"
className="absolute inset-y-0 left-0 flex items-center justify-center w-10 pl-2"
onClick={handleCopyLink}
type="button"
>
<Copy size={'medium'} />
</button>
<label className="sr-only" htmlFor="linkToCurrentVersion">
Link to current version
</label>
<input
className="block w-full pl-10 text-gray-500 border-2 rounded-md border-dc-grey300"
id="linkToCurrentVersion"
readOnly
type="text"
value={linkToCurrentVersion}
/>
</div>
</div>
<div>
<SidebarHeader>Version</SidebarHeader>
<label className="sr-only" htmlFor="version">
Version
</label>
<Select
id="version"
name="version"
onChange={handleChangeVersion}
value={version}
>
{versionsArray.map((v) => (
<option key={v} value={v}>
{v}
</option>
))}
</Select>
</div>
{/* show install code if it's a CRAN package */}
{showInstallCode && (
<div>
<SidebarHeader>Install</SidebarHeader>
<div className="prose-sm prose sm:prose">
<pre>
<code>{`install.packages('${packageName}')`}</code>
</pre>
</div>
</div>
)}
{/* only show downloads data if we have 3+ months of it */}
{monthlyDownloads && monthlyDownloads.length >= 3 && (
<div>
<SidebarHeader>Monthly Downloads</SidebarHeader>
<div className="flex items-baseline justify-between">
<div className="text-3xl font-light">
{downloadsLastMonth.toLocaleString()}
</div>
<div className="w-3/5">
<MonthlyDownloadsChart monthlyDownloads={monthlyDownloads} />
</div>
</div>
</div>
)}
<div className="flex">
<div className="w-1/2">
<SidebarHeader>Version</SidebarHeader>
<SidebarValue>{version}</SidebarValue>
</div>
<div className="w-1/2">
<SidebarHeader>License</SidebarHeader>
<SidebarValue>{license}</SidebarValue>
</div>
</div>
{repository && (
<>
<div className="flex">
<div className="w-1/2">
<SidebarHeader>Issues</SidebarHeader>
<SidebarValue>
<a
href={`${githubUrl}/issues`}
rel="noopener noreferrer"
target="_blank"
>
{repository.issues.toLocaleString()}
</a>
</SidebarValue>
</div>
<div className="w-1/2">
<SidebarHeader>Pull Requests</SidebarHeader>
<SidebarValue>
<a
href={`${githubUrl}/pulls`}
rel="noopener noreferrer"
target="_blank"
>
{repository.pullRequests.toLocaleString()}
</a>
</SidebarValue>
</div>
</div>
<div className="flex">
<div className="w-1/2">
<SidebarHeader>Stars</SidebarHeader>
<SidebarValue>
<a href={githubUrl} rel="noopener noreferrer" target="_blank">
{repository.stars.toLocaleString()}
</a>
</SidebarValue>
</div>
<div className="w-1/2">
<SidebarHeader>Forks</SidebarHeader>
<SidebarValue>
<a href={githubUrl} rel="noopener noreferrer" target="_blank">
{repository.forks.toLocaleString()}
</a>
</SidebarValue>
</div>
</div>
<div>
<SidebarHeader>Repository</SidebarHeader>
<SidebarValue Icon={FaGithub}>
<a href={githubUrl} rel="noopener noreferrer" target="_blank">
{githubUrl}
</a>
</SidebarValue>
</div>
</>
)}
{homeUrl && (
<div>
<SidebarHeader>Homepage</SidebarHeader>
<SidebarValue Icon={FaHome}>
<a href={homeUrl} rel="noopener noreferrer" target="_blank">
{homeUrl}
</a>
</SidebarValue>
</div>
)}
<div className="flex">
<div className="w-1/2">
<SidebarHeader>Maintainer</SidebarHeader>
<SidebarValue Icon={FaUser}>
{availableAuthor ? (
<Link href={`/collaborators/name/${encodeURI(maintainer.name)}`}>
{maintainer.name}
</Link>
) : (
maintainer.name
)}
</SidebarValue>
</div>
{lastPublished && (
<div className="w-1/2">
<SidebarHeader>Last Published</SidebarHeader>
<SidebarValue>{format(lastPublished, 'PPP')}</SidebarValue>
</div>
)}
</div>
</div>
);
}