2024-05-29 13:41:07 +00:00
|
|
|
import React, { useContext } from "react";
|
2022-04-01 06:42:26 +00:00
|
|
|
import { InjectedRouter } from "react-router";
|
2023-03-31 17:40:14 +00:00
|
|
|
|
2024-10-04 17:25:08 +00:00
|
|
|
import UnsupportedScreenSize from "layouts/UnsupportedScreenSize";
|
|
|
|
|
|
2022-04-01 06:42:26 +00:00
|
|
|
import { AppContext } from "context/app";
|
|
|
|
|
import { NotificationContext } from "context/notification";
|
|
|
|
|
import { TableContext } from "context/table";
|
2025-02-28 13:45:33 +00:00
|
|
|
import { INotification } from "interfaces/notification";
|
2025-11-07 22:30:51 +00:00
|
|
|
import classNames from "classnames";
|
2022-04-01 06:42:26 +00:00
|
|
|
|
2022-04-07 16:08:00 +00:00
|
|
|
import paths from "router/paths";
|
2022-04-22 16:45:35 +00:00
|
|
|
import useDeepEffect from "hooks/useDeepEffect";
|
2022-04-01 06:42:26 +00:00
|
|
|
import FlashMessage from "components/FlashMessage";
|
|
|
|
|
import SiteTopNav from "components/top_nav/SiteTopNav";
|
2023-03-31 17:40:14 +00:00
|
|
|
import { QueryParams } from "utilities/url";
|
2025-11-07 22:30:51 +00:00
|
|
|
import shouldShowUnsupportedScreen from "layouts/UnsupportedScreenSize/helpers";
|
2022-04-01 06:42:26 +00:00
|
|
|
|
|
|
|
|
interface ICoreLayoutProps {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
router: InjectedRouter; // v3
|
2023-03-31 17:40:14 +00:00
|
|
|
// TODO: standardize typing and usage of location across app components
|
|
|
|
|
location: {
|
|
|
|
|
pathname: string;
|
|
|
|
|
search: string;
|
|
|
|
|
hash?: string;
|
|
|
|
|
query: QueryParams;
|
|
|
|
|
};
|
2022-04-01 06:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-22 17:52:20 +00:00
|
|
|
const CoreLayout = ({ children, router, location }: ICoreLayoutProps) => {
|
2024-05-29 13:41:07 +00:00
|
|
|
const { config, currentUser } = useContext(AppContext);
|
2022-04-07 16:08:00 +00:00
|
|
|
const { notification, hideFlash } = useContext(NotificationContext);
|
2022-04-01 06:42:26 +00:00
|
|
|
const { setResetSelectedRows } = useContext(TableContext);
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
useDeepEffect(() => {
|
2025-02-28 13:45:33 +00:00
|
|
|
if (
|
|
|
|
|
notification &&
|
|
|
|
|
(notification as INotification).alertType === "success"
|
|
|
|
|
) {
|
2022-04-01 06:42:26 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
|
setResetSelectedRows(true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setResetSelectedRows(false);
|
|
|
|
|
}, 300);
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
}, [notification]);
|
|
|
|
|
|
|
|
|
|
const onLogoutUser = async () => {
|
2022-04-07 16:08:00 +00:00
|
|
|
const { LOGOUT } = paths;
|
|
|
|
|
router.push(LOGOUT);
|
2022-04-01 06:42:26 +00:00
|
|
|
};
|
|
|
|
|
|
2024-10-22 17:52:20 +00:00
|
|
|
const onUserMenuItemClick = (path: string) => {
|
2024-12-20 15:39:06 +00:00
|
|
|
router.push(path);
|
2022-04-01 06:42:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fullWidthFlash = !currentUser;
|
|
|
|
|
|
|
|
|
|
if (!currentUser || !config) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 22:30:51 +00:00
|
|
|
const coreWrapperClassnames = classNames("core-wrapper", {
|
|
|
|
|
"low-width-supported": !shouldShowUnsupportedScreen(location.pathname),
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-01 06:42:26 +00:00
|
|
|
return (
|
|
|
|
|
<div className="app-wrap">
|
2025-11-07 22:30:51 +00:00
|
|
|
{shouldShowUnsupportedScreen(location.pathname) && (
|
|
|
|
|
<UnsupportedScreenSize />
|
|
|
|
|
)}
|
2023-01-26 19:33:54 +00:00
|
|
|
<nav className="site-nav-container">
|
2022-04-01 06:42:26 +00:00
|
|
|
<SiteTopNav
|
|
|
|
|
config={config}
|
2023-03-31 17:40:14 +00:00
|
|
|
currentUser={currentUser}
|
|
|
|
|
location={location}
|
2022-04-01 06:42:26 +00:00
|
|
|
onLogoutUser={onLogoutUser}
|
2024-10-22 17:52:20 +00:00
|
|
|
onUserMenuItemClick={onUserMenuItemClick}
|
2022-04-01 06:42:26 +00:00
|
|
|
/>
|
|
|
|
|
</nav>
|
2025-11-07 22:30:51 +00:00
|
|
|
<div className={coreWrapperClassnames}>
|
2022-04-01 06:42:26 +00:00
|
|
|
<FlashMessage
|
|
|
|
|
fullWidth={fullWidthFlash}
|
|
|
|
|
notification={notification}
|
|
|
|
|
onRemoveFlash={hideFlash}
|
2024-10-22 17:52:20 +00:00
|
|
|
pathname={location.pathname}
|
2022-04-01 06:42:26 +00:00
|
|
|
/>
|
2024-05-29 13:41:07 +00:00
|
|
|
|
2022-04-01 06:42:26 +00:00
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CoreLayout;
|