|
| 1 | +import Link from "next/link"; |
| 2 | +import { ElementType } from "react"; |
| 3 | +import { isValidUrl } from "../../../../../../common/utils"; |
| 4 | +import { |
| 5 | + ANCHOR_TARGET, |
| 6 | + REL_ATTRIBUTE, |
| 7 | +} from "../../../../../Links/common/entities"; |
| 8 | +import { |
| 9 | + assertAnchorRelAttribute, |
| 10 | + assertAnchorTargetAttribute, |
| 11 | +} from "../../../../../common/Link/typeGuards"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Returns the component to use for a link based on the given href and client-side navigation flag. |
| 15 | + * @param href - The href attribute to use. |
| 16 | + * @param isClientSide - Whether the link is a client-side navigation. |
| 17 | + * @returns The component to use for the link. |
| 18 | + */ |
| 19 | +export function getComponent(href: string, isClientSide: boolean): ElementType { |
| 20 | + if (isClientSide) return Link; // Use Next/Link for client-side navigation. |
| 21 | + if (isValidUrl(href)) return "a"; // Use anchor tag for external links. |
| 22 | + return "span"; // Use span for invalid links. |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Returns the rel attribute for a link based on the given rel and client-side navigation flag. |
| 27 | + * @param rel - The rel attribute to use. |
| 28 | + * @param isClientSideNavigation - Whether the link is a client-side navigation. |
| 29 | + * @returns The rel attribute for the link. |
| 30 | + */ |
| 31 | +export function getRelAttribute( |
| 32 | + rel: string | undefined, |
| 33 | + isClientSideNavigation: boolean |
| 34 | +): string { |
| 35 | + if (rel) { |
| 36 | + assertAnchorRelAttribute(rel); |
| 37 | + return rel; |
| 38 | + } |
| 39 | + return isClientSideNavigation |
| 40 | + ? REL_ATTRIBUTE.NO_OPENER |
| 41 | + : REL_ATTRIBUTE.NO_OPENER_NO_REFERRER; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Returns the target attribute for a link based on the given target and client-side navigation flag. |
| 46 | + * @param target - The target attribute to use. |
| 47 | + * @param isClientSideNavigation - Whether the link is a client-side navigation. |
| 48 | + * @returns The target attribute for the link. |
| 49 | + */ |
| 50 | +export function getTargetAttribute( |
| 51 | + target: string | undefined, |
| 52 | + isClientSideNavigation: boolean |
| 53 | +): string { |
| 54 | + if (target) { |
| 55 | + assertAnchorTargetAttribute(target); |
| 56 | + return target; |
| 57 | + } |
| 58 | + return isClientSideNavigation ? ANCHOR_TARGET.SELF : ANCHOR_TARGET.BLANK; |
| 59 | +} |
0 commit comments