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) => {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user