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.

App.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 * as React from 'react';
  21. import { connect } from 'react-redux';
  22. import { lazyLoadComponent } from 'sonar-ui-common/components/lazyLoadComponent';
  23. import { fetchLanguages } from '../../store/rootActions';
  24. import { getAppState, getCurrentUser, getGlobalSettingValue, Store } from '../../store/rootReducer';
  25. import KeyboardShortcutsModal from './KeyboardShortcutsModal';
  26. const PageTracker = lazyLoadComponent(() => import('./PageTracker'));
  27. interface StateProps {
  28. appState: T.AppState | undefined;
  29. currentUser: T.CurrentUser | undefined;
  30. enableGravatar: boolean;
  31. gravatarServerUrl: string;
  32. }
  33. interface DispatchProps {
  34. fetchLanguages: () => Promise<void>;
  35. }
  36. interface OwnProps {
  37. children: JSX.Element;
  38. }
  39. type Props = StateProps & DispatchProps & OwnProps;
  40. class App extends React.PureComponent<Props> {
  41. mounted = false;
  42. componentDidMount() {
  43. this.mounted = true;
  44. this.props.fetchLanguages();
  45. this.setScrollbarWidth();
  46. }
  47. componentWillUnmount() {
  48. this.mounted = false;
  49. }
  50. setScrollbarWidth = () => {
  51. // See https://stackoverflow.com/questions/13382516/getting-scroll-bar-width-using-javascript
  52. const outer = document.createElement('div');
  53. outer.style.visibility = 'hidden';
  54. outer.style.width = '100px';
  55. outer.style.msOverflowStyle = 'scrollbar';
  56. document.body.appendChild(outer);
  57. const widthNoScroll = outer.offsetWidth;
  58. outer.style.overflow = 'scroll';
  59. const inner = document.createElement('div');
  60. inner.style.width = '100%';
  61. outer.appendChild(inner);
  62. const widthWithScroll = inner.offsetWidth;
  63. if (outer.parentNode) {
  64. outer.parentNode.removeChild(outer);
  65. }
  66. document.body.style.setProperty('--sbw', `${widthNoScroll - widthWithScroll}px`);
  67. };
  68. renderPreconnectLink = () => {
  69. const parser = document.createElement('a');
  70. parser.href = this.props.gravatarServerUrl;
  71. if (parser.hostname !== window.location.hostname) {
  72. return <link href={parser.origin} rel="preconnect" />;
  73. } else {
  74. return null;
  75. }
  76. };
  77. render() {
  78. return (
  79. <>
  80. <PageTracker>{this.props.enableGravatar && this.renderPreconnectLink()}</PageTracker>
  81. {this.props.children}
  82. <KeyboardShortcutsModal />
  83. </>
  84. );
  85. }
  86. }
  87. const mapStateToProps = (state: Store): StateProps => {
  88. const enableGravatar = getGlobalSettingValue(state, 'sonar.lf.enableGravatar');
  89. const gravatarServerUrl = getGlobalSettingValue(state, 'sonar.lf.gravatarServerUrl');
  90. return {
  91. appState: getAppState(state),
  92. currentUser: getCurrentUser(state),
  93. enableGravatar: Boolean(enableGravatar && enableGravatar.value === 'true'),
  94. gravatarServerUrl: (gravatarServerUrl && gravatarServerUrl.value) || ''
  95. };
  96. };
  97. const mapDispatchToProps = ({
  98. fetchLanguages
  99. } as any) as DispatchProps;
  100. export default connect(mapStateToProps, mapDispatchToProps)(App);