]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11321 Fallback to generic avatar if the image doesn't load
authorGrégoire Aubert <gregoire.aubert@sonarsource.com>
Tue, 6 Nov 2018 10:43:15 +0000 (11:43 +0100)
committerSonarTech <sonartech@sonarsource.com>
Fri, 16 Nov 2018 19:21:06 +0000 (20:21 +0100)
server/sonar-web/src/main/js/components/common/OrganizationAvatar.tsx

index c4ad360e7295b38c2199896b9d526fdca34c34fb..bda1c6774a04db37164f7b774ad7a8a3b5a749ef 100644 (file)
 import * as React from 'react';
 import * as classNames from 'classnames';
 import GenericAvatar from '../ui/GenericAvatar';
+import { OrganizationBase } from '../../app/types';
 import './OrganizationAvatar.css';
 
 interface Props {
   className?: string;
-  organization: {
-    avatar?: string;
-    name: string;
-  };
+  organization: Pick<OrganizationBase, 'avatar' | 'name'>;
   small?: boolean;
 }
 
-export default function OrganizationAvatar({ className, organization, small }: Props) {
-  return (
-    <div
-      className={classNames(
-        'navbar-context-avatar',
-        'rounded',
-        { 'no-border': !organization.avatar, 'is-small': small },
-        className
-      )}>
-      {organization.avatar ? (
-        <img alt={organization.name} className="rounded" src={organization.avatar} />
-      ) : (
-        <GenericAvatar name={organization.name} size={small ? 15 : 30} />
-      )}
-    </div>
-  );
+interface State {
+  imgLoadError: boolean;
+}
+
+export default class OrganizationAvatar extends React.PureComponent<Props, State> {
+  state = { imgLoadError: false };
+
+  handleImgError = () => {
+    this.setState({ imgLoadError: true });
+  };
+
+  render() {
+    const { className, organization, small } = this.props;
+    const { imgLoadError } = this.state;
+    return (
+      <div
+        className={classNames(
+          'navbar-context-avatar',
+          'rounded',
+          { 'no-border': !organization.avatar, 'is-small': small },
+          className
+        )}>
+        {organization.avatar && !imgLoadError ? (
+          <img
+            alt={organization.name}
+            className="rounded"
+            onError={this.handleImgError}
+            src={organization.avatar}
+          />
+        ) : (
+          <GenericAvatar name={organization.name} size={small ? 15 : 30} />
+        )}
+      </div>
+    );
+  }
 }