Skip to content

Commit

Permalink
fix(console,ui): fix locale guard issue in settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
charIeszhao committed Sep 7, 2022
1 parent 9851619 commit e200578
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConnectorDto, ConnectorType } from '@logto/schemas';
import { languageKeyGuard } from '@logto/shared';
import { getDefaultLanguage } from '@logto/shared';
import { conditional } from '@silverhand/essentials';
import i18next from 'i18next';
import { Controller, useForm } from 'react-hook-form';
Expand Down Expand Up @@ -31,11 +31,7 @@ const Guide = ({ connector, onClose }: Props) => {
const { updateSettings } = useSettings();
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const { id: connectorId, type: connectorType, name, configTemplate, readme } = connector;

const localeRaw = i18next.language;
const result = languageKeyGuard.safeParse(localeRaw);
const connectorName = result.success ? name[result.data] : name.en;

const connectorName = name[getDefaultLanguage(i18next.language)];
const isSocialConnector =
connectorType !== ConnectorType.Sms && connectorType !== ConnectorType.Email;
const methods = useForm<GuideForm>({ reValidateMode: 'onBlur' });
Expand Down
4 changes: 2 additions & 2 deletions packages/console/src/pages/Settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { languageOptions } from '@logto/phrases';
import { AppearanceMode } from '@logto/schemas';
import { languageKeyGuard } from '@logto/shared';
import { getDefaultLanguage } from '@logto/shared';
import classNames from 'classnames';
import { Controller, useForm } from 'react-hook-form';
import { toast } from 'react-hot-toast';
Expand All @@ -25,7 +25,7 @@ const Settings = () => {
i18n: { language },
} = useTranslation(undefined, { keyPrefix: 'admin_console' });

const defaultLanguage = languageKeyGuard.default('en').parse(language);
const defaultLanguage = getDefaultLanguage(language);

const { data, error, update, isLoading, isLoaded } = useUserPreferences();
const {
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/src/language.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { z } from 'zod';

import { fallback } from './utilities/zod';

export const languageKeys = ['en', 'fr', 'pt-PT', 'zh-CN', 'tr-TR', 'ko-KR'] as const;
export const languageKeyGuard = z.enum(languageKeys);
export type LanguageKey = z.infer<typeof languageKeyGuard>;

export const getDefaultLanguage = (language: string): LanguageKey => {
return languageKeyGuard.or(fallback<LanguageKey>('en')).parse(language);
};
15 changes: 15 additions & 0 deletions packages/shared/src/utilities/zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { any } from 'zod';

/**
* https://github.com/colinhacks/zod/issues/316#issuecomment-850906479
* Create a schema matches anything and returns a value. Use it with `or`:
*
* const schema = zod.number();
* const tolerant = schema.or(fallback(-1));
*
* schema.parse('foo') // => ZodError
* tolerant.parse('foo') // -1
*/
export function fallback<T>(value: T) {
return any().transform(() => value);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { languageKeyGuard } from '@logto/shared';
import { getDefaultLanguage } from '@logto/shared';
import { useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';

Expand Down Expand Up @@ -47,7 +47,7 @@ const SocialSignInDropdown = ({ isOpen, onClose, connectors, anchorRef }: Props)
>
{connectors.map((connector) => {
const { id, name, logo, logoDark } = connector;
const localName = name[languageKeyGuard.default('en').parse(language)];
const localName = name[getDefaultLanguage(language)];

return (
<DropdownItem
Expand Down

0 comments on commit e200578

Please sign in to comment.