aboutsummaryrefslogtreecommitdiff
path: root/src/components/GithubAvatar/GithubAvatar.tsx
blob: 5daaac182fcbdf141af9e9b1b495afa33c8aacd2 (plain)
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
import React from 'react';

import {
  Avatar,
  Link,
} from '@material-ui/core';

interface PropTypes {
  username: string;
}

const githubUrl = 'https://github.com/';
const getUserGithubUrl = (username: string) => githubUrl + username;

const GithubAvatar: React.FC<PropTypes> = ({ username }) => {
  if (username === 'anonymous') return <Avatar/>;

  const userGithubUrl = getUserGithubUrl(username);
  const avatarUrl = userGithubUrl + '.png';
  const usernameTokens = username.split(/[ ,.\-_#@;]/g);
  const altText = (
    (usernameTokens.length > 1)?
      (usernameTokens[0][0] + usernameTokens[1][0])
    :
      usernameTokens[0][0]
  ).toUpperCase()

  return (
    <Link href={userGithubUrl}>
      <Avatar>
        <img src={avatarUrl} alt={altText} />
      </Avatar>
    </Link>
  )
};

export { getUserGithubUrl };
export default GithubAvatar;