fix(frontend): Fix i18n types

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-12-18 22:34:10 +01:00
parent 43ada39a1c
commit 6f81c5e194
4 changed files with 9 additions and 7 deletions

View file

@ -5,7 +5,7 @@
*/ */
import { ExternalLink } from './external-link' import { ExternalLink } from './external-link'
import type { TranslatedLinkProps } from './types' import type { TranslatedLinkProps } from './types'
import React from 'react' import React, { useMemo } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
/** /**
@ -17,5 +17,6 @@ import { useTranslation } from 'react-i18next'
*/ */
export const TranslatedExternalLink: React.FC<TranslatedLinkProps> = ({ i18nKey, i18nOption, ...props }) => { export const TranslatedExternalLink: React.FC<TranslatedLinkProps> = ({ i18nKey, i18nOption, ...props }) => {
const { t } = useTranslation() const { t } = useTranslation()
return <ExternalLink text={t(i18nKey, i18nOption)} {...props} /> const text = useMemo(() => (i18nOption ? t(i18nKey, i18nOption) : t(i18nKey)), [i18nKey, i18nOption, t])
return <ExternalLink text={text} {...props} />
} }

View file

@ -5,7 +5,7 @@
*/ */
import { InternalLink } from './internal-link' import { InternalLink } from './internal-link'
import type { TranslatedLinkProps } from './types' import type { TranslatedLinkProps } from './types'
import React from 'react' import React, { useMemo } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
/** /**
@ -17,5 +17,6 @@ import { useTranslation } from 'react-i18next'
*/ */
export const TranslatedInternalLink: React.FC<TranslatedLinkProps> = ({ i18nKey, i18nOption, ...props }) => { export const TranslatedInternalLink: React.FC<TranslatedLinkProps> = ({ i18nKey, i18nOption, ...props }) => {
const { t } = useTranslation() const { t } = useTranslation()
return <InternalLink text={t(i18nKey, i18nOption)} {...props} /> const text = useMemo(() => (i18nOption ? t(i18nKey, i18nOption) : t(i18nKey)), [i18nKey, i18nOption, t])
return <InternalLink text={text} {...props} />
} }

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import type { IconName } from '../fork-awesome/fork-awesome-icon' import type { IconName } from '../fork-awesome/fork-awesome-icon'
import type { TOptions } from 'i18next' import type { TOptionsBase } from 'i18next'
interface GeneralLinkProp { interface GeneralLinkProp {
href: string href: string
@ -20,5 +20,5 @@ export interface LinkWithTextProps extends GeneralLinkProp {
export interface TranslatedLinkProps extends GeneralLinkProp { export interface TranslatedLinkProps extends GeneralLinkProp {
i18nKey: string i18nKey: string
i18nOption?: TOptions i18nOption?: TOptionsBase & Record<string, unknown>
} }

View file

@ -75,7 +75,7 @@ export const UiNotificationBoundary: React.FC<PropsWithChildren> = ({ children }
) )
const showErrorNotification = useCallback( const showErrorNotification = useCallback(
(messageI18nKey: string, messageI18nOptions?: TOptions) => (messageI18nKey: string, messageI18nOptions: Record<string, unknown> = {}) =>
(error: Error): void => { (error: Error): void => {
log.error(t(messageI18nKey, messageI18nOptions), error) log.error(t(messageI18nKey, messageI18nOptions), error)
void dispatchUiNotification('common.errorOccurred', messageI18nKey, { void dispatchUiNotification('common.errorOccurred', messageI18nKey, {