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.

AlmSynchronisationWarning.tsx 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 styled from '@emotion/styled';
  21. import { Link, Spinner } from '@sonarsource/echoes-react';
  22. import { formatDistance } from 'date-fns';
  23. import { CheckIcon, FlagMessage, FlagWarningIcon, themeColor } from 'design-system';
  24. import * as React from 'react';
  25. import { FormattedMessage } from 'react-intl';
  26. import { translate, translateWithParameters } from '../../helpers/l10n';
  27. import { AlmSyncStatus } from '../../types/provisioning';
  28. import { TaskStatuses } from '../../types/tasks';
  29. interface SynchronisationWarningProps {
  30. data: AlmSyncStatus;
  31. short?: boolean;
  32. }
  33. interface LastSyncProps {
  34. info: AlmSyncStatus['lastSync'];
  35. short?: boolean;
  36. }
  37. function LastSyncAlert({ info, short }: Readonly<LastSyncProps>) {
  38. if (info === undefined) {
  39. return null;
  40. }
  41. const { finishedAt, errorMessage, status, summary, warningMessage } = info;
  42. const formattedDate = finishedAt ? formatDistance(new Date(finishedAt), new Date()) : '';
  43. if (short) {
  44. return status === TaskStatuses.Success ? (
  45. <div>
  46. <IconWrapper className="sw-ml-2">
  47. {warningMessage ? (
  48. <FlagWarningIcon className="sw-mr-2" />
  49. ) : (
  50. <CheckIcon width={32} height={32} className="sw-mr-2" />
  51. )}
  52. </IconWrapper>
  53. <i>
  54. {warningMessage ? (
  55. <FormattedMessage
  56. defaultMessage={translate(
  57. 'settings.authentication.github.synchronization_successful.with_warning',
  58. )}
  59. id="settings.authentication.github.synchronization_successful.with_warning"
  60. values={{
  61. date: formattedDate,
  62. details: (
  63. <Link className="sw-ml-2" to="/admin/settings?category=authentication&tab=github">
  64. {translate('settings.authentication.github.synchronization_details_link')}
  65. </Link>
  66. ),
  67. }}
  68. />
  69. ) : (
  70. translateWithParameters(
  71. 'settings.authentication.github.synchronization_successful',
  72. formattedDate,
  73. )
  74. )}
  75. </i>
  76. </div>
  77. ) : (
  78. <FlagMessage variant="error">
  79. <div>
  80. <FormattedMessage
  81. defaultMessage={translate(
  82. 'settings.authentication.github.synchronization_failed_short',
  83. )}
  84. id="settings.authentication.github.synchronization_failed_short"
  85. values={{
  86. details: (
  87. <Link className="sw-ml-2" to="/admin/settings?category=authentication&tab=github">
  88. {translate('settings.authentication.github.synchronization_details_link')}
  89. </Link>
  90. ),
  91. }}
  92. />
  93. </div>
  94. </FlagMessage>
  95. );
  96. }
  97. return (
  98. <>
  99. <FlagMessage
  100. aria-live="assertive"
  101. role="alert"
  102. variant={status === TaskStatuses.Success ? 'success' : 'error'}
  103. >
  104. <div>
  105. {status === TaskStatuses.Success ? (
  106. <>
  107. {translateWithParameters(
  108. 'settings.authentication.github.synchronization_successful',
  109. formattedDate,
  110. )}
  111. <br />
  112. {summary ?? ''}
  113. </>
  114. ) : (
  115. <React.Fragment key={`synch-alert-${finishedAt}`}>
  116. <div>
  117. {translateWithParameters(
  118. 'settings.authentication.github.synchronization_failed',
  119. formattedDate,
  120. )}
  121. </div>
  122. <br />
  123. {errorMessage ?? ''}
  124. </React.Fragment>
  125. )}
  126. </div>
  127. </FlagMessage>
  128. <FlagMessage variant="warning" role="alert" aria-live="assertive">
  129. {warningMessage}
  130. </FlagMessage>
  131. </>
  132. );
  133. }
  134. export default function AlmSynchronisationWarning({
  135. data,
  136. short,
  137. }: Readonly<SynchronisationWarningProps>) {
  138. const loadingLabel =
  139. data.nextSync &&
  140. translate(
  141. data.nextSync.status === TaskStatuses.Pending
  142. ? 'settings.authentication.github.synchronization_pending'
  143. : 'settings.authentication.github.synchronization_in_progress',
  144. );
  145. return (
  146. <>
  147. {!short && (
  148. <div className={data.nextSync ? 'sw-flex sw-gap-2 sw-mb-4' : ''}>
  149. <Spinner ariaLabel={loadingLabel} isLoading={!!data.nextSync} />
  150. <div>{data.nextSync && loadingLabel}</div>
  151. </div>
  152. )}
  153. <LastSyncAlert short={short} info={data.lastSync} />
  154. </>
  155. );
  156. }
  157. const IconWrapper = styled.span`
  158. color: ${themeColor('iconSuccess')};
  159. `;