BodyWrapper.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React, { FC, useEffect } from "react";
  2. import { bindActionCreators } from "redux";
  3. import { connect } from "react-redux";
  4. import styled from "styled-components";
  5. import cookie from "js-cookie";
  6. import { Flex } from "reflexbox/styled-components";
  7. import Header from "./Header";
  8. import PageLoading from "./PageLoading";
  9. import { renewAuthUser } from "../actions";
  10. import { initGA, logPageView } from "../helpers/analytics";
  11. import { useStoreState } from "../store";
  12. interface Props {
  13. norenew?: boolean;
  14. pageLoading: boolean;
  15. renewAuthUser: any; // TODO: better typing;
  16. }
  17. const Wrapper = styled.div`
  18. position: relative;
  19. min-height: 100vh;
  20. display: flex;
  21. flex-direction: column;
  22. align-items: center;
  23. box-sizing: border-box;
  24. * {
  25. box-sizing: border-box;
  26. }
  27. *::-moz-focus-inner {
  28. border: none;
  29. }
  30. @media only screen and (max-width: 448px) {
  31. font-size: 14px;
  32. }
  33. `;
  34. const BodyWrapper: FC<Props> = ({ children, norenew, renewAuthUser }) => {
  35. const loading = useStoreState(s => s.loading.loading);
  36. useEffect(() => {
  37. // FIXME: types bro
  38. if (process.env.GOOGLE_ANALYTICS) {
  39. if (!(window as any).GA_INITIALIZED) {
  40. initGA();
  41. (window as any).GA_INITIALIZED = true;
  42. }
  43. logPageView();
  44. }
  45. const token = cookie.get("token");
  46. if (!token || norenew) return undefined;
  47. renewAuthUser(token);
  48. }, []);
  49. const content = loading ? <PageLoading /> : children;
  50. return (
  51. <Wrapper>
  52. <Flex
  53. minHeight="100vh"
  54. width={1}
  55. flex="0 0 auto"
  56. alignItems="center"
  57. flexDirection="column"
  58. >
  59. <Header />
  60. {content}
  61. </Flex>
  62. </Wrapper>
  63. );
  64. };
  65. BodyWrapper.defaultProps = {
  66. norenew: false
  67. };
  68. const mapDispatchToProps = dispatch => ({
  69. renewAuthUser: bindActionCreators(renewAuthUser, dispatch)
  70. });
  71. export default connect(
  72. null,
  73. mapDispatchToProps
  74. )(BodyWrapper);