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