integrationIcon.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 type {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 || undefined}
  29. onError={() => {
  30. this.setState({imgSrc: ICON_PATHS[integration.provider.key] || DEFAULT_ICON});
  31. }}
  32. />
  33. );
  34. }
  35. }
  36. function IntegrationIcon({integration, size = 32}: Props) {
  37. return integration.icon ? (
  38. <Icon size={size} integration={integration} />
  39. ) : (
  40. <PluginIcon size={size} pluginId={integration.provider.key} />
  41. );
  42. }
  43. export default IntegrationIcon;