1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*
* SonarQube
* Copyright (C) 2009-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { differenceInDays } from 'date-fns';
import * as React from 'react';
import { connect } from 'react-redux';
import { showLicense } from '../../api/marketplace';
import { Location, Router, withRouter } from '../../components/hoc/withRouter';
import { lazyLoadComponent } from '../../components/lazyLoadComponent';
import { parseDate, toShortNotSoISOString } from '../../helpers/dates';
import { hasMessage } from '../../helpers/l10n';
import { get, save } from '../../helpers/storage';
import { isLoggedIn } from '../../helpers/users';
import { getAppState, getCurrentUser, Store } from '../../store/rootReducer';
import { EditionKey } from '../../types/editions';
import { CurrentUser } from '../../types/types';
const LicensePromptModal = lazyLoadComponent(
() => import('../../apps/marketplace/components/LicensePromptModal'),
'LicensePromptModal'
);
interface StateProps {
canAdmin?: boolean;
currentEdition?: EditionKey;
currentUser: CurrentUser;
}
interface OwnProps {
children?: React.ReactNode;
}
interface WithRouterProps {
location: Pick<Location, 'pathname'>;
router: Pick<Router, 'push'>;
}
type Props = StateProps & OwnProps & WithRouterProps;
interface State {
open?: boolean;
}
const LICENSE_PROMPT = 'sonarqube.license.prompt';
export class StartupModal extends React.PureComponent<Props, State> {
state: State = {};
componentDidMount() {
this.tryAutoOpenLicense();
}
closeLicense = () => {
this.setState({ open: false });
};
tryAutoOpenLicense = () => {
const { canAdmin, currentEdition, currentUser } = this.props;
const hasLicenseManager = hasMessage('license.prompt.title');
const hasLicensedEdition = currentEdition && currentEdition !== EditionKey.community;
if (canAdmin && hasLicensedEdition && isLoggedIn(currentUser) && hasLicenseManager) {
const lastPrompt = get(LICENSE_PROMPT, currentUser.login);
if (!lastPrompt || differenceInDays(new Date(), parseDate(lastPrompt)) >= 1) {
showLicense()
.then(license => {
if (!license || !license.isValidEdition) {
save(LICENSE_PROMPT, toShortNotSoISOString(new Date()), currentUser.login);
this.setState({ open: true });
}
})
.catch(() => {});
}
}
};
render() {
const { open } = this.state;
return (
<>
{this.props.children}
{open && <LicensePromptModal onClose={this.closeLicense} />}
</>
);
}
}
const mapStateToProps = (state: Store): StateProps => ({
canAdmin: getAppState(state).canAdmin,
currentEdition: getAppState(state).edition as EditionKey, // TODO: Fix once AppState is no longer ambiant.
currentUser: getCurrentUser(state)
});
export default connect(mapStateToProps)(withRouter(StartupModal));
|