This commit is contained in:
eric
2026-06-08 06:49:21 -05:00
parent 06ab949d81
commit 874941575c
9 changed files with 364 additions and 9 deletions

View File

@@ -53,7 +53,10 @@ export function LocaleProvider({
const setLocale = useCallback((newLocale: Locale) => {
const path = window.location.pathname;
const newPath = path.replace(/^\/(zh|en)/, `/${newLocale}`);
window.location.href = newPath || `/${newLocale}/digital`;
void import("@/app/lib/navigation-loading").then(({ startNavigation }) => {
startNavigation();
window.location.href = newPath || `/${newLocale}/digital`;
});
}, []);
const value: LocaleContextValue = {
@@ -95,7 +98,10 @@ export function useLocaleRouter() {
? cleanPath
: `/digital${cleanPath === "/" ? "" : cleanPath}`;
const newPath = `/${newLocale}${digitalPath}`;
window.location.href = newPath;
void import("@/app/lib/navigation-loading").then(({ startNavigation }) => {
startNavigation();
window.location.href = newPath;
});
},
};
}

View File

@@ -3,10 +3,14 @@
import NextLink from "next/link";
import { usePathname as useNextPathname } from "next/navigation";
import { useLocale } from "../context/LocaleContext";
import {
isInternalNavigationClick,
startNavigation,
} from "@/app/lib/navigation-loading";
type LocaleLinkProps = React.ComponentProps<typeof NextLink>;
export function LocaleLink({ href, ...props }: LocaleLinkProps) {
export function LocaleLink({ href, onClick, ...props }: LocaleLinkProps) {
const locale = useLocale();
const hrefStr = typeof href === "string" ? href : href.pathname ?? "/";
const localeHref =
@@ -15,7 +19,19 @@ export function LocaleLink({ href, ...props }: LocaleLinkProps) {
: hrefStr.startsWith("/")
? `/${locale}/digital${hrefStr === "/" ? "" : hrefStr}`
: hrefStr;
return <NextLink href={localeHref} {...props} />;
return (
<NextLink
href={localeHref}
onClick={(event) => {
if (isInternalNavigationClick(event.nativeEvent, event.currentTarget)) {
startNavigation();
}
onClick?.(event);
}}
{...props}
/>
);
}
export function usePathname() {