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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 from './utils/installExtensionsHandler';
  21. import { installGlobal, DEFAULT_LANGUAGE, requestMessages } from '../helpers/l10n';
  22. import { request, parseJSON } from '../helpers/request';
  23. import { getSystemStatus } from '../helpers/system';
  24. import './styles/sonar.css';
  25. installGlobal();
  26. if (isMainApp()) {
  27. installExtensionsHandler();
  28. Promise.all([loadMessages(), loadUser(), loadAppState(), loadApp()]).then(
  29. ([lang, user, appState, startReactApp]) => {
  30. startReactApp(lang, user, appState);
  31. },
  32. error => {
  33. if (isResponse(error) && error.status === 401) {
  34. redirectToLogin();
  35. } else {
  36. logError(error);
  37. }
  38. }
  39. );
  40. } else {
  41. // login, maintenance or setup pages
  42. const appStatePromise: Promise<T.AppState> = new Promise(resolve =>
  43. loadAppState()
  44. .then(data => resolve(data))
  45. .catch(() => resolve(undefined))
  46. );
  47. Promise.all([loadMessages(), appStatePromise, loadApp()]).then(
  48. ([lang, appState, startReactApp]) => {
  49. startReactApp(lang, undefined, appState);
  50. },
  51. error => {
  52. logError(error);
  53. }
  54. );
  55. }
  56. function loadMessages() {
  57. return requestMessages().then(setLanguage, setLanguage);
  58. }
  59. function loadLocaleData(langToLoad: string) {
  60. return Promise.all([import('react-intl/locale-data/' + langToLoad), import('react-intl')]).then(
  61. ([intlBundle, intl]) => {
  62. intl.addLocaleData(intlBundle.default);
  63. }
  64. );
  65. }
  66. function setLanguage(lang: string) {
  67. const langToLoad = lang || DEFAULT_LANGUAGE;
  68. // No need to load english (default) bundle, it's coming with react-intl
  69. if (langToLoad !== DEFAULT_LANGUAGE) {
  70. return loadLocaleData(langToLoad).then(() => langToLoad, () => DEFAULT_LANGUAGE);
  71. } else {
  72. return DEFAULT_LANGUAGE;
  73. }
  74. }
  75. function loadUser() {
  76. return request('/api/users/current')
  77. .submit()
  78. .then(checkStatus)
  79. .then(parseJSON);
  80. }
  81. function loadAppState() {
  82. return request('/api/navigation/global')
  83. .submit()
  84. .then(checkStatus)
  85. .then(parseJSON);
  86. }
  87. function loadApp() {
  88. return import(/* webpackChunkName: 'app' */ './utils/startReactApp').then(i => i.default);
  89. }
  90. function checkStatus(response: Response) {
  91. return new Promise((resolve, reject) => {
  92. if (response.status >= 200 && response.status < 300) {
  93. resolve(response);
  94. } else {
  95. reject(response);
  96. }
  97. });
  98. }
  99. function isResponse(error: any): error is Response {
  100. return typeof error.status === 'number';
  101. }
  102. function redirectToLogin() {
  103. const returnTo = window.location.pathname + window.location.search + window.location.hash;
  104. window.location.href = `${getBaseUrl()}/sessions/new?return_to=${encodeURIComponent(returnTo)}`;
  105. }
  106. function logError(error: any) {
  107. // eslint-disable-next-line no-console
  108. console.error('Application failed to start!', error);
  109. }
  110. function isMainApp() {
  111. const { pathname } = window.location;
  112. return (
  113. getSystemStatus() === 'UP' &&
  114. !pathname.startsWith(`${getBaseUrl()}/sessions`) &&
  115. !pathname.startsWith(`${getBaseUrl()}/maintenance`) &&
  116. !pathname.startsWith(`${getBaseUrl()}/setup`) &&
  117. !pathname.startsWith(`${getBaseUrl()}/markdown/help`)
  118. );
  119. }
  120. function getBaseUrl(): string {
  121. return (window as any).baseUrl;
  122. }