Skip to content

Commit 3a3745a

Browse files
committed
fix(web): code scalability and abstractions
1 parent c6e32a3 commit 3a3745a

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

web/src/pages/Home/index.tsx

+15-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React, { useEffect, useState } from "react";
1+
import React from "react";
22
import styled, { useTheme } from "styled-components";
3+
import { useMeasure } from "react-use";
34
import CourtOverview from "./CourtOverview";
45
import LatestCases from "./LatestCases";
56
import Community from "./Community";
@@ -19,33 +20,25 @@ const Container = styled.div`
1920
`;
2021

2122
const Header = () => {
22-
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
23-
24-
useEffect(() => {
25-
const handleResize = () => {
26-
setWindowWidth(window.innerWidth);
27-
};
28-
29-
window.addEventListener("resize", handleResize);
30-
31-
return () => {
32-
window.removeEventListener("resize", handleResize);
33-
};
34-
}, []);
35-
23+
const [ref, { width }] = useMeasure();
3624
const theme = useTheme();
3725
const themeIsLight = theme.name === "light";
38-
const breakpointIsBig = windowWidth > BREAKPOINT_SMALL_SCREEN;
26+
const breakpointIsBig = width > BREAKPOINT_SMALL_SCREEN;
3927
return (
40-
<>
41-
{themeIsLight && breakpointIsBig && <HeaderLightDesktop />}
42-
{!themeIsLight && breakpointIsBig && <HeaderDarkDesktop />}
43-
{themeIsLight && !breakpointIsBig && <HeaderLightMobile />}
44-
{!themeIsLight && !breakpointIsBig && <HeaderDarkMobile />}
45-
</>
28+
<div ref={ref}>
29+
{breakpointIsBig ? <HeaderDesktop themeIsLight={themeIsLight} /> : <HeaderMobile themeIsLight={themeIsLight} />}
30+
</div>
4631
);
4732
};
4833

34+
const HeaderDesktop: React.FC<{ themeIsLight: boolean }> = ({ themeIsLight }) => {
35+
return themeIsLight ? <HeaderLightDesktop /> : <HeaderDarkDesktop />;
36+
};
37+
38+
const HeaderMobile: React.FC<{ themeIsLight: boolean }> = ({ themeIsLight }) => {
39+
return themeIsLight ? <HeaderLightMobile /> : <HeaderDarkMobile />;
40+
};
41+
4942
const Home: React.FC = () => {
5043
return (
5144
<HomePageProvider timeframe={getOneYearAgoTimestamp()}>

0 commit comments

Comments
 (0)