Gray out passed days, scroll to current day by disabling history restoration in History API

This commit is contained in:
VityaSchel
2023-10-03 18:05:38 +04:00
parent a756e4ee13
commit 256153f7c3
7 changed files with 52 additions and 5 deletions

View File

@@ -7,6 +7,8 @@ import { NavBar } from '@/widgets/navbar'
import { LastUpdateAt } from '@/entities/last-update-at'
import { groups } from '@/shared/data/groups'
import crypto from 'crypto'
import React from 'react'
import { getDayOfWeek } from '@/shared/utils'
type PageProps = NextSerialized<{
schedule: Day[]
@@ -16,6 +18,25 @@ type PageProps = NextSerialized<{
export default function HomePage(props: PageProps) {
const { schedule, parsedAt } = nextDeserializer(props)
React.useEffect(() => {
if (typeof window !== 'undefined') {
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual'
}
const interval = setInterval(async () => {
const today = getDayOfWeek(new Date())
const todayBlock = document.getElementById(today)
if (todayBlock) {
const GAP = 48
const HEADER_HEIGHT = 64
window.scrollTo({ top: todayBlock.offsetTop - GAP - HEADER_HEIGHT })
clearInterval(interval)
}
await new Promise(resolve => setTimeout(resolve, 100))
})
}
}, [schedule])
return (
<>
<NavBar />