This repository was archived by the owner on Mar 24, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub.tsx
More file actions
205 lines (200 loc) · 6.85 KB
/
github.tsx
File metadata and controls
205 lines (200 loc) · 6.85 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
203
204
205
import React from 'react';
import { cn } from '@/lib/utils';
import {
GitPullRequest,
GitCommit,
GitFork,
CircleDot,
GitBranch,
} from 'lucide-react';
import { Avatar, AvatarImage } from '@/components/ui/avatar';
interface GitHubLinkProps extends React.HTMLAttributes<HTMLAnchorElement> {
type: 'issue' | 'pr' | 'commit' | 'user' | 'repo' | 'org' | 'branch';
reference: string;
repo?: string;
className?: string;
}
export function GitHubLink({
type,
reference,
repo,
className,
...props
}: GitHubLinkProps) {
let href = '';
let icon = null;
let label = '';
let displayText: string | React.ReactNode = reference;
let avatar = null;
let shortReference = '';
const username = reference.replace('@', '');
switch (type) {
case 'issue':
href = `https://github.com/${repo || 'allthingslinux/allthingslinux'}/issues/${reference.replace('#', '')}`;
icon = <CircleDot className="h-4 w-4 text-blue-400" />;
if (repo) {
const [org, repoName] = repo.split('/');
displayText = (
<span className="flex flex-wrap items-center gap-1.5">
<span className="text-blue-300 text-xs sm:text-sm">{org}</span>
<span className="text-muted-foreground">/</span>
<span className="text-blue-300 text-xs sm:text-sm">{repoName}</span>
<span className="text-muted-foreground">#</span>
<span className="text-blue-400 font-mono text-xs sm:text-sm">
{reference.replace('#', '')}
</span>
</span>
);
} else {
displayText = (
<span className="text-blue-400 font-mono text-xs sm:text-sm">
{reference}
</span>
);
}
break;
case 'pr':
href = `https://github.com/${repo || 'allthingslinux/allthingslinux'}/pull/${reference.replace('#', '')}`;
icon = <GitPullRequest className="h-4 w-4 text-blue-400" />;
if (repo) {
const [org, repoName] = repo.split('/');
displayText = (
<span className="flex flex-wrap items-center gap-1.5">
<span className="text-blue-300 text-xs sm:text-sm">{org}</span>
<span className="text-muted-foreground">/</span>
<span className="text-blue-300 text-xs sm:text-sm">{repoName}</span>
<span className="text-muted-foreground">#</span>
<span className="text-blue-400 font-mono text-xs sm:text-sm">
{reference.replace('#', '')}
</span>
</span>
);
} else {
displayText = (
<span className="text-blue-400 font-mono text-xs sm:text-sm">
{reference}
</span>
);
}
break;
case 'branch':
href = `https://github.com/${repo || 'allthingslinux/allthingslinux'}/tree/${reference}`;
icon = <GitBranch className="h-4 w-4 text-purple-400" />;
if (repo) {
const [org, repoName] = repo.split('/');
displayText = (
<span className="flex flex-wrap items-center gap-1.5">
{repo !== 'allthingslinux/allthingslinux' ? (
<>
<span className="text-blue-300 text-xs sm:text-sm">{org}</span>
<span className="text-muted-foreground">/</span>
<span className="text-blue-300 text-xs sm:text-sm">
{repoName}
</span>
<span className="text-muted-foreground">:</span>
</>
) : (
<span className="hidden sm:inline-flex sm:items-center">
<span className="text-blue-300 text-sm">{org}</span>
<span className="text-muted-foreground">/</span>
<span className="text-blue-300 text-sm">{repoName}</span>
<span className="text-muted-foreground">:</span>
</span>
)}
<span className="text-purple-400 text-xs sm:text-sm">
{reference}
</span>
</span>
);
} else {
displayText = (
<span className="text-purple-400 text-xs sm:text-sm">
{reference}
</span>
);
}
break;
case 'commit':
shortReference = reference.slice(0, 7);
href = `https://github.com/${repo || 'allthingslinux/allthingslinux'}/commit/${reference}`;
icon = <GitCommit className="h-4 w-4 text-green-400" />;
if (repo) {
const [org, repoName] = repo.split('/');
displayText = (
<span className="flex flex-wrap items-center gap-1.5">
<span className="text-blue-300 text-xs sm:text-sm">{org}</span>
<span className="text-muted-foreground">/</span>
<span className="text-blue-300 text-xs sm:text-sm">{repoName}</span>
<span className="text-muted-foreground">@</span>
<span className="text-green-400 text-xs sm:text-sm">
{shortReference}
</span>
</span>
);
} else {
displayText = (
<span className="text-green-400 text-xs sm:text-sm">
{shortReference}
</span>
);
}
break;
case 'user':
case 'org':
href = `https://github.com/${username}`;
avatar = (
<Avatar className="h-4 w-4 sm:h-5 sm:w-5">
<AvatarImage
src={`https://github.com/${username}.png`}
alt={username}
/>
</Avatar>
);
displayText = <span className="text-xs sm:text-sm">@{username}</span>;
break;
case 'repo':
href = `https://github.com/${reference}`;
icon = <GitFork className="h-4 w-4" />;
label = 'Repository';
break;
}
return (
<a
href={href}
target="_blank"
rel="noopener noreferrer"
className={cn(
'inline-flex flex-wrap items-center gap-1.5 sm:gap-2 rounded-md px-1.5 sm:px-2 py-0.5 sm:py-1 text-xs sm:text-sm font-medium',
type === 'commit' && 'font-mono bg-green-500/10 hover:bg-green-500/20',
type === 'branch' &&
'font-mono bg-purple-500/10 hover:bg-purple-500/20',
['issue', 'pr'].includes(type) &&
'font-mono bg-blue-500/10 hover:bg-blue-500/20',
!['issue', 'pr', 'commit', 'branch'].includes(type) &&
'bg-blue-500/10 hover:bg-blue-500/20',
className
)}
{...props}
>
<span className="flex items-center gap-1">
{avatar || icon}
{!['user', 'org', 'commit', 'branch', 'issue', 'pr'].includes(type) && (
<span className="text-blue-300 text-xs sm:text-sm">{label}</span>
)}
</span>
{typeof displayText === 'string' ? (
<span
className={cn(
'text-blue-400 text-xs sm:text-sm',
type === 'commit' && 'text-green-400',
type === 'branch' && 'text-purple-400'
)}
>
{displayText}
</span>
) : (
displayText
)}
</a>
);
}