import React, { type Ref, forwardRef, useContext } from "react"; import { useGo } from "@hooks/router"; import { RouterContext } from "@contexts/router"; import type { GoConfigWithResource } from "../../hooks/router/use-go"; import warnOnce from "warn-once"; type LinkPropsWithGo = { go: Omit; }; type LinkPropsWithTo = { to: string; }; export type LinkProps = React.PropsWithChildren< (LinkPropsWithGo | LinkPropsWithTo) & TProps >; /** * @param to The path to navigate to. * @param go The useGo.go params to navigate to. If `to` provided, this will be ignored. * @returns routerProvider.Link if it is provided, otherwise an anchor tag. */ const LinkComponent = ( props: LinkProps, ref: Ref, ) => { const routerContext = useContext(RouterContext); const LinkFromContext = routerContext?.Link; const goFunction = useGo(); let resolvedTo = ""; if ("go" in props) { if (!routerContext?.go) { warnOnce( true, "[Link]: `routerProvider` is not found. To use `go`, Please make sure that you have provided the `routerProvider` for `` https://refine.dev/docs/routing/router-provider/ \n", ); } resolvedTo = goFunction({ ...props.go, type: "path" }) as string; } if ("to" in props) { resolvedTo = props.to; } if (LinkFromContext) { return ( ); } return ( ); }; export const Link = forwardRef(LinkComponent) as ( props: LinkProps & { ref?: Ref }, ) => ReturnType;