saleor-dashboard/src/components/AppLayout/AppLayout.tsx

235 lines
7.1 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import LinearProgress from "@material-ui/core/LinearProgress";
2020-09-10 15:37:25 +00:00
import { makeStyles, Theme } from "@material-ui/core/styles";
import useMediaQuery from "@material-ui/core/useMediaQuery";
2019-10-09 15:14:23 +00:00
import { createConfigurationMenu } from "@saleor/configuration";
2019-11-14 14:10:52 +00:00
import useAppState from "@saleor/hooks/useAppState";
2019-06-19 14:40:52 +00:00
import useNavigator from "@saleor/hooks/useNavigator";
import useTheme from "@saleor/hooks/useTheme";
import useUser from "@saleor/hooks/useUser";
2019-10-25 12:54:14 +00:00
import { staffMemberDetailsUrl } from "@saleor/staff/urls";
import React from "react";
2020-09-10 15:20:22 +00:00
import { useIntl } from "react-intl";
import useRouter from "use-react-router";
2019-06-19 14:40:52 +00:00
import Container from "../Container";
2019-11-14 14:10:52 +00:00
import ErrorPage from "../ErrorPage";
2020-08-08 12:59:09 +00:00
import Navigator from "../Navigator";
2020-08-10 12:43:24 +00:00
import NavigatorButton from "../NavigatorButton/NavigatorButton";
2020-09-07 11:28:59 +00:00
import SideBar from "../SideBar";
2020-09-11 13:58:15 +00:00
import SideBarDrawer from "../SideBarDrawer/SideBarDrawer";
2020-09-10 15:20:22 +00:00
import UserChip from "../UserChip";
2019-06-19 14:40:52 +00:00
import AppActionContext from "./AppActionContext";
import AppHeaderContext from "./AppHeaderContext";
2020-09-07 14:37:58 +00:00
import { appLoaderHeight } from "./consts";
2019-08-29 10:55:56 +00:00
import createMenuStructure from "./menuStructure";
2019-06-19 14:40:52 +00:00
import ThemeSwitch from "./ThemeSwitch";
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(
theme => ({
2019-06-19 14:40:52 +00:00
appAction: {
[theme.breakpoints.down("sm")]: {
left: 0,
width: "100%"
},
2019-06-19 14:40:52 +00:00
bottom: 0,
gridColumn: 2,
2019-10-23 15:10:46 +00:00
position: "sticky",
zIndex: 10
2019-06-19 14:40:52 +00:00
},
appLoader: {
height: appLoaderHeight,
2020-09-07 11:28:59 +00:00
marginBottom: theme.spacing(4),
2019-06-19 14:40:52 +00:00
zIndex: 1201
},
2019-10-23 15:10:46 +00:00
appLoaderPlaceholder: {
height: appLoaderHeight,
2020-09-07 11:28:59 +00:00
marginBottom: theme.spacing(4)
2019-10-23 15:10:46 +00:00
},
2020-09-10 15:20:22 +00:00
2019-06-19 14:40:52 +00:00
content: {
2020-09-07 11:28:59 +00:00
flex: 1
2019-06-19 14:40:52 +00:00
},
darkThemeSwitch: {
2019-11-12 16:28:21 +00:00
[theme.breakpoints.down("sm")]: {
2020-09-10 15:11:36 +00:00
marginRight: theme.spacing(1)
2019-11-12 16:28:21 +00:00
},
2019-10-28 16:16:49 +00:00
marginRight: theme.spacing(2)
2019-06-19 14:40:52 +00:00
},
header: {
display: "grid",
gridTemplateAreas: `"headerAnchor headerToolbar"`,
2019-11-12 16:28:21 +00:00
[theme.breakpoints.down("sm")]: {
gridTemplateAreas: `"headerToolbar"
"headerAnchor"`
2019-11-12 16:28:21 +00:00
},
marginBottom: theme.spacing(3)
},
headerAnchor: {
gridArea: "headerAnchor"
},
headerToolbar: {
2019-06-19 14:40:52 +00:00
display: "flex",
gridArea: "headerToolbar",
2019-06-19 14:40:52 +00:00
height: 40,
[theme.breakpoints.down("sm")]: {
height: "auto"
}
2019-06-19 14:40:52 +00:00
},
root: {
2020-09-21 15:19:33 +00:00
[theme.breakpoints.up("md")]: {
display: "flex"
},
2019-08-09 11:14:35 +00:00
width: `100%`
2019-06-19 14:40:52 +00:00
},
spacer: {
flex: 1
},
userBar: {
alignItems: "center",
display: "flex"
},
2020-09-10 15:20:22 +00:00
2019-06-19 14:40:52 +00:00
view: {
flex: 1,
flexGrow: 1,
marginLeft: 0,
2019-10-28 16:16:49 +00:00
paddingBottom: theme.spacing(),
2019-10-23 15:10:46 +00:00
[theme.breakpoints.up("sm")]: {
2019-10-28 16:16:49 +00:00
paddingBottom: theme.spacing(3)
2019-10-23 15:10:46 +00:00
}
},
viewContainer: {
2020-09-07 11:28:59 +00:00
minHeight: `calc(100vh - ${theme.spacing(4) + appLoaderHeight + 120}px)`
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
}),
{
name: "AppLayout"
}
);
2019-06-19 14:40:52 +00:00
interface AppLayoutProps {
children: React.ReactNode;
}
2019-11-14 14:10:52 +00:00
const AppLayout: React.FC<AppLayoutProps> = ({ children }) => {
const classes = useStyles({});
const { isDark, toggleTheme } = useTheme();
const appActionAnchor = React.useRef<HTMLDivElement>();
const appHeaderAnchor = React.useRef<HTMLDivElement>();
const { logout, user } = useUser();
const navigate = useNavigator();
const intl = useIntl();
const [appState, dispatchAppState] = useAppState();
const { location } = useRouter();
2020-08-08 12:59:09 +00:00
const [isNavigatorVisible, setNavigatorVisibility] = React.useState(false);
2020-09-10 15:37:25 +00:00
const isMdUp = useMediaQuery((theme: Theme) => theme.breakpoints.up("md"));
2019-08-29 10:55:56 +00:00
2019-11-14 14:10:52 +00:00
const menuStructure = createMenuStructure(intl);
const configurationMenu = createConfigurationMenu(intl);
const userPermissions = user?.userPermissions || [];
2019-10-09 15:14:23 +00:00
2019-11-14 14:10:52 +00:00
const renderConfigure = configurationMenu.some(section =>
section.menuItems.some(
menuItem =>
!!userPermissions.find(
userPermission => userPermission.code === menuItem.permission
)
)
);
2019-06-19 14:40:52 +00:00
2019-11-14 14:10:52 +00:00
const handleErrorBack = () => {
navigate("/");
dispatchAppState({
payload: {
error: null
},
type: "displayError"
});
};
return (
2020-08-08 12:59:09 +00:00
<>
<Navigator
visible={isNavigatorVisible}
setVisibility={setNavigatorVisibility}
/>
<AppHeaderContext.Provider value={appHeaderAnchor}>
<AppActionContext.Provider value={appActionAnchor}>
<div className={classes.root}>
2020-09-10 15:37:25 +00:00
{isMdUp && (
2020-09-10 15:11:36 +00:00
<SideBar
menuItems={menuStructure}
location={location.pathname}
user={user}
renderConfigure={renderConfigure}
onMenuItemClick={navigate}
/>
2020-09-10 15:37:25 +00:00
)}
2020-09-07 11:28:59 +00:00
<div className={classes.content}>
2020-08-08 12:59:09 +00:00
{appState.loading ? (
<LinearProgress className={classes.appLoader} color="primary" />
) : (
<div className={classes.appLoaderPlaceholder} />
)}
<div className={classes.viewContainer}>
<div>
<Container>
<div className={classes.header}>
<div
className={classes.headerAnchor}
ref={appHeaderAnchor}
/>
<div className={classes.headerToolbar}>
{!isMdUp && (
<SideBarDrawer
menuItems={menuStructure}
location={location.pathname}
user={user}
renderConfigure={renderConfigure}
onMenuItemClick={navigate}
/>
)}
<div className={classes.spacer} />
<div className={classes.userBar}>
<ThemeSwitch
className={classes.darkThemeSwitch}
checked={isDark}
onClick={toggleTheme}
/>
<NavigatorButton
isMac={navigator.platform
.toLowerCase()
.includes("mac")}
onClick={() => setNavigatorVisibility(true)}
/>
<UserChip
onLogout={logout}
onProfileClick={() =>
navigate(staffMemberDetailsUrl(user.id))
}
user={user}
/>
</div>
2019-11-14 14:10:52 +00:00
</div>
2019-06-19 14:40:52 +00:00
</div>
2020-08-08 12:59:09 +00:00
</Container>
</div>
<main className={classes.view}>
{appState.error
? appState.error === "unhandled" && (
<ErrorPage onBack={handleErrorBack} />
)
: children}
</main>
2019-10-30 14:34:24 +00:00
</div>
2020-08-08 12:59:09 +00:00
<div className={classes.appAction} ref={appActionAnchor} />
2019-11-14 14:10:52 +00:00
</div>
</div>
2020-08-08 12:59:09 +00:00
</AppActionContext.Provider>
</AppHeaderContext.Provider>
</>
2019-11-14 14:10:52 +00:00
);
};
2019-06-19 14:40:52 +00:00
export default AppLayout;