You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AlmTabRenderer.tsx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 { FormattedMessage } from 'react-intl';
  22. import { Link } from 'react-router';
  23. import DeferredSpinner from 'sonar-ui-common/components/ui/DeferredSpinner';
  24. import { translate } from 'sonar-ui-common/helpers/l10n';
  25. import { AlmBindingDefinition, AlmKeys } from '../../../../types/alm-settings';
  26. import AlmBindingDefinitionForm, {
  27. AlmBindingDefinitionFormChildrenProps
  28. } from './AlmBindingDefinitionForm';
  29. import AlmBindingDefinitionsTable from './AlmBindingDefinitionsTable';
  30. import AlmIntegrationFeatureBox, {
  31. AlmIntegrationFeatureBoxProps
  32. } from './AlmIntegrationFeatureBox';
  33. export interface AlmTabRendererProps<B> {
  34. additionalColumnsHeaders: string[];
  35. additionalColumnsKeys: Array<keyof B>;
  36. additionalTableInfo?: React.ReactNode;
  37. alm: AlmKeys;
  38. editedDefinition?: B;
  39. defaultBinding: B;
  40. definitions: B[];
  41. features?: AlmIntegrationFeatureBoxProps[];
  42. form: (props: AlmBindingDefinitionFormChildrenProps<B>) => React.ReactNode;
  43. help?: React.ReactNode;
  44. loading: boolean;
  45. multipleAlmEnabled: boolean;
  46. onCancel: () => void;
  47. onCreate: () => void;
  48. onDelete: (definitionKey: string) => void;
  49. onEdit: (definitionKey: string) => void;
  50. onSubmit: (config: B, originalKey: string) => void;
  51. success: boolean;
  52. }
  53. export default function AlmTabRenderer<B extends AlmBindingDefinition>(
  54. props: AlmTabRendererProps<B>
  55. ) {
  56. const {
  57. additionalColumnsHeaders,
  58. additionalColumnsKeys,
  59. additionalTableInfo,
  60. alm,
  61. defaultBinding,
  62. definitions,
  63. editedDefinition,
  64. features = [],
  65. form,
  66. loading,
  67. multipleAlmEnabled,
  68. success,
  69. help = (
  70. <FormattedMessage
  71. defaultMessage={translate(`settings.almintegration.${alm}.info`)}
  72. id={`settings.almintegration.${alm}.info`}
  73. values={{
  74. link: (
  75. <Link target="_blank" to="/documentation/analysis/pr-decoration/">
  76. {translate('learn_more')}
  77. </Link>
  78. )
  79. }}
  80. />
  81. )
  82. } = props;
  83. let definition: B | undefined;
  84. let mappedDefinitions: Array<{ key: string; additionalColumns: string[] }> = [];
  85. let showEdit: boolean | undefined;
  86. if (!multipleAlmEnabled) {
  87. definition = editedDefinition;
  88. if (definition === undefined && definitions.length > 0) {
  89. definition = definitions[0];
  90. }
  91. showEdit = definition && editedDefinition === undefined;
  92. } else {
  93. mappedDefinitions = definitions.map(({ key, ...properties }) => {
  94. const additionalColumns = additionalColumnsKeys.map(k => (properties as any)[k]);
  95. return {
  96. key,
  97. additionalColumns
  98. };
  99. });
  100. }
  101. return (
  102. <div className="big-padded">
  103. {multipleAlmEnabled ? (
  104. <DeferredSpinner loading={loading}>
  105. <AlmBindingDefinitionsTable
  106. additionalColumnsHeaders={additionalColumnsHeaders}
  107. additionalTableInfo={additionalTableInfo}
  108. alm={alm}
  109. definitions={mappedDefinitions}
  110. onCreate={props.onCreate}
  111. onDelete={props.onDelete}
  112. onEdit={props.onEdit}
  113. />
  114. {editedDefinition && (
  115. <AlmBindingDefinitionForm
  116. bindingDefinition={editedDefinition}
  117. help={help}
  118. onCancel={props.onCancel}
  119. onSubmit={props.onSubmit}
  120. showInModal={true}>
  121. {form}
  122. </AlmBindingDefinitionForm>
  123. )}
  124. </DeferredSpinner>
  125. ) : (
  126. <AlmBindingDefinitionForm
  127. bindingDefinition={definition || defaultBinding}
  128. help={help}
  129. hideKeyField={true}
  130. loading={loading}
  131. onCancel={props.onCancel}
  132. onDelete={definition ? props.onDelete : undefined}
  133. onEdit={showEdit ? props.onEdit : undefined}
  134. onSubmit={props.onSubmit}
  135. readOnly={showEdit}
  136. success={success}>
  137. {form}
  138. </AlmBindingDefinitionForm>
  139. )}
  140. {features.length > 0 && (
  141. <div className="big-spacer-top big-padded-top bordered-top">
  142. <h3 className="big-spacer-bottom">{translate('settings.almintegration.features')}</h3>
  143. <div className="display-flex-wrap">
  144. {features.map((feature, i) => (
  145. <AlmIntegrationFeatureBox key={i} {...feature} />
  146. ))}
  147. </div>
  148. </div>
  149. )}
  150. </div>
  151. );
  152. }