fleet/frontend/layouts/CoreLayout/CoreLayout.jsx

224 lines
5.4 KiB
React
Raw Normal View History

import React, { Component } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { logoutUser } from "redux/nodes/auth/actions";
import { push } from "react-router-redux";
import { TableContext } from "context/table";
import { isEqual } from "lodash";
import permissionUtils from "utilities/permissions";
import configInterface from "interfaces/config";
import FlashMessage from "components/flash_messages/FlashMessage";
import PersistentFlash from "components/flash_messages/PersistentFlash";
import SiteTopNav from "components/side_panels/SiteTopNav";
import userInterface from "interfaces/user";
import notificationInterface from "interfaces/notification";
import { hideFlash } from "redux/nodes/notifications/actions";
import { licenseExpirationWarning } from "fleet/helpers";
const expirationMessage = (
<>
Your license for Fleet Premium is about to expire. If youd like to renew or
have questions about downgrading,{" "}
<a
href="https://github.com/fleetdm/fleet/blob/main/docs/1-Using-Fleet/10-Teams.md#expired_license"
target="_blank"
rel="noopener noreferrer"
>
please head to the Fleet documentation
</a>
.
</>
);
export class CoreLayout extends Component {
static propTypes = {
children: PropTypes.node,
config: configInterface,
dispatch: PropTypes.func,
user: userInterface,
2017-01-05 18:08:19 +00:00
fullWidthFlash: PropTypes.bool,
notifications: notificationInterface,
persistentFlash: PropTypes.shape({
showFlash: PropTypes.bool.isRequired,
message: PropTypes.string.isRequired,
}).isRequired,
isPremiumTier: PropTypes.bool,
};
constructor(props) {
super(props);
this.state = {
showExpirationFlashMessage: false,
};
}
componentWillReceiveProps(nextProps) {
const { notifications, config } = nextProps;
const table = this.context;
// on success of an action, the table will reset its checkboxes.
// setTimeout is to help with race conditions as table reloads
// in some instances (i.e. Manage Hosts)
if (!isEqual(this.props.notifications, notifications)) {
if (notifications.alertType === "success") {
setTimeout(() => {
table.setResetSelectedRows(true);
setTimeout(() => {
table.setResetSelectedRows(false);
}, 300);
}, 0);
}
}
this.setState({
showExpirationFlashMessage: licenseExpirationWarning(config.expiration),
});
}
onLogoutUser = () => {
const { dispatch } = this.props;
dispatch(logoutUser());
return false;
};
onNavItemClick = (path) => {
return (evt) => {
evt.preventDefault();
const { dispatch } = this.props;
if (path.indexOf("http") !== -1) {
global.window.open(path, "_blank");
2017-01-18 14:40:28 +00:00
return false;
}
dispatch(push(path));
return false;
};
};
2017-01-05 18:08:19 +00:00
onRemoveFlash = () => {
const { dispatch } = this.props;
dispatch(hideFlash);
return false;
};
2017-01-05 18:08:19 +00:00
onRemoveExpirationWarning = () => {
const { showExpirationFlashMessage } = this.state;
this.setState({
showExpirationFlashMessage: !showExpirationFlashMessage,
});
};
2017-01-05 18:08:19 +00:00
onUndoActionClick = (undoAction) => {
return (evt) => {
evt.preventDefault();
const { dispatch } = this.props;
const { onRemoveFlash } = this;
dispatch(undoAction);
return onRemoveFlash();
};
};
2017-01-05 18:08:19 +00:00
static contextType = TableContext;
render() {
2017-02-24 23:01:43 +00:00
const {
fullWidthFlash,
notifications,
children,
config,
persistentFlash,
user,
isPremiumTier,
2017-02-24 23:01:43 +00:00
} = this.props;
const { showExpirationFlashMessage } = this.state;
const {
onRemoveFlash,
onRemoveExpirationWarning,
onUndoActionClick,
} = this;
const expirationNotification = {
alertType: "warning-filled",
isVisible: true,
message: expirationMessage,
};
if (!user) return false;
const { onLogoutUser, onNavItemClick } = this;
const { pathname } = global.window.location;
return (
<div className="app-wrap">
<nav className="site-nav">
<SiteTopNav
2016-12-01 22:14:39 +00:00
config={config}
onLogoutUser={onLogoutUser}
onNavItemClick={onNavItemClick}
2016-12-01 22:14:39 +00:00
pathname={pathname}
user={user}
/>
</nav>
<div className="core-wrapper">
{persistentFlash.showFlash && (
<PersistentFlash message={persistentFlash.message} />
)}
{isPremiumTier && showExpirationFlashMessage && (
<FlashMessage
fullWidth={fullWidthFlash}
notification={expirationNotification}
onRemoveFlash={onRemoveExpirationWarning}
/>
)}
2017-01-05 18:08:19 +00:00
<FlashMessage
fullWidth={fullWidthFlash}
notification={notifications}
onRemoveFlash={onRemoveFlash}
onUndoActionClick={onUndoActionClick}
/>
{children}
</div>
</div>
);
}
}
const mapStateToProps = (state) => {
const {
app: { config },
auth: { user },
2017-01-05 18:08:19 +00:00
notifications,
persistentFlash,
} = state;
const isPremiumTier = permissionUtils.isPremiumTier(state.app.config);
2017-01-05 18:08:19 +00:00
const fullWidthFlash = !user;
return {
config,
fullWidthFlash,
notifications,
persistentFlash,
user,
isPremiumTier,
};
};
export default connect(mapStateToProps)(CoreLayout);