EndpointLogos.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from "react";
  15. import { observer } from "mobx-react";
  16. import styled, { css } from "styled-components";
  17. import configLoader from "@src/utils/Config";
  18. import Generic from "./resources/Generic";
  19. const Wrapper = styled.div<any>``;
  20. const Logo = styled.div<any>`
  21. display: flex;
  22. align-items: center;
  23. justify-content: center;
  24. width: ${props => props.width}px;
  25. height: ${props => props.height}px;
  26. ${(props: any) =>
  27. props.url
  28. ? css`
  29. background: url("${props.url}") no-repeat center;
  30. `
  31. : ""}
  32. background-size: contain;
  33. `;
  34. const widthHeights = [
  35. { w: 80, h: 32 },
  36. { w: 105, h: 42 },
  37. { w: 185, h: 128 },
  38. { w: 185, h: 64 },
  39. ];
  40. type Props = {
  41. endpoint?: string | null;
  42. height: number;
  43. disabled?: boolean;
  44. white?: boolean;
  45. baseUrl?: string;
  46. onClick?: () => void;
  47. style?: React.CSSProperties;
  48. };
  49. @observer
  50. class EndpointLogos extends React.Component<Props> {
  51. static defaultProps: Props = {
  52. height: 64,
  53. };
  54. renderGenericLogo(size: { w: number; h: number }) {
  55. return (
  56. <Generic
  57. size={size}
  58. name={this.props.endpoint || ""}
  59. disabled={this.props.disabled}
  60. white={this.props.white}
  61. />
  62. );
  63. }
  64. render() {
  65. const size = widthHeights.find(wh => wh.h === this.props.height);
  66. if (!size) {
  67. return null;
  68. }
  69. let imageUrl: string | null = null;
  70. const provider = this.props.endpoint;
  71. if (
  72. provider &&
  73. Object.keys(configLoader.config.providerNames).indexOf(provider) > -1
  74. ) {
  75. imageUrl = `${this.props.baseUrl || ""}/api/logos/${provider}/${size.h}`;
  76. const style = this.props.white
  77. ? "white"
  78. : this.props.disabled
  79. ? "disabled"
  80. : null;
  81. imageUrl = style ? `${imageUrl}/${style}` : imageUrl;
  82. }
  83. return (
  84. <Wrapper {...this.props}>
  85. <Logo width={size.w} height={size.h} url={imageUrl}>
  86. {imageUrl ? null : this.renderGenericLogo(size)}
  87. </Logo>
  88. </Wrapper>
  89. );
  90. }
  91. }
  92. export default EndpointLogos;