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.

UpdateNotification.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { Banner } from 'design-system';
  21. import { groupBy, isEmpty, mapValues } from 'lodash';
  22. import * as React from 'react';
  23. import DismissableAlert from '../../../components/ui/DismissableAlert';
  24. import SystemUpgradeButton from '../../../components/upgrade/SystemUpgradeButton';
  25. import { UpdateUseCase } from '../../../components/upgrade/utils';
  26. import { translate } from '../../../helpers/l10n';
  27. import { hasGlobalPermission } from '../../../helpers/users';
  28. import { useSystemUpgrades } from '../../../queries/system';
  29. import { Permissions } from '../../../types/permissions';
  30. import { isLoggedIn } from '../../../types/users';
  31. import { useAppState } from '../app-state/withAppStateContext';
  32. import { useCurrentUser } from '../current-user/CurrentUserContext';
  33. import { BANNER_VARIANT, isCurrentVersionLTA, isMinorUpdate, isPatchUpdate } from './helpers';
  34. interface Props {
  35. dismissable?: boolean;
  36. }
  37. const VERSION_PARSER = /^(\d+)\.(\d+)(\.(\d+))?/;
  38. export default function UpdateNotification({ dismissable }: Readonly<Props>) {
  39. const appState = useAppState();
  40. const { currentUser } = useCurrentUser();
  41. const canUserSeeNotification =
  42. isLoggedIn(currentUser) && hasGlobalPermission(currentUser, Permissions.Admin);
  43. const regExpParsedVersion = VERSION_PARSER.exec(appState.version);
  44. const { data } = useSystemUpgrades({
  45. enabled: canUserSeeNotification && regExpParsedVersion !== null,
  46. });
  47. if (
  48. !canUserSeeNotification ||
  49. regExpParsedVersion === null ||
  50. data === undefined ||
  51. isEmpty(data.upgrades)
  52. ) {
  53. return null;
  54. }
  55. const { upgrades, installedVersionActive, latestLTA } = data;
  56. const parsedVersion = regExpParsedVersion
  57. .slice(1)
  58. .map(Number)
  59. .map((n) => (isNaN(n) ? 0 : n));
  60. const systemUpgrades = mapValues(
  61. groupBy(upgrades, (upgrade) => {
  62. const [major] = upgrade.version.split('.');
  63. return major;
  64. }),
  65. (upgrades) =>
  66. groupBy(upgrades, (upgrade) => {
  67. const [, minor] = upgrade.version.split('.');
  68. return minor;
  69. }),
  70. );
  71. let useCase = UpdateUseCase.NewVersion;
  72. if (!installedVersionActive) {
  73. useCase = UpdateUseCase.CurrentVersionInactive;
  74. } else if (
  75. isPatchUpdate(parsedVersion, systemUpgrades) &&
  76. (isCurrentVersionLTA(parsedVersion, latestLTA) || !isMinorUpdate(parsedVersion, systemUpgrades))
  77. ) {
  78. useCase = UpdateUseCase.NewPatch;
  79. }
  80. const latest = [...upgrades].sort(
  81. (upgrade1, upgrade2) =>
  82. new Date(upgrade2.releaseDate ?? '').getTime() -
  83. new Date(upgrade1.releaseDate ?? '').getTime(),
  84. )[0];
  85. const dismissKey = useCase + latest.version;
  86. return dismissable ? (
  87. <DismissableAlert
  88. alertKey={dismissKey}
  89. variant={BANNER_VARIANT[useCase]}
  90. className={`it__promote-update-notification it__upgrade-prompt-${useCase}`}
  91. >
  92. {translate('admin_notification.update', useCase)}
  93. <SystemUpgradeButton
  94. systemUpgrades={upgrades}
  95. updateUseCase={useCase}
  96. latestLTA={latestLTA}
  97. />
  98. </DismissableAlert>
  99. ) : (
  100. <Banner variant={BANNER_VARIANT[useCase]} className={`it__upgrade-prompt-${useCase}`}>
  101. {translate('admin_notification.update', useCase)}
  102. <SystemUpgradeButton
  103. systemUpgrades={upgrades}
  104. updateUseCase={useCase}
  105. latestLTA={latestLTA}
  106. />
  107. </Banner>
  108. );
  109. }