Merged in fix/SW-3026-prod---users-are-not-focused-on- (pull request #2379)
fix: SW-3026 Fixed scroll issue * fix: SW-3026 Fixed scroll issue * fix: SW-3026 Moved to global solution for scroll to top after page navigation Approved-by: Tobias Johansson Approved-by: Linus Flood
This commit is contained in:
56
apps/scandic-web/components/RouteChange.tsx
Normal file
56
apps/scandic-web/components/RouteChange.tsx
Normal file
@@ -0,0 +1,56 @@
|
||||
"use client"
|
||||
|
||||
import { usePathname, useSearchParams } from "next/navigation"
|
||||
import { startTransition, useEffect } from "react"
|
||||
|
||||
import useRouterTransitionStore from "@/stores/router-transition"
|
||||
import useTrackingStore from "@/stores/tracking"
|
||||
|
||||
import useLang from "@/hooks/useLang"
|
||||
import { trackPageViewStart } from "@/utils/tracking"
|
||||
|
||||
export default function RouteChange() {
|
||||
const pathName = usePathname()
|
||||
const searchParams = useSearchParams()
|
||||
const currentLang = useLang()
|
||||
|
||||
const {
|
||||
setInitialPageLoadTime,
|
||||
updateRouteInfo,
|
||||
hasPathOrLangChanged,
|
||||
hasBookingFlowParamsChanged,
|
||||
} = useTrackingStore()
|
||||
|
||||
const { startRouterTransition } = useRouterTransitionStore()
|
||||
|
||||
useEffect(() => {
|
||||
if (pathName.includes("payment-callback")) {
|
||||
return
|
||||
}
|
||||
|
||||
if (hasPathOrLangChanged()) {
|
||||
// Scroll to top on page load whenever page is navigated to new URL (page)
|
||||
window.scrollTo({ top: 0, left: 0, behavior: "instant" })
|
||||
}
|
||||
|
||||
updateRouteInfo(pathName, currentLang, searchParams)
|
||||
if (hasPathOrLangChanged() || hasBookingFlowParamsChanged()) {
|
||||
setInitialPageLoadTime(Date.now())
|
||||
trackPageViewStart()
|
||||
startTransition(() => {
|
||||
startRouterTransition()
|
||||
})
|
||||
}
|
||||
}, [
|
||||
pathName,
|
||||
searchParams,
|
||||
currentLang,
|
||||
updateRouteInfo,
|
||||
hasPathOrLangChanged,
|
||||
hasBookingFlowParamsChanged,
|
||||
setInitialPageLoadTime,
|
||||
startRouterTransition,
|
||||
])
|
||||
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user