integrationIcon.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import PluginIcon, {DEFAULT_ICON, ICON_PATHS} from 'sentry/plugins/components/pluginIcon';
  4. import {Integration} from 'sentry/types';
  5. type Props = {
  6. integration: Integration;
  7. size?: number;
  8. };
  9. type State = {
  10. imgSrc: Integration['icon'];
  11. };
  12. type IconProps = Pick<Props, 'size'>;
  13. const StyledIcon = styled('img')<IconProps>`
  14. height: ${p => p.size}px;
  15. width: ${p => p.size}px;
  16. border-radius: 2px;
  17. display: block;
  18. `;
  19. class Icon extends Component<Props, State> {
  20. state: State = {
  21. imgSrc: this.props.integration.icon,
  22. };
  23. render() {
  24. const {integration, size} = this.props;
  25. return (
  26. <StyledIcon
  27. size={size}
  28. src={this.state.imgSrc}
  29. onError={() => {
  30. this.setState({imgSrc: ICON_PATHS[integration.provider.key] || DEFAULT_ICON});
  31. }}
  32. />
  33. );
  34. }
  35. }
  36. const IntegrationIcon = ({integration, size = 32}: Props) =>
  37. integration.icon ? (
  38. <Icon size={size} integration={integration} />
  39. ) : (
  40. <PluginIcon size={size} pluginId={integration.provider.key} />
  41. );
  42. export default IntegrationIcon;