Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Frontend/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import SignInPage from './src/pages/SignIn'
import WishlistPage from './src/pages/WishlistPage'
import ProfilePage from './src/pages/ProfilePage'
import FriendsPage from './src/pages/FriendsPage'
import ScrollManager from './src/components/ScrollManager'

function App() {
return (
<HashRouter>
<ScrollManager />
<Routes>
<Route path="/" element={<MainPage />} />
<Route path="/GamePage" element={<GamePage />} />
Expand Down
3 changes: 2 additions & 1 deletion Frontend/src/FrontendLogic/MainPageLogicFrontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export function PrepareDiscountGenreSearch(genre: string) {
'GameStart:filtersSearchpage': genre ? [genre] : [],
[DISCOUNT_ONLY_KEY]: true,
'GameStart:querySearchpage': '',
'GameStart:sortOptionSearchpage': '',
'GameStart:sortOptionSearchpage': 'discount-desc',
'GameStart:filterMenuOpenSearchpage': true,
'GameStart:pageSearchpage': 1
}

Expand Down
9 changes: 7 additions & 2 deletions Frontend/src/components/GenreGamesSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,14 @@ export function GenreGamesSection({
</div>
{showSeeMore && (
<div className="genre-see-more">
<Link to="/SearchPage" onClick={OpenDiscountsInSearch}>
<Button
component={Link}
to="/SearchPage"
size="small"
onClick={OpenDiscountsInSearch}
>
See More
</Link>
</Button>
</div>
)}
<div className="genre-games-row">
Expand Down
52 changes: 52 additions & 0 deletions Frontend/src/components/ScrollManager.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useEffect, useLayoutEffect, useRef } from 'react'
import { useLocation, useNavigationType } from 'react-router-dom'

const savedPositions = new Map<string, number>()
const RESTORE_TIMEOUT_MS = 2000
const USER_SCROLL_EVENTS = ['wheel', 'keydown', 'pointerdown', 'touchstart'] as const

//Ensures that the scroll position on a page is restored if the user navigates back to it, but resets to the top of the page if
// navigated to from another page (ex. clicking a passive card to access a game page, or clicking a page in the header)
//Ensures consistency accross browsers.
export default function ScrollManager() {
const location = useLocation()
const navigationType = useNavigationType()
const keyRef = useRef(location.key)

useEffect(() => {
window.history.scrollRestoration = 'manual'
const SavePosition = () => savedPositions.set(keyRef.current, window.scrollY)
window.addEventListener('scroll', SavePosition, { passive: true })
return () => window.removeEventListener('scroll', SavePosition)
}, [])

useLayoutEffect(() => {
keyRef.current = location.key
const target = navigationType === 'POP' ? (savedPositions.get(location.key) ?? 0) : 0

const start = performance.now()
let frame = 0
const TryScroll = () => {
window.scrollTo(0, target)
const reached = Math.abs(window.scrollY - target) < 1
if (!reached && performance.now() - start < RESTORE_TIMEOUT_MS) {
frame = requestAnimationFrame(TryScroll)
}
}
TryScroll()

//Stops restoring as soon as user scrolls themselves.
const Cancel = () => cancelAnimationFrame(frame)
for (const event of USER_SCROLL_EVENTS) {
window.addEventListener(event, Cancel, { passive: true })
}
return () => {
Cancel()
for (const event of USER_SCROLL_EVENTS) {
window.removeEventListener(event, Cancel)
}
}
}, [location.key, navigationType])

return null
}
1 change: 0 additions & 1 deletion Frontend/src/css/ActiveGameCard.css
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@
min-height: clamp(6rem, 28vh, 20rem);
gap: 0.65rem;
overflow-y: auto;
overscroll-behavior: contain;
scrollbar-gutter: stable;
}

Expand Down
3 changes: 2 additions & 1 deletion Frontend/src/tests/MainPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ describe('MainPage', () => {
expect(sessionStorage.getItem('GameStart:filtersSearchpage')).toBe('["Adventure"]')
expect(sessionStorage.getItem('GameStart:discountedOnlySearchpage')).toBe('true')
expect(sessionStorage.getItem('GameStart:querySearchpage')).toBe('""')
expect(sessionStorage.getItem('GameStart:sortOptionSearchpage')).toBe('""')
expect(sessionStorage.getItem('GameStart:sortOptionSearchpage')).toBe('"discount-desc"')
expect(sessionStorage.getItem('GameStart:filterMenuOpenSearchpage')).toBe('true')
expect(sessionStorage.getItem('GameStart:pageSearchpage')).toBe('1')
})

Expand Down