Files
multica/apps/docs/components/locale-link.tsx
Naiyuan Qing bd1fb10afa chore: react-doctor cleanup — button types, useContext→use(), toSorted, error fixes (#3350)
- Add explicit type="button" to 61 <button> elements missing the attribute
- Replace useContext() with React 19 use() across 16 context consumers
- Replace [...arr].sort() with arr.toSorted() in 12 web/desktop files
  (mobile excluded — Hermes lacks toSorted support)
- Fix rules-of-hooks violation: useSidebar try/catch → useSidebarSafe null check
- Fix nested component definition: useMemo wrapping HeaderRight → useCallback
- Fix missing ARIA: add aria-expanded + aria-controls to combobox in create-squad

React Doctor score: 23 → 30. No behavioral changes, no business logic modified.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 14:57:07 +08:00

49 lines
1.3 KiB
TypeScript

"use client";
import Link from "next/link";
import {
createContext,
use,
type AnchorHTMLAttributes,
type ReactNode,
} from "react";
import { i18n, type Lang } from "@/lib/i18n";
import { prefixLocale } from "@/lib/locale-link";
const DocsLocaleContext = createContext<Lang>(i18n.defaultLanguage as Lang);
// Wraps the rendered MDX subtree so descendant <LocaleLink>s and any
// editorial component using `useDocsLocale()` know which language the page
// was rendered in. Mounted at each docs page entry; never elsewhere.
export function DocsLocaleProvider({
lang,
children,
}: {
lang: Lang;
children: ReactNode;
}) {
return (
<DocsLocaleContext.Provider value={lang}>
{children}
</DocsLocaleContext.Provider>
);
}
export function useDocsLocale(): Lang {
return use(DocsLocaleContext);
}
// Drop-in replacement for the MDX-rendered `<a>` element. Keeps the same
// surface shape as the default `a` from `defaultMdxComponents` but routes
// internal links through the locale prefixer + next/link so client-side
// navigation stays inside the active locale.
export function LocaleLink({
href,
...rest
}: AnchorHTMLAttributes<HTMLAnchorElement> & { href?: string }) {
const lang = useDocsLocale();
if (!href) return <a {...rest} />;
const final = prefixLocale(href, lang);
return <Link href={final} {...rest} />;
}