Files
openmonetis/components/navbar/nav-link.tsx
Felipe Coutinho 842919bce5 refactor: substituir topbar por navbar componentizada
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-27 15:40:48 +00:00

31 lines
788 B
TypeScript

"use client";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { useMemo } from "react";
const PERIOD_PARAM = "periodo";
type NavLinkProps = Omit<React.ComponentProps<typeof Link>, "href"> & {
href: string;
preservePeriod?: boolean;
};
export function NavLink({
href,
preservePeriod = false,
...props
}: NavLinkProps) {
const searchParams = useSearchParams();
const resolvedHref = useMemo(() => {
if (!preservePeriod) return href;
const periodo = searchParams.get(PERIOD_PARAM);
if (!periodo) return href;
const separator = href.includes("?") ? "&" : "?";
return `${href}${separator}${PERIOD_PARAM}=${encodeURIComponent(periodo)}`;
}, [href, preservePeriod, searchParams]);
return <Link href={resolvedHref} {...props} />;
}