Skip to content

Commit 71c6f86

Browse files
committed
Merge branch 'dev' into listen-to-feature
2 parents c6b1d45 + 2e2f886 commit 71c6f86

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/components/Simulator/index.tsx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ReactNode, useEffect, useMemo, useState } from "react"
2-
import { useRouter } from "next/router"
2+
import { useSearchParams } from "next/navigation"
33

44
import { trackCustomEvent } from "@/lib/utils/matomo"
55

@@ -18,9 +18,9 @@ import { Phone } from "./Phone"
1818
import { SimulatorModal } from "./SimulatorModal"
1919
import { Template } from "./Template"
2020
import type { PathId, SimulatorData } from "./types"
21-
import { getValidPathId } from "./utils"
21+
import { getValidPathId, isValidPathId } from "./utils"
2222

23-
import { usePathname } from "@/i18n/routing"
23+
import { usePathname, useRouter } from "@/i18n/routing"
2424

2525
type SimulatorProps = {
2626
children: ReactNode
@@ -29,21 +29,21 @@ type SimulatorProps = {
2929
export const Simulator = ({ children, data }: SimulatorProps) => {
3030
const router = useRouter()
3131
const pathname = usePathname()
32+
const searchParams = useSearchParams()
3233

3334
// Track step
3435
const [step, setStep] = useState(0) // 0-indexed to use as array index
3536

3637
// Track pathID
37-
const pathId = getValidPathId(router.query.sim as string)
38+
const pathId = getValidPathId(searchParams.get(PATH_ID_QUERY_PARAM))
3839
const simulator: SimulatorDetails | null = pathId ? data[pathId] : null
3940
const totalSteps: number = simulator ? simulator.explanations.length : 0
4041

4142
// If pathId present, modal is open, else closed
4243
const isOpen = !!pathId
4344

4445
const clearUrlParams = () => {
45-
const pathWithoutParams = pathname.replace(/\?[^#]*/, "")
46-
router.replace(pathWithoutParams)
46+
router.replace(pathname, { scroll: false })
4747
}
4848

4949
// When simulator closed: log event, clear URL params and close modal
@@ -62,7 +62,9 @@ export const Simulator = ({ children, data }: SimulatorProps) => {
6262
// Remove URL search param if invalid pathId
6363
useEffect(() => {
6464
setStep(0)
65-
if (!pathId) clearUrlParams()
65+
if (pathId && !isValidPathId(pathId)) {
66+
clearUrlParams()
67+
}
6668
// eslint-disable-next-line react-hooks/exhaustive-deps
6769
}, [pathId])
6870

@@ -91,10 +93,15 @@ export const Simulator = ({ children, data }: SimulatorProps) => {
9193

9294
const openPath = (id: PathId): void => {
9395
// Set new pathId in navigation
94-
const params = new URLSearchParams()
95-
params.set(PATH_ID_QUERY_PARAM, id)
96-
const url = `?${params.toString()}#${SIMULATOR_ID}`
97-
router.replace(url)
96+
router.replace(
97+
{
98+
pathname,
99+
query: {
100+
[PATH_ID_QUERY_PARAM]: id,
101+
},
102+
},
103+
{ scroll: false }
104+
)
98105
}
99106

100107
// Navigation object passed to child components

src/components/Simulator/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export const getValidPathId = (pathIdString: string | null): PathId | null => {
66
return pathIdString
77
}
88

9-
const isValidPathId = (pathIdString: string | null): pathIdString is PathId => {
9+
export const isValidPathId = (
10+
pathIdString: string | null
11+
): pathIdString is PathId => {
1012
return PATH_IDS.includes(pathIdString as PathId)
1113
}
1214

0 commit comments

Comments
 (0)