-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogo.tsx
48 lines (38 loc) · 1.09 KB
/
Logo.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
import { Avatar } from 'antd';
import { observable } from 'mobx';
import { observer } from 'mobx-react';
import { Component } from 'react';
export interface GitLogoProps {
name: string;
}
@observer
export class GitLogo extends Component<GitLogoProps> {
@observable
accessor path = '';
async componentDidMount() {
const { name } = this.props;
const topic = name.toLowerCase();
try {
const { src } = await this.loadImage(
`https://raw.githubusercontent.com/github/explore/master/topics/${topic}/${topic}.png`
);
this.path = src;
} catch {
const { src } = await this.loadImage(`https://github.com/${name}.png`);
this.path = src;
}
}
loadImage(path: string) {
return new Promise<HTMLImageElement>((resolve, reject) => {
const image = new globalThis.Image();
image.onload = () => resolve(image);
image.onerror = reject;
image.src = path;
});
}
render() {
const { path } = this;
const { name } = this.props;
return path && <Avatar shape="square" size="large" src={path} alt={name} />;
}
}