type: fix useLocale type error of site (#48204)

This commit is contained in:
lijianan 2024-04-01 12:07:58 +08:00 committed by GitHub
parent ff231ab557
commit 4eaa664d2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 14 deletions

View File

@ -1,16 +1,22 @@
import { useLocale as useDumiLocale } from 'dumi'; import { useLocale as useDumiLocale } from 'dumi';
export interface LocaleMap<Key extends string> { export interface LocaleMap<
cn: Record<Key, string>; K extends PropertyKey = PropertyKey,
en: Record<Key, string>; V extends string | ((...params: any[]) => string) = string,
> {
cn: Record<K, V>;
en: Record<K, V>;
} }
function useLocale<Key extends string>( const useLocale = <
localeMap?: LocaleMap<Key>, K extends PropertyKey = PropertyKey,
): [Record<Key, string>, 'cn' | 'en'] { V extends string | ((...params: any[]) => string) = string,
>(
localeMap?: LocaleMap<K, V>,
): [Record<K, V>, 'cn' | 'en'] => {
const { id } = useDumiLocale(); const { id } = useDumiLocale();
const localeType = id === 'zh-CN' ? 'cn' : 'en'; const localeType = id === 'zh-CN' ? 'cn' : 'en';
return [localeMap?.[localeType]!, localeType]; return [localeMap?.[localeType]!, localeType] as const;
} };
export default useLocale; export default useLocale;

View File

@ -1,9 +1,10 @@
import { FormattedMessage } from 'dumi'; import React from 'react';
import React, { useMemo, useState } from 'react';
import { ColorPicker } from 'antd'; import { ColorPicker } from 'antd';
import type { Color } from 'antd/es/color-picker'; import type { Color } from 'antd/es/color-picker';
import ColorPatterns from './ColorPatterns'; import { FormattedMessage } from 'dumi';
import useLocale from '../../../hooks/useLocale'; import useLocale from '../../../hooks/useLocale';
import ColorPatterns from './ColorPatterns';
const primaryMinSaturation = 70; // 主色推荐最小饱和度 const primaryMinSaturation = 70; // 主色推荐最小饱和度
const primaryMinBrightness = 70; // 主色推荐最小亮度 const primaryMinBrightness = 70; // 主色推荐最小亮度
@ -22,8 +23,8 @@ const locales = {
}; };
const ColorPaletteTool: React.FC = () => { const ColorPaletteTool: React.FC = () => {
const [primaryColor, setPrimaryColor] = useState<string>('#1890ff'); const [primaryColor, setPrimaryColor] = React.useState<string>('#1890ff');
const [primaryColorInstance, setPrimaryColorInstance] = useState<Color>(null); const [primaryColorInstance, setPrimaryColorInstance] = React.useState<Color>();
const [locale] = useLocale(locales); const [locale] = useLocale(locales);
@ -32,7 +33,7 @@ const ColorPaletteTool: React.FC = () => {
setPrimaryColorInstance(color); setPrimaryColorInstance(color);
}; };
const colorValidation = useMemo<React.ReactNode>(() => { const colorValidation = React.useMemo<React.ReactNode>(() => {
let text = ''; let text = '';
if (primaryColorInstance) { if (primaryColorInstance) {
const { s, b } = primaryColorInstance.toHsb() || {}; const { s, b } = primaryColorInstance.toHsb() || {};