forked from JoelSutherland/GitHub-jQuery-Repo-Widget
/
EventFlow.tsx
145 lines (124 loc) · 3.9 KB
/
EventFlow.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
import { LinkHeader } from 'koajax';
import { observable } from 'mobx';
import { WebCellProps, attribute, component, observer } from 'web-cell';
import * as style from './common.module.less';
import { Event, client, getEvents } from './service';
export interface GithubEventsProps extends WebCellProps {
user?: string;
organization?: string;
repository?: string;
}
@component({ tagName: 'github-events' })
@observer
export class GithubEvents extends HTMLElement {
declare props: GithubEventsProps;
@attribute
@observable
accessor user = '';
@attribute
@observable
accessor organization = 'EasyWebApp';
@attribute
@observable
accessor repository = '';
@observable
accessor end = false;
@observable
accessor list: Event[] = [];
private loading = false;
private nextPage = '';
async loadPage() {
this.loading = true;
const { headers, body } = await (!this.nextPage
? getEvents(this)
: client.get<Event[]>(this.nextPage));
const { next } = headers.Link as LinkHeader;
if (next) this.nextPage = next.URI;
else this.end = true;
this.list = [...this.list, ...body];
this.loading = false;
}
loadMore = (bottom: HTMLElement) =>
new IntersectionObserver((_, observer) => {
if (!this.end) {
if (!this.loading) this.loadPage();
} else observer.disconnect();
}).observe(bottom);
renderPayload = ({
ref,
master_branch,
issue,
pull_request,
release,
member,
pages
}: Event['payload']) => (
<>
<a
target="_blank"
href={(issue || pull_request || release || member)?.html_url}
>
{ref ||
master_branch ||
(issue || pull_request)?.title ||
release?.name ||
member?.login}
</a>
<ol>
{pages?.map(({ action, html_url, summary, sha, title }) => (
<li>
{action}
<a
target="_blank"
href={html_url}
title={summary || sha}
>
{title}
</a>
</li>
))}
</ol>
</>
);
renderEvent = ({ actor, repo, created_at, payload, type }: Event) => (
<li key={created_at} className="d-flex align-items-center my-3">
<a
className="text-center w-25"
target="_blank"
href={`https://github.com/${actor.login}`}
title={actor.login}
>
<img
className={`${style.logo} ${style.big}`}
src={actor.avatar_url}
/>
<div>{actor.display_login}</div>
</a>
<div>
<h4>
<a target="_blank" href={`https://github.com/${repo.name}`}>
{repo.name}
</a>
</h4>
<time className="pe-1" dateTime={created_at}>
{new Date(created_at).toLocaleString()}
</time>
<strong className="pe-1">
{payload.action || type.replace('Event', '')}
</strong>
{this.renderPayload(payload)}
</div>
</li>
);
render() {
const { list } = this;
return (
<div>
<ul className="list-unstyled">{list.map(this.renderEvent)}</ul>
<p className="text-center" ref={this.loadMore}>
{this.end ? 'No more' : 'Loading'}
</p>
</div>
);
}
}