Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BadgesModal.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import BadgeButton from './BadgeButton';
  22. import BadgeParams from './BadgeParams';
  23. import { BadgeType, BadgeOptions, getBadgeUrl } from './utils';
  24. import { Metric, BranchLike } from '../../../app/types';
  25. import CodeSnippet from '../../../components/common/CodeSnippet';
  26. import Modal from '../../../components/controls/Modal';
  27. import { getBranchLikeQuery } from '../../../helpers/branches';
  28. import { translate } from '../../../helpers/l10n';
  29. import './styles.css';
  30. import { Button, ResetButtonLink } from '../../../components/ui/buttons';
  31. interface Props {
  32. branchLike?: BranchLike;
  33. metrics: { [key: string]: Metric };
  34. onSonarCloud: boolean;
  35. project: string;
  36. qualifier: string;
  37. }
  38. interface State {
  39. open: boolean;
  40. selectedType: BadgeType;
  41. badgeOptions: BadgeOptions;
  42. }
  43. export default class BadgesModal extends React.PureComponent<Props, State> {
  44. state: State = {
  45. open: false,
  46. selectedType: BadgeType.measure,
  47. badgeOptions: { color: 'white', metric: 'alert_status' }
  48. };
  49. handleClose = () => {
  50. this.setState({ open: false });
  51. };
  52. handleOpen = () => {
  53. this.setState({ open: true });
  54. };
  55. handleSelectBadge = (selectedType: BadgeType) => {
  56. this.setState({ selectedType });
  57. };
  58. handleUpdateOptions = (options: Partial<BadgeOptions>) => {
  59. this.setState(state => ({ badgeOptions: { ...state.badgeOptions, ...options } }));
  60. };
  61. render() {
  62. const { branchLike, project, qualifier } = this.props;
  63. const { selectedType, badgeOptions } = this.state;
  64. const header = translate('overview.badges.title');
  65. const fullBadgeOptions = { project, ...badgeOptions, ...getBranchLikeQuery(branchLike) };
  66. const badges = this.props.onSonarCloud
  67. ? [BadgeType.measure, BadgeType.qualityGate, BadgeType.marketing]
  68. : [BadgeType.measure, BadgeType.qualityGate];
  69. return (
  70. <div className="overview-meta-card">
  71. <Button className="js-project-badges" onClick={this.handleOpen}>
  72. {translate('overview.badges.get_badge', qualifier)}
  73. </Button>
  74. {this.state.open && (
  75. <Modal contentLabel={header} onRequestClose={this.handleClose}>
  76. <header className="modal-head">
  77. <h2>{header}</h2>
  78. </header>
  79. <div className="modal-body">
  80. <p className="huge-spacer-bottom">
  81. {translate('overview.badges.description', qualifier)}
  82. </p>
  83. <div className="badges-list spacer-bottom">
  84. {badges.map(type => (
  85. <BadgeButton
  86. key={type}
  87. onClick={this.handleSelectBadge}
  88. selected={type === selectedType}
  89. type={type}
  90. url={getBadgeUrl(type, fullBadgeOptions)}
  91. />
  92. ))}
  93. </div>
  94. <p className="text-center note huge-spacer-bottom">
  95. {translate('overview.badges', selectedType, 'description', qualifier)}
  96. </p>
  97. <BadgeParams
  98. className="big-spacer-bottom"
  99. metrics={this.props.metrics}
  100. options={badgeOptions}
  101. type={selectedType}
  102. updateOptions={this.handleUpdateOptions}
  103. />
  104. <CodeSnippet isOneLine={true} snippet={getBadgeUrl(selectedType, fullBadgeOptions)} />
  105. </div>
  106. <footer className="modal-foot">
  107. <ResetButtonLink className="js-modal-close" onClick={this.handleClose}>
  108. {translate('close')}
  109. </ResetButtonLink>
  110. </footer>
  111. </Modal>
  112. )}
  113. </div>
  114. );
  115. }
  116. }