53 lines
997 B
TypeScript
53 lines
997 B
TypeScript
import { Link } from "@inertiajs/react";
|
|
import { type ReactNode } from "react";
|
|
|
|
import useIsExternalLink from "@/hooks/use-is-external-link";
|
|
|
|
interface SmartLinkProps {
|
|
href?: string;
|
|
onClick?: () => void;
|
|
type?: "button" | "submit" | "reset";
|
|
className?: string;
|
|
children: ReactNode;
|
|
}
|
|
|
|
export default function SmartLink({
|
|
href,
|
|
onClick,
|
|
type = "button",
|
|
className = "",
|
|
children,
|
|
}: Readonly<SmartLinkProps>) {
|
|
const isExternal = useIsExternalLink(href);
|
|
|
|
if (href) {
|
|
if (isExternal) {
|
|
return (
|
|
<a
|
|
href={href}
|
|
className={className}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
{children}
|
|
</a>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Link href={href} className={className}>
|
|
{children}
|
|
</Link>
|
|
);
|
|
}
|
|
|
|
const handleClick = () => {
|
|
onClick?.();
|
|
};
|
|
|
|
return (
|
|
<button type={type} onClick={handleClick} className={className}>
|
|
{children}
|
|
</button>
|
|
);
|
|
}
|