PR fixes
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import { useEffect, useState } from "react"
|
|
||||||
|
|
||||||
import { dt } from "@/lib/dt"
|
import { dt } from "@/lib/dt"
|
||||||
|
|
||||||
@@ -14,24 +13,6 @@ import styles from "./stay.module.css"
|
|||||||
|
|
||||||
import type { StayCardProps } from "@/types/components/myPages/stays/stayCard"
|
import type { StayCardProps } from "@/types/components/myPages/stays/stayCard"
|
||||||
|
|
||||||
const useCheckIfExternal = (bookingUrl: string) => {
|
|
||||||
const [isExternal, setIsExternal] = useState(false)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof window !== "undefined") {
|
|
||||||
const hostName = window.location.hostname
|
|
||||||
const bookingURL = new URL(bookingUrl)
|
|
||||||
|
|
||||||
const hostsMatch = hostName === bookingURL.hostname
|
|
||||||
const langRouteRegex = /^\/[a-zA-Z]{2}\//
|
|
||||||
|
|
||||||
setIsExternal(!hostsMatch || !langRouteRegex.test(bookingURL.pathname))
|
|
||||||
}
|
|
||||||
}, [bookingUrl])
|
|
||||||
|
|
||||||
return isExternal
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function StayCard({ stay }: StayCardProps) {
|
export default function StayCard({ stay }: StayCardProps) {
|
||||||
const lang = useLang()
|
const lang = useLang()
|
||||||
|
|
||||||
@@ -45,13 +26,8 @@ export default function StayCard({ stay }: StayCardProps) {
|
|||||||
const departDate = depart.format("DD MMM YYYY")
|
const departDate = depart.format("DD MMM YYYY")
|
||||||
const departDateTime = depart.format("YYYY-MM-DD")
|
const departDateTime = depart.format("YYYY-MM-DD")
|
||||||
|
|
||||||
// TODO: Remove this check (and hook) and only return <Link /> when current web is deleted
|
return (
|
||||||
const isExternal = useCheckIfExternal(bookingUrl)
|
<Link href={bookingUrl} className={styles.link}>
|
||||||
|
|
||||||
const linkProps = {
|
|
||||||
href: bookingUrl,
|
|
||||||
className: styles.link,
|
|
||||||
children: (
|
|
||||||
<article className={styles.stay}>
|
<article className={styles.stay}>
|
||||||
<Image
|
<Image
|
||||||
className={styles.image}
|
className={styles.image}
|
||||||
@@ -76,8 +52,6 @@ export default function StayCard({ stay }: StayCardProps) {
|
|||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
</article>
|
</article>
|
||||||
),
|
</Link>
|
||||||
}
|
)
|
||||||
|
|
||||||
return isExternal ? <a {...linkProps} /> : <Link {...linkProps} />
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { startTransition, useCallback, useMemo } from "react"
|
|||||||
|
|
||||||
import useRouterTransitionStore from "@/stores/router-transition"
|
import useRouterTransitionStore from "@/stores/router-transition"
|
||||||
|
|
||||||
|
import { useCheckIfExternalLink } from "@/hooks/useCheckIfExternalLink"
|
||||||
import { trackClick, trackPageViewStart } from "@/utils/tracking"
|
import { trackClick, trackPageViewStart } from "@/utils/tracking"
|
||||||
|
|
||||||
import { linkVariants } from "./variants"
|
import { linkVariants } from "./variants"
|
||||||
@@ -55,6 +56,9 @@ export default function Link({
|
|||||||
return `${href}${search}`
|
return `${href}${search}`
|
||||||
}, [href, searchParams, keepSearchParams])
|
}, [href, searchParams, keepSearchParams])
|
||||||
|
|
||||||
|
// TODO: Remove this check (and hook) and only return <Link /> when current web is deleted
|
||||||
|
const isExternal = useCheckIfExternalLink(href)
|
||||||
|
|
||||||
const startRouterTransition = useRouterTransitionStore(
|
const startRouterTransition = useRouterTransitionStore(
|
||||||
(state) => state.startRouterTransition
|
(state) => state.startRouterTransition
|
||||||
)
|
)
|
||||||
@@ -65,11 +69,17 @@ export default function Link({
|
|||||||
}
|
}
|
||||||
}, [trackingId])
|
}, [trackingId])
|
||||||
|
|
||||||
return (
|
const linkProps = {
|
||||||
|
href: fullUrl,
|
||||||
|
className: classNames,
|
||||||
|
}
|
||||||
|
|
||||||
|
return isExternal ? (
|
||||||
|
<a {...linkProps} {...props} />
|
||||||
|
) : (
|
||||||
<NextLink
|
<NextLink
|
||||||
scroll={scroll}
|
scroll={scroll}
|
||||||
prefetch={prefetch}
|
prefetch={prefetch}
|
||||||
className={classNames}
|
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
if (onClick) {
|
if (onClick) {
|
||||||
onClick(e)
|
onClick(e)
|
||||||
@@ -94,9 +104,9 @@ export default function Link({
|
|||||||
router.push(fullUrl, { scroll })
|
router.push(fullUrl, { scroll })
|
||||||
})
|
})
|
||||||
}}
|
}}
|
||||||
href={fullUrl}
|
|
||||||
id={trackingId}
|
id={trackingId}
|
||||||
{...props}
|
{...props}
|
||||||
|
{...linkProps}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
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