File size: 875 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { addLocaleToPathLocaleInFront, useLocale } from '@automattic/i18n-utils';
import { forwardRef } from 'react';
import { useSelector } from 'calypso/state';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';

export const LocalizedLink = forwardRef< HTMLAnchorElement, JSX.IntrinsicElements[ 'a' ] >(
	( { children, href = '', onClick = () => {}, ...props }, ref ) => {
		const isLoggedIn = useSelector( isUserLoggedIn );

		// `addLocaleToPathLocaleInFront` retrieves the active locale differently from `useLocale`.
		// Crucially, it doesn't work with SSR unless we pass the `useLocale` value explicitly
		const locale = useLocale();
		const localizedHref = isLoggedIn ? href : addLocaleToPathLocaleInFront( href, locale );

		return (
			<a { ...props } href={ localizedHref } onClick={ onClick } ref={ ref }>
				{ children }
			</a>
		);
	}
);