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.

AnalysisWarningsModal.tsx 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 { DangerButtonSecondary, FlagMessage, HtmlFormatter, Modal, Spinner } from 'design-system';
  21. import * as React from 'react';
  22. import withCurrentUserContext from '../../../app/components/current-user/withCurrentUserContext';
  23. import { translate } from '../../../helpers/l10n';
  24. import { sanitizeStringRestricted } from '../../../helpers/sanitize';
  25. import { useDismissBranchWarningMutation } from '../../../queries/branch';
  26. import { TaskWarning } from '../../../types/tasks';
  27. import { Component } from '../../../types/types';
  28. import { CurrentUser } from '../../../types/users';
  29. interface Props {
  30. component: Component;
  31. currentUser: CurrentUser;
  32. onClose: () => void;
  33. warnings: TaskWarning[];
  34. }
  35. export function AnalysisWarningsModal(props: Props) {
  36. const { component, currentUser, warnings } = props;
  37. const { mutate, isPending, variables } = useDismissBranchWarningMutation();
  38. const handleDismissMessage = (messageKey: string) => {
  39. mutate({ component, key: messageKey });
  40. };
  41. const body = (
  42. <>
  43. {warnings.map(({ dismissable, key, message }) => (
  44. <React.Fragment key={key}>
  45. <div className="sw-flex sw-items-center sw-mt-2">
  46. <FlagMessage variant="warning">
  47. <HtmlFormatter>
  48. <span
  49. // eslint-disable-next-line react/no-danger
  50. dangerouslySetInnerHTML={{
  51. __html: sanitizeStringRestricted(message.trim().replace(/\n/g, '<br />')),
  52. }}
  53. />
  54. </HtmlFormatter>
  55. </FlagMessage>
  56. </div>
  57. <div>
  58. {dismissable && currentUser.isLoggedIn && (
  59. <div className="sw-mt-4">
  60. <DangerButtonSecondary
  61. disabled={Boolean(isPending)}
  62. onClick={() => {
  63. handleDismissMessage(key);
  64. }}
  65. >
  66. {translate('dismiss_permanently')}
  67. </DangerButtonSecondary>
  68. <Spinner className="sw-ml-2" loading={isPending && variables?.key === key} />
  69. </div>
  70. )}
  71. </div>
  72. </React.Fragment>
  73. ))}
  74. </>
  75. );
  76. return (
  77. <Modal
  78. headerTitle={translate('warnings')}
  79. onClose={props.onClose}
  80. body={body}
  81. primaryButton={null}
  82. secondaryButtonLabel={translate('close')}
  83. />
  84. );
  85. }
  86. export default withCurrentUserContext(AnalysisWarningsModal);