useMemo instead

This commit is contained in:
Linus Flood
2024-10-21 13:48:09 +02:00
parent 09777ee909
commit c816b9f802

View File

@@ -1,9 +1,7 @@
import { useEffect, useState } from "react"
import { useMemo } from "react"
export const useCheckIfExternalLink = (url: string) => {
const [isExternal, setIsExternal] = useState(false)
useEffect(() => {
return useMemo(() => {
if (typeof window !== "undefined" && url?.length) {
try {
const hostName = window.location.hostname
@@ -12,12 +10,12 @@ export const useCheckIfExternalLink = (url: string) => {
const hostsMatch = hostName === newURL.hostname
const langRouteRegex = /^\/[a-zA-Z]{2}\//
setIsExternal(!hostsMatch || !langRouteRegex.test(newURL.pathname))
return !hostsMatch || !langRouteRegex.test(newURL.pathname)
} catch {
// Don't care. Expecting internal url (#, /my-pages/overview, etc)
return false
}
}
return false
}, [url])
return isExternal
}