-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogo.tsx
More file actions
50 lines (40 loc) · 1.14 KB
/
Logo.tsx
File metadata and controls
50 lines (40 loc) · 1.14 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
import { CardMedia } from '@mui/material';
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 && <CardMedia sx={{ height: 32, width: 32 }} component="img" image={path} alt={name} />
);
}
}