import type { ComponentProps } from "react"; import { $path, type RouteOptions } from "astro-js-typesafe-routes/path"; type LinkBase = Omit, "href">; /** Internal links require `to` and optional route params */ type InternalLinkProps = LinkBase & RouteOptions & { external?: false; href?: never; // Forbid 'href' on internal links to avoid confusion }; /** External links require `href` and explicit `external={true}` */ type ExternalLinkProps = LinkBase & { external: true; href: string; to?: never; // Forbid 'to' on external links }; export type ReactLinkProps = InternalLinkProps | ExternalLinkProps; export default function Link({ external = false, ...props }: ReactLinkProps) { if (external) { // External link: treat as standard const { children, ...rest } = props as ExternalLinkProps; return {children}; } // Internal link: treat as type-safe route // We extract route-specific props so they don't leak to the tag const { to, params, locale, search, hash, children, ...anchorProps } = props as InternalLinkProps; const resolvedHref = $path({ to, params, locale, search, hash }); return ( {children} ); }