fix(BOOK-405): Pushing to history when opening sidepeek to avoid navigating back inside the booking flow
Approved-by: Chuma Mcphoy (We Ahead)
This commit is contained in:
32
packages/common/hooks/usePopStateHandler.ts
Normal file
32
packages/common/hooks/usePopStateHandler.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, useRef } from "react"
|
||||
|
||||
const callbacks = new Set<() => void>()
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
window.addEventListener("popstate", () => {
|
||||
callbacks.forEach((callback) => callback())
|
||||
})
|
||||
}
|
||||
|
||||
export default function usePopStateHandler(
|
||||
callback: () => void,
|
||||
enabled = true
|
||||
) {
|
||||
const callbackRef = useRef(callback)
|
||||
callbackRef.current = callback
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) {
|
||||
return
|
||||
}
|
||||
|
||||
const handler = () => callbackRef.current()
|
||||
callbacks.add(handler)
|
||||
|
||||
return () => {
|
||||
callbacks.delete(handler)
|
||||
}
|
||||
}, [enabled])
|
||||
}
|
||||
Reference in New Issue
Block a user