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.

index.ts 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 { installExtensionsHandler, installWebAnalyticsHandler } from '../helpers/extensionsHandler';
  21. import { loadL10nBundle } from '../helpers/l10n';
  22. import { parseJSON, request } from '../helpers/request';
  23. import { getBaseUrl, getSystemStatus } from '../helpers/system';
  24. import { AppState } from '../types/appstate';
  25. import './styles/sonar.ts';
  26. installWebAnalyticsHandler();
  27. if (isMainApp()) {
  28. installExtensionsHandler();
  29. Promise.all([loadL10nBundle(), loadUser(), loadAppState(), loadApp()]).then(
  30. ([l10nBundle, user, appState, startReactApp]) => {
  31. startReactApp(l10nBundle.locale, appState, user);
  32. },
  33. error => {
  34. if (isResponse(error) && error.status === 401) {
  35. redirectToLogin();
  36. } else {
  37. logError(error);
  38. }
  39. }
  40. );
  41. } else {
  42. // login, maintenance or setup pages
  43. const appStatePromise: Promise<AppState> = new Promise(resolve => {
  44. loadAppState()
  45. .then(data => {
  46. resolve(data);
  47. })
  48. .catch(() => {
  49. resolve({
  50. edition: undefined,
  51. productionDatabase: true,
  52. qualifiers: [],
  53. settings: {},
  54. version: ''
  55. });
  56. });
  57. });
  58. Promise.all([loadL10nBundle(), appStatePromise, loadApp()]).then(
  59. ([l10nBundle, appState, startReactApp]) => {
  60. startReactApp(l10nBundle.locale, appState);
  61. },
  62. error => {
  63. logError(error);
  64. }
  65. );
  66. }
  67. function loadUser() {
  68. return request('/api/users/current')
  69. .submit()
  70. .then(checkStatus)
  71. .then(parseJSON);
  72. }
  73. function loadAppState() {
  74. return request('/api/navigation/global')
  75. .submit()
  76. .then(checkStatus)
  77. .then(parseJSON);
  78. }
  79. function loadApp() {
  80. return import(/* webpackChunkName: 'app' */ './utils/startReactApp').then(i => i.default);
  81. }
  82. function checkStatus(response: Response) {
  83. return new Promise((resolve, reject) => {
  84. if (response.status >= 200 && response.status < 300) {
  85. resolve(response);
  86. } else {
  87. reject(response);
  88. }
  89. });
  90. }
  91. function isResponse(error: any): error is Response {
  92. return typeof error.status === 'number';
  93. }
  94. function redirectToLogin() {
  95. const returnTo = window.location.pathname + window.location.search + window.location.hash;
  96. window.location.href = `${getBaseUrl()}/sessions/new?return_to=${encodeURIComponent(returnTo)}`;
  97. }
  98. function logError(error: any) {
  99. // eslint-disable-next-line no-console
  100. console.error('Application failed to start!', error);
  101. }
  102. function isMainApp() {
  103. const { pathname } = window.location;
  104. return (
  105. getSystemStatus() === 'UP' &&
  106. !pathname.startsWith(`${getBaseUrl()}/sessions`) &&
  107. !pathname.startsWith(`${getBaseUrl()}/maintenance`) &&
  108. !pathname.startsWith(`${getBaseUrl()}/setup`) &&
  109. !pathname.startsWith(`${getBaseUrl()}/formatting/help`)
  110. );
  111. }