File size: 3,315 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// @flow
import * as React from 'react';
import { Query } from 'react-apollo';
import {
  getUserByUsernameQuery,
  type GetUserType,
} from 'shared/graphql/queries/user/getUser';
import { UserHoverProfile } from 'src/components/hoverProfile';
import AvatarImage from './image';
import { Container, AvatarLink } from './style';
import ConditionalWrap from 'src/components/conditionalWrap';

type HandlerProps = {
  user?: GetUserType,
  username?: string,
  size?: number,
  mobilesize?: number,
  style?: Object,
  showHoverProfile?: boolean,
  isClickable?: boolean,
  dataCy?: string,
  onlineBorderColor?: ?Function,
};

type AvatarProps = {
  ...$Exact<HandlerProps>,
  user: GetUserType,
};

const GetUserByUsername = (props: HandlerProps) => {
  const { username, showHoverProfile = true } = props;
  return (
    <Query variables={{ username }} query={getUserByUsernameQuery}>
      {({ data }) => {
        if (!data || !data.user) return null;
        return (
          <ConditionalWrap
            condition={showHoverProfile}
            wrap={() => (
              <UserHoverProfile username={props.username}>
                <Avatar user={data.user} {...props} />
              </UserHoverProfile>
            )}
          >
            <Avatar user={data.user} {...props} />
          </ConditionalWrap>
        );
      }}
    </Query>
  );
};

class Avatar extends React.Component<AvatarProps> {
  render() {
    const {
      user,
      dataCy,
      size = 32,
      mobilesize,
      style,
      isClickable = true,
    } = this.props;

    const src = user.profilePhoto;

    const userFallback = '/img/default_avatar.svg';
    const source = [src, userFallback];

    return (
      <Container
        style={style}
        type={'user'}
        data-cy={dataCy}
        size={size}
        mobileSize={mobilesize}
      >
        <ConditionalWrap
          condition={!!user.username && isClickable}
          wrap={() => (
            <AvatarLink to={`/users/${user.username}`}>
              <AvatarImage
                src={source}
                size={size}
                mobilesize={mobilesize}
                type={'user'}
                alt={user.name || user.username}
              />
            </AvatarLink>
          )}
        >
          <AvatarImage
            src={source}
            size={size}
            mobilesize={mobilesize}
            type={'user'}
            alt={user.name || user.username}
          />
        </ConditionalWrap>
      </Container>
    );
  }
}

class AvatarHandler extends React.Component<HandlerProps> {
  render() {
    const { showHoverProfile = true, isClickable } = this.props;

    if (this.props.user) {
      const user = this.props.user;
      return (
        <ConditionalWrap
          condition={showHoverProfile}
          wrap={() => (
            <UserHoverProfile username={user.username}>
              <Avatar {...this.props} />
            </UserHoverProfile>
          )}
        >
          <Avatar {...this.props} />
        </ConditionalWrap>
      );
    }

    if (!this.props.user && this.props.username) {
      return (
        <GetUserByUsername
          username={this.props.username}
          isClickable={isClickable}
        />
      );
    }

    return null;
  }
}

export default AvatarHandler;