Files
web/hooks/useCheckIfExternalLink.ts
2024-10-21 13:48:09 +02:00

22 lines
602 B
TypeScript

import { useMemo } from "react"
export const useCheckIfExternalLink = (url: string) => {
return useMemo(() => {
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}\//
return !hostsMatch || !langRouteRegex.test(newURL.pathname)
} catch {
// Don't care. Expecting internal url (#, /my-pages/overview, etc)
return false
}
}
return false
}, [url])
}