CurrentIdentity.js

 1import React from 'react';
 2import gql from 'graphql-tag';
 3import { Query } from 'react-apollo';
 4import Avatar from '@material-ui/core/Avatar';
 5import { makeStyles } from '@material-ui/styles';
 6
 7const useStyles = makeStyles(theme => ({
 8  displayName: {
 9    marginLeft: theme.spacing(2),
10  },
11}));
12
13const QUERY = gql`
14  {
15    defaultRepository {
16      userIdentity {
17        displayName
18        avatarUrl
19      }
20    }
21  }
22`;
23
24const CurrentIdentity = () => {
25  const classes = useStyles();
26  return (
27    <Query query={QUERY}>
28      {({ loading, error, data }) => {
29        if (error || loading || !data.defaultRepository.userIdentity)
30          return null;
31        const user = data.defaultRepository.userIdentity;
32        return (
33          <>
34            <Avatar src={user.avatarUrl}>
35              {user.displayName.charAt(0).toUpperCase()}
36            </Avatar>
37            <div className={classes.displayName}>{user.displayName}</div>
38          </>
39        );
40      }}
41    </Query>
42  );
43};
44
45export default CurrentIdentity;