aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/app/components/current-user
diff options
context:
space:
mode:
Diffstat (limited to 'server/sonar-web/src/main/js/app/components/current-user')
-rw-r--r--server/sonar-web/src/main/js/app/components/current-user/CurrentUserContext.ts31
-rw-r--r--server/sonar-web/src/main/js/app/components/current-user/CurrentUserContextProvider.tsx62
-rw-r--r--server/sonar-web/src/main/js/app/components/current-user/withCurrentUserContext.tsx42
3 files changed, 135 insertions, 0 deletions
diff --git a/server/sonar-web/src/main/js/app/components/current-user/CurrentUserContext.ts b/server/sonar-web/src/main/js/app/components/current-user/CurrentUserContext.ts
new file mode 100644
index 00000000000..efb10534705
--- /dev/null
+++ b/server/sonar-web/src/main/js/app/components/current-user/CurrentUserContext.ts
@@ -0,0 +1,31 @@
+/*
+ * 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 * as React from 'react';
+import { CurrentUser, HomePage } from '../../../types/users';
+
+export interface CurrentUserContextInterface {
+ currentUser: CurrentUser;
+ updateCurrentUserHomepage: (homepage: HomePage) => void;
+ updateCurrentUserSonarLintAdSeen: () => void;
+}
+
+export const CurrentUserContext = React.createContext<CurrentUserContextInterface | undefined>(
+ undefined
+);
diff --git a/server/sonar-web/src/main/js/app/components/current-user/CurrentUserContextProvider.tsx b/server/sonar-web/src/main/js/app/components/current-user/CurrentUserContextProvider.tsx
new file mode 100644
index 00000000000..9ffe3d1ec59
--- /dev/null
+++ b/server/sonar-web/src/main/js/app/components/current-user/CurrentUserContextProvider.tsx
@@ -0,0 +1,62 @@
+/*
+ * 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 * as React from 'react';
+import { CurrentUser, HomePage } from '../../../types/users';
+import { CurrentUserContext } from './CurrentUserContext';
+
+interface Props {
+ currentUser?: CurrentUser;
+}
+
+interface State {
+ currentUser: CurrentUser;
+}
+
+export default class CurrentUserContextProvider extends React.PureComponent<Props, State> {
+ constructor(props: Props) {
+ super(props);
+ this.state = { currentUser: props.currentUser ?? { isLoggedIn: false } };
+ }
+
+ updateCurrentUserHomepage = (homepage: HomePage) => {
+ this.setState(prevState => ({
+ currentUser: { ...prevState.currentUser, homepage }
+ }));
+ };
+
+ updateCurrentUserSonarLintAdSeen = () => {
+ this.setState(prevState => ({
+ currentUser: { ...prevState.currentUser, sonarLintAdSeen: true }
+ }));
+ };
+
+ render() {
+ return (
+ <CurrentUserContext.Provider
+ value={{
+ currentUser: this.state.currentUser,
+ updateCurrentUserHomepage: this.updateCurrentUserHomepage,
+ updateCurrentUserSonarLintAdSeen: this.updateCurrentUserSonarLintAdSeen
+ }}>
+ {this.props.children}
+ </CurrentUserContext.Provider>
+ );
+ }
+}
diff --git a/server/sonar-web/src/main/js/app/components/current-user/withCurrentUserContext.tsx b/server/sonar-web/src/main/js/app/components/current-user/withCurrentUserContext.tsx
new file mode 100644
index 00000000000..e2621f072ac
--- /dev/null
+++ b/server/sonar-web/src/main/js/app/components/current-user/withCurrentUserContext.tsx
@@ -0,0 +1,42 @@
+/*
+ * 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 * as React from 'react';
+import { getWrappedDisplayName } from '../../../components/hoc/utils';
+import { CurrentUserContext, CurrentUserContextInterface } from './CurrentUserContext';
+
+export default function withCurrentUserContext<P>(
+ WrappedComponent: React.ComponentType<P & Pick<CurrentUserContextInterface, 'currentUser'>>
+) {
+ return class WithCurrentUserContext extends React.PureComponent<
+ Omit<P, keyof CurrentUserContextInterface>
+ > {
+ static displayName = getWrappedDisplayName(WrappedComponent, 'withCurrentUserContext');
+
+ render() {
+ return (
+ <CurrentUserContext.Consumer>
+ {(currentUserContext: CurrentUserContextInterface) => (
+ <WrappedComponent {...currentUserContext} {...(this.props as P)} />
+ )}
+ </CurrentUserContext.Consumer>
+ );
+ }
+ };
+}