impress-2020/src/app/PageLayout.js

133 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-09-06 18:12:34 -07:00
import React from "react";
2020-09-06 18:42:39 -07:00
import { Box, Button, HStack, IconButton, useColorMode } from "@chakra-ui/core";
2020-09-06 18:12:34 -07:00
import { Link } from "react-router-dom";
import { useAuth0 } from "@auth0/auth0-react";
2020-09-06 18:42:39 -07:00
import { ChevronLeftIcon, MoonIcon, SunIcon } from "@chakra-ui/icons";
2020-09-06 18:12:34 -07:00
import useCurrentUser from "./components/useCurrentUser";
2020-09-06 18:42:39 -07:00
// TODO: Replace with lower-res version
import HomepageSplashImg from "../images/homepage-splash.png";
function PageLayout({ children, hideHomeLink }) {
2020-09-06 18:12:34 -07:00
return (
2020-09-06 18:45:20 -07:00
<Box padding="6" paddingTop="3" maxWidth="800px" margin="0 auto">
2020-09-06 18:42:39 -07:00
<Box
width="100%"
display="flex"
alignItems="center"
flexWrap="wrap"
marginBottom="6"
>
{!hideHomeLink && <HomeLink />}
2020-09-06 18:12:34 -07:00
<Box flex="1 0 0" />
<UserLoginLogout />
</Box>
{children}
</Box>
);
}
2020-09-06 18:42:39 -07:00
function HomeLink() {
return (
<Box
as={Link}
to="/"
display="flex"
alignItems="center"
position="relative"
role="group"
transition="transform 0.2s"
_hover={{ transform: "scale(1.1)" }}
_focus={{ outline: "0", transform: "scale(1.1)" }}
>
<Box position="absolute" right="100%">
<ChevronLeftIcon />
</Box>
<Box
as="img"
src={HomepageSplashImg}
alt=""
height="2em"
width="2em"
borderRadius="lg"
boxShadow="md"
/>
<Box
height="2em"
width="2em"
position="absolute"
top="0"
left="0"
right="0"
bottom="0"
borderRadius="lg"
transition="border 0.2s"
_groupFocus={{ border: "2px", borderColor: "green.400" }}
/>
</Box>
);
}
2020-09-06 18:12:34 -07:00
function UserLoginLogout() {
const { isLoading, isAuthenticated, loginWithRedirect, logout } = useAuth0();
const { id, username } = useCurrentUser();
if (isLoading) {
return null;
}
if (isAuthenticated) {
return (
2020-09-06 18:42:39 -07:00
<HStack align="center" spacing="2">
2020-09-06 18:12:34 -07:00
{username && <Box fontSize="sm">Hi, {username}!</Box>}
2020-09-06 18:42:39 -07:00
<ColorModeToggleButton />
2020-09-06 18:12:34 -07:00
{id && (
<Button
as={Link}
to={`/user/${id}/items`}
size="sm"
variant="outline"
>
Items
</Button>
)}
<Button
size="sm"
variant="outline"
onClick={() => logout({ returnTo: window.location.origin })}
>
Log out
</Button>
2020-09-06 18:42:39 -07:00
</HStack>
2020-09-06 18:12:34 -07:00
);
} else {
return (
2020-09-06 18:42:39 -07:00
<HStack align="center" spacing="2">
<ColorModeToggleButton />
<Button size="sm" variant="outline" onClick={() => loginWithRedirect()}>
Log in
</Button>
</HStack>
2020-09-06 18:12:34 -07:00
);
}
}
function ColorModeToggleButton() {
const { colorMode, toggleColorMode } = useColorMode();
return (
<IconButton
aria-label={
colorMode === "light" ? "Switch to dark mode" : "Switch to light mode"
}
icon={colorMode === "light" ? <MoonIcon /> : <SunIcon />}
onClick={toggleColorMode}
2020-09-06 18:42:39 -07:00
variant="outline"
size="sm"
2020-09-06 18:12:34 -07:00
/>
);
}
export default PageLayout;