11import styled from 'styled-components' ;
2- import { lazy , Suspense , useEffect } from 'react' ;
2+ import { lazy , Suspense , useCallback , useEffect } from 'react' ;
33import { useRecoilValue , useSetRecoilState } from 'recoil' ;
44import { accountAtom } from 'client/atom/accountAtom' ;
55import { leftSpaceAtom } from 'client/atom/canvasAtom' ;
66import { currentRoomIndexAtom } from 'client/atom/roomAtom' ;
7+ import { LANDSCAPE_WIDTH , PORTRAIT_WIDTH } from 'shared/constant' ;
8+ import { debounce , throttle } from 'shared/util' ;
79import Entrance from './entrance/Entrance' ;
810const Lobby = lazy ( ( ) => import ( './lobby/Lobby' ) ) ;
911const Game = lazy ( ( ) => import ( './game/Game' ) ) ;
1012
1113const MainWrapper = styled . div `
1214 position: relative;
1315
14- width: 800px ;
16+ width: ${ LANDSCAPE_WIDTH } px ;
1517 height: 600px;
1618
17- @media screen and (max-width: 800px ) {
18- width: 500px ;
19+ @media screen and (max-width: ${ LANDSCAPE_WIDTH } px ) {
20+ width: ${ PORTRAIT_WIDTH } px ;
1921 min-height: 700px;
2022 height: calc(var(--vh, 1vh) * 100);
2123 }
@@ -26,36 +28,30 @@ const MainWrapper = styled.div`
2628 user-select: none;
2729` ;
2830
29- const throttle = ( callback , delay ) => {
30- let previousCall = new Date ( ) . getTime ( ) ;
31-
32- return ( ...args ) => {
33- const time = new Date ( ) . getTime ( ) ;
34-
35- if ( time - previousCall >= delay ) {
36- previousCall = time ;
37- callback ( ...args ) ;
38- }
39- } ;
40- } ;
41-
4231const Main = ( ) => {
4332 const isLogined = useRecoilValue ( accountAtom ) !== null ;
4433 const isInRoom = useRecoilValue ( currentRoomIndexAtom ) !== null ;
4534 const setLeftSpace = useSetRecoilState ( leftSpaceAtom ) ;
4635
47- useEffect ( ( ) => {
48- let timer ;
36+ const setHeight = useCallback (
37+ throttle ( ( ) => document . documentElement . style . setProperty ( '--vh' , `${ window . innerHeight * 0.01 } px` ) , 20 ) ,
38+ [ ]
39+ ) ;
40+
41+ const setLeft = useCallback (
42+ debounce ( ( ) => {
43+ const width = window . innerWidth ;
44+ if ( width > LANDSCAPE_WIDTH ) setLeftSpace ( ( width - LANDSCAPE_WIDTH ) / 2 ) ;
45+ else if ( width > PORTRAIT_WIDTH ) setLeftSpace ( ( width - PORTRAIT_WIDTH ) / 2 ) ;
46+ } , 250 ) ,
47+ [ ]
48+ ) ;
4949
50- const resizeHandler = throttle ( ( ) => {
51- document . documentElement . style . setProperty ( '--vh' , `${ window . innerHeight * 0.01 } px` ) ;
52- clearTimeout ( timer ) ;
53- timer = setTimeout ( ( ) => {
54- const width = window . innerWidth ;
55- if ( width > 800 ) setLeftSpace ( ( width - 800 ) / 2 ) ;
56- else if ( width > 500 ) setLeftSpace ( ( width - 500 ) / 2 ) ;
57- } , 250 ) ;
58- } , 100 ) ;
50+ useEffect ( ( ) => {
51+ const resizeHandler = ( ) => {
52+ setHeight ( ) ;
53+ setLeft ( ) ;
54+ } ;
5955
6056 window . addEventListener ( 'resize' , resizeHandler ) ;
6157
@@ -66,7 +62,7 @@ const Main = () => {
6662
6763 return (
6864 < MainWrapper >
69- < Suspense fallback = { < > </ > } > { isLogined ? isInRoom ? < Game /> : < Lobby /> : < Entrance /> } </ Suspense >
65+ < Suspense fallback = { null } > { isLogined ? isInRoom ? < Game /> : < Lobby /> : < Entrance /> } </ Suspense >
7066 </ MainWrapper >
7167 ) ;
7268} ;
0 commit comments