69 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-10-28 20:29:04 +08:00
import { css, cx } from '@emotion/css'
2022-04-09 00:28:37 +08:00
import Router from './Router'
2022-10-28 20:29:04 +08:00
import useIntersectionObserver from '@/web/hooks/useIntersectionObserver'
import uiStates from '@/web/states/uiStates'
import { useEffect, useRef, useState } from 'react'
import { breakpoint as bp, ease } from '@/web/utils/const'
import { useSnapshot } from 'valtio'
import persistedUiStates from '@/web/states/persistedUiStates'
import { motion, useAnimation } from 'framer-motion'
import { sleep } from '@/web/utils/common'
import player from '@/web/states/player'
2022-03-13 14:40:38 +08:00
const Main = () => {
2022-10-28 20:29:04 +08:00
const playerSnapshot = useSnapshot(player)
// Show/hide topbar background
const observePoint = useRef<HTMLDivElement | null>(null)
const { onScreen } = useIntersectionObserver(observePoint)
useEffect(() => {
uiStates.hideTopbarBackground = onScreen
return () => {
uiStates.hideTopbarBackground = false
}
}, [onScreen])
// Change width when player is minimized
const { minimizePlayer } = useSnapshot(persistedUiStates)
const [isMaxWidth, setIsMaxWidth] = useState(minimizePlayer)
const controlsMain = useAnimation()
useEffect(() => {
const animate = async () => {
await controlsMain.start({ opacity: 0 })
await sleep(100)
setIsMaxWidth(minimizePlayer)
await controlsMain.start({ opacity: 1 })
}
if (minimizePlayer !== isMaxWidth) animate()
}, [controlsMain, isMaxWidth, minimizePlayer])
2022-03-13 14:40:38 +08:00
return (
2022-10-28 20:29:04 +08:00
<motion.main
id='main'
animate={controlsMain}
transition={{ ease, duration: 0.4 }}
className={cx(
'no-scrollbar z-10 h-screen overflow-y-auto',
css`
${bp.lg} {
margin-left: 144px;
margin-right: ${isMaxWidth || !playerSnapshot.track ? 92 : 382}px;
}
`
)}
2022-03-13 14:40:38 +08:00
>
2022-10-28 20:29:04 +08:00
<div ref={observePoint}></div>
<div
className={css`
margin-top: 132px;
`}
>
2022-03-13 14:40:38 +08:00
<Router />
2022-10-28 20:29:04 +08:00
</div>
</motion.main>
2022-03-13 14:40:38 +08:00
)
}
export default Main