Files
web/components/Common/CustomLink.jsx
Husanjonazamov 64af77101f classify web
2026-02-24 12:52:49 +05:00

30 lines
871 B
JavaScript

"use client";
import { getCurrentLangCode } from "@/redux/reducer/languageSlice";
import { getDefaultLanguageCode } from "@/redux/reducer/settingSlice";
import Link from "next/link";
import { useSelector } from "react-redux";
const CustomLink = ({ href, children, ...props }) => {
const defaultLangCode = useSelector(getDefaultLanguageCode);
const currentLangCode = useSelector(getCurrentLangCode);
const langCode = currentLangCode || defaultLangCode;
// Split hash (#) safely from href
const [baseHref, hash = ""] = href.split("#");
// Append lang param safely
const separator = baseHref.includes("?") ? "&" : "?";
const newHref = `${baseHref}${separator}lang=${langCode}${
hash ? `#${hash}` : ""
}`;
return (
<Link href={newHref} {...props}>
{children}
</Link>
);
};
export default CustomLink;