add hook to get window size

This commit is contained in:
Matthew Russell 2022-03-01 16:05:31 -08:00
parent 99855e6d65
commit 62187b38dd
3 changed files with 50 additions and 2 deletions

View File

@ -2,7 +2,14 @@ import { gql, useQuery } from '@apollo/client';
import AutoSizer from 'react-virtualized-auto-sizer';
import classNames from 'classnames';
import { useRouter } from 'next/router';
import React, { Children, isValidElement, ReactNode, useEffect } from 'react';
import React, {
Children,
isValidElement,
ReactNode,
useEffect,
useState,
} from 'react';
import debounce from 'lodash/debounce';
import { Market, MarketVariables, Market_market } from './__generated__/Market';
// Top level page query
@ -23,6 +30,7 @@ const MARKET_QUERY = gql`
const MarketPage = () => {
const { query } = useRouter();
const { w } = useWindowSize();
const { data, loading, error } = useQuery<Market, MarketVariables>(
MARKET_QUERY,
{
@ -41,7 +49,7 @@ const MarketPage = () => {
return (
<>
{typeof window !== 'undefined' && window.innerWidth > 1040 ? (
{w > 1050 ? (
<TradeGrid market={data.market} />
) : (
<TradePanels market={data.market} />
@ -246,3 +254,37 @@ const Views = {
positions: Positions,
collateral: Collateral,
};
const useWindowSize = () => {
const [windowSize, setWindowSize] = useState(() => {
if (typeof window !== 'undefined') {
return {
w: window.innerWidth,
h: window.innerHeight,
};
}
// Something sensible for server rendered page
return {
w: 1200,
h: 900,
};
});
useEffect(() => {
const handleResize = debounce((event) => {
setWindowSize({
w: event.target.innerWidth,
h: event.target.innerHeight,
});
}, 300);
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
return windowSize;
};

View File

@ -22,6 +22,7 @@
"core-js": "^3.6.5",
"env-cmd": "^10.1.0",
"graphql": "^15.7.2",
"lodash": "^4.17.21",
"lodash.debounce": "^4.0.8",
"next": "12.0.7",
"postcss": "^8.4.6",

View File

@ -13111,6 +13111,11 @@ lodash@4.x, lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15,
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
log-symbols@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-1.0.2.tgz#376ff7b58ea3086a0f09facc74617eca501e1a18"