Skip to content

Commit 371a337

Browse files
committed
Preserve the current document in translation links
1 parent 2e6dca1 commit 371a337

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎src/components/Layout/TopNav/TopNav.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ export default function TopNav({
169169
const [isScrolled, setIsScrolled] = useState(false);
170170
const scrollParentRef = useRef<HTMLDivElement>(null);
171171
const {asPath} = useRouter();
172+
const translationsHref =
173+
section === 'learn' || section === 'reference'
174+
? `/community/translations?from=${encodeURIComponent(
175+
asPath.split(/[?#]/)[0]
176+
)}`
177+
: '/community/translations';
172178

173179
// HACK. Fix up the data structures instead.
174180
if ((routeTree as any).routes.length === 1) {
@@ -378,7 +384,7 @@ export default function TopNav({
378384
</div>
379385
<div className="flex">
380386
<Link
381-
href="/community/translations"
387+
href={translationsHref}
382388
aria-label="Translations"
383389
className="active:scale-95 transition-transform flex w-12 h-12 rounded-full items-center justify-center hover:bg-primary/5 hover:dark:bg-primary-dark/5 outline-link">
384390
{languageIcon}

‎src/components/MDX/MDXComponents.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import {Children, useContext, useMemo} from 'react';
1313
import * as React from 'react';
1414
import cn from 'classnames';
15+
import {useRouter} from 'next/router';
1516
import type {HTMLAttributes} from 'react';
1617

1718
import CodeBlock from './CodeBlock';
@@ -469,6 +470,9 @@ function InlineTocItem({items}: {items: Array<NestedTocNode>}) {
469470
type TranslationProgress = 'complete' | 'in-progress';
470471

471472
function LanguageList({progress}: {progress: TranslationProgress}) {
473+
const from = useRouter().query.from;
474+
const pathname =
475+
typeof from === 'string' && /^\/(?!\/)[^\\?#]*$/.test(from) ? from : '';
472476
const allLanguages = React.useContext(LanguagesContext) ?? [];
473477
const languages = allLanguages
474478
.filter(
@@ -484,7 +488,10 @@ function LanguageList({progress}: {progress: TranslationProgress}) {
484488
{languages.map(({code, name, enName}) => {
485489
return (
486490
<LI key={code}>
487-
<Link href={`https://${code}.react.dev/`}>
491+
<Link
492+
href={`https://${code}.react.dev${
493+
progress === 'complete' ? pathname || '/' : '/'
494+
}`}>
488495
{enName} ({name})
489496
</Link>{' '}
490497
&mdash;{' '}

0 commit comments

Comments
 (0)