Files
whyrating/apps/web/src/modules/common/turbo-link.tsx
2026-02-04 01:55:00 +01:00

53 lines
1.0 KiB
TypeScript

"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import type { ComponentProps } from "react";
type TurboLinkProps = ComponentProps<typeof Link>;
export const TurboLink = ({
onMouseEnter,
onPointerEnter,
onTouchStart,
onFocus,
children,
...props
}: TurboLinkProps) => {
const router = useRouter();
const strHref =
typeof props.href === "string" ? props.href : props.href.href;
const conditionalPrefetch = () => {
if (strHref) {
void router.prefetch(strHref);
}
};
return (
<Link
{...props}
prefetch={false}
onMouseEnter={(e) => {
conditionalPrefetch();
onMouseEnter?.(e);
}}
onPointerEnter={(e) => {
conditionalPrefetch();
onPointerEnter?.(e);
}}
onTouchStart={(e) => {
conditionalPrefetch();
onTouchStart?.(e);
}}
onFocus={(e) => {
conditionalPrefetch();
onFocus?.(e);
}}
>
{children}
</Link>
);
};