forked from JoelSutherland/GitHub-jQuery-Repo-Widget
/
index.tsx
144 lines (128 loc) · 4.52 KB
/
index.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
import { observable } from 'mobx';
import { WebCellProps, attribute, component, observer } from 'web-cell';
import { Owner, Repository, getOwner, getRepositories } from '../service';
import * as style from './index.module.less';
export interface GithubProfileProps extends WebCellProps {
user?: string;
organization?: string;
}
@component({ tagName: 'github-profile' })
@observer
export class GithubProfile extends HTMLElement {
declare props: GithubProfileProps;
@attribute
@observable
accessor user = '';
@attribute
@observable
accessor organization = '';
@observable
accessor currentOwner = {
login: '',
name: '',
avatar_url: '',
html_url: '',
followers: 0,
updated_at: '',
languages: [],
repositories: [] as Repository[]
} as Partial<Owner & { languages: string[]; repositories: Repository[] }>;
async connectedCallback() {
const { user, organization } = this;
const owner = await (user
? getOwner('user', user)
: getOwner('organization', organization)),
sort = 'updated';
let list = await (user
? getRepositories('user', user, { sort })
: getRepositories('organization', organization, { sort }));
list = list.filter(({ fork }) => !fork);
const repositories = list
.filter(({ stargazers_count }) => stargazers_count)
.sort((A, B) => B.stargazers_count - A.stargazers_count)
.slice(0, 5),
languages = [
...new Set(list.map(({ language }) => language))
].filter(Boolean);
this.currentOwner = { ...owner, languages, repositories };
}
renderRepository = ({
html_url,
description,
name,
updated_at,
stargazers_count
}) => (
<a
key={html_url}
className={style['profile-repos']}
target="_blank"
href={html_url}
title={description}
>
<span className="d-flex justify-content-between">
<span className={style['repos-name']}>{name}</span>
<span className={style['repos-star']}>{stargazers_count}</span>
</span>
<time className={style['repos-updated']} dateTime={updated_at}>
Updated:
{new Date(updated_at).toLocaleString()}
</time>
</a>
);
render() {
const {
avatar_url,
html_url,
name,
login,
followers,
languages,
repositories
} = this.currentOwner;
return (
<div className={style['gh-profile-card']}>
<header>
<header className="d-flex justify-content-between">
<img
className={style['profile-avatar']}
src={avatar_url}
/>
<div>
<a
className={style['profile-name']}
target="_blank"
href={html_url}
>
{name}
</a>
<div className="d-flex justify-content-between mt-1">
<a
className={style['profile-follow']}
target="_blank"
href={html_url}
>
Follow @{login}
</a>
<span className={style['profile-followers']}>
{followers}
</span>
</div>
</div>
</header>
<ul className={style['profile-languages']}>
{languages.map(name => (
<li key={name}>{name}</li>
))}
</ul>
</header>
<section>
<header className={style['repos-header']}>
Most popular original repositories
</header>
{repositories.map(this.renderRepository)}
</section>
</div>
);
}
}