perf: 表情面板增加中英国际化,并优化useStorage的缓存策略

以减少在使用hooks时因异步带来的gap
pull/70/head
moonrailgun 2 years ago
parent 9a4466d844
commit ef41833519

@ -17,6 +17,11 @@ export interface StorageObject {
export const [getStorage, setStorage] = export const [getStorage, setStorage] =
buildRegFn<() => StorageObject>('storage'); buildRegFn<() => StorageObject>('storage');
/**
* hook, gap
*/
const useStorageCache = new Map<string, any>();
/** /**
* *
* @param key * @param key
@ -26,18 +31,24 @@ export function useStorage<T = any>(
key: string, key: string,
defaultValue?: T defaultValue?: T
): [T | undefined, { set: (v: T) => void; save: (v: T) => void }] { ): [T | undefined, { set: (v: T) => void; save: (v: T) => void }] {
const [value, setValue] = useState<T | undefined>(defaultValue); const [value, setValue] = useState<T | undefined>(
useStorageCache.get(key) ?? defaultValue
);
useLayoutEffect(() => { useLayoutEffect(() => {
getStorage() getStorage()
.get(key) .get(key)
.then((data: T) => setValue(data ?? defaultValue)); .then((data: T) => {
setValue(data ?? defaultValue);
useStorageCache.set(key, data ?? defaultValue);
});
}, [key]); }, [key]);
const set = useCallback( const set = useCallback(
(newVal: T) => { (newVal: T) => {
setValue(newVal); setValue(newVal);
getStorage().set(key, newVal); getStorage().set(key, newVal);
useStorageCache.set(key, newVal);
}, },
[key] [key]
); );
@ -46,6 +57,7 @@ export function useStorage<T = any>(
(newVal: T) => { (newVal: T) => {
setValue(newVal); setValue(newVal);
getStorage().save(key, newVal); getStorage().save(key, newVal);
useStorageCache.set(key, newVal);
}, },
[key] [key]
); );

@ -3,7 +3,8 @@ import { isValidStr, useColorScheme, useLanguage } from 'tailchat-shared';
import { emojiData } from './const'; import { emojiData } from './const';
import Picker from '@emoji-mart/react'; import Picker from '@emoji-mart/react';
import type { EmojiData } from './types'; import type { EmojiData } from './types';
import i18n from '@emoji-mart/data/i18n/zh.json'; import i18nZh from '@emoji-mart/data/i18n/zh.json';
import i18nEn from '@emoji-mart/data/i18n/en.json';
import spritesUrl from './twitter.png'; import spritesUrl from './twitter.png';
import './Picker.less'; import './Picker.less';
@ -36,7 +37,7 @@ export const EmojiPicker: React.FC<EmojiPickerProps> = React.memo((props) => {
set="twitter" set="twitter"
data={emojiData} data={emojiData}
theme={isDarkMode ? 'dark' : 'light'} theme={isDarkMode ? 'dark' : 'light'}
i18n={language === 'zh-CN' ? i18n : undefined} i18n={language === 'zh-CN' ? i18nZh : i18nEn}
previewPosition="none" previewPosition="none"
skinTonePosition="none" skinTonePosition="none"
onEmojiSelect={handleSelect} onEmojiSelect={handleSelect}

Loading…
Cancel
Save