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