fix: move useLazyPathname to hooks dir

This commit is contained in:
Christel Westerberg
2024-08-28 15:41:25 +02:00
parent 354567cbf5
commit fd5380a812
3 changed files with 17 additions and 17 deletions

View File

@@ -7,8 +7,8 @@ import { login } from "@/constants/routes/handleAuth"
import Link from "@/components/TempDesignSystem/Link"
import { LinkProps } from "@/components/TempDesignSystem/Link/link"
import useLang from "@/hooks/useLang"
import { useLazyPathname } from "@/hooks/useLazyPathname"
import { trackLoginClick } from "@/utils/tracking"
import { useLazyPathname } from "@/utils/url"
import { TrackingPosition } from "@/types/components/tracking"

16
hooks/useLazyPathname.ts Normal file
View File

@@ -0,0 +1,16 @@
"use client"
import { usePathname } from "next/navigation"
import { useEffect, useState } from "react"
/*** This hook is used to get the current pathname (as reflected in window.location.href) of the page. During ssr, the value from usePathname()
* is the value return from NextResponse.rewrite() (e.g. the path from the app directory) instead of the actual pathname from the URL.
*/
export function useLazyPathname() {
const pathName = usePathname()
const [updatedPathName, setUpdatedPathName] = useState<string | null>(null)
useEffect(() => {
setUpdatedPathName(pathName)
}, [pathName])
return updatedPathName ? updatedPathName : null
}

View File

@@ -1,6 +1,3 @@
import { usePathname } from "next/navigation"
import { useEffect, useState } from "react"
export function removeMultipleSlashes(str: string) {
return str.replaceAll(/\/\/+/g, "/")
}
@@ -12,16 +9,3 @@ export function removeTrailingSlash(pathname: string) {
}
return pathname
}
/*** This hook is used to get the current pathname (as reflected in window.location.href) of the page. During ssr, the value from usePathname()
* is the value return from NextResponse.rewrite() (e.g. the path from the app directory) instead of the actual pathname from the URL.
*/
export function useLazyPathname() {
const pathName = usePathname()
const [updatedPathName, setUpdatedPathName] = useState<string | null>(null)
useEffect(() => {
setUpdatedPathName(pathName)
}, [pathName])
return updatedPathName ? updatedPathName : null
}