From abcc9a8672530189089b7fc775781f525ce87d67 Mon Sep 17 00:00:00 2001 From: KaiyiWing Date: Mon, 25 Dec 2023 21:35:15 +0800 Subject: [PATCH] fix: add workaround if word.trans is not an array of string --- public/dicts/2024HongBao_T1.json | 4 +++- src/pages/Typing/hooks/useWordList.ts | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/public/dicts/2024HongBao_T1.json b/public/dicts/2024HongBao_T1.json index 29ab1613..268a1c2d 100644 --- a/public/dicts/2024HongBao_T1.json +++ b/public/dicts/2024HongBao_T1.json @@ -321,7 +321,9 @@ }, { "name": "stun", - "trans": "使震惊,使晕倒;给(某人)以深刻印象,使深深感动", + "trans": [ + "使震惊,使晕倒;给(某人)以深刻印象,使深深感动" + ], "usphone": "stʌn", "ukphone": "stʌn" }, diff --git a/src/pages/Typing/hooks/useWordList.ts b/src/pages/Typing/hooks/useWordList.ts index 1fc2dd49..24842e85 100644 --- a/src/pages/Typing/hooks/useWordList.ts +++ b/src/pages/Typing/hooks/useWordList.ts @@ -35,8 +35,21 @@ export function useWordList(): UseWordListResult { ? wordList.slice(currentChapter * CHAPTER_LENGTH, (currentChapter + 1) * CHAPTER_LENGTH) : [] - // 记录原始 index - return newWords.map((word, index) => ({ ...word, index })) + return newWords.map((word, index) => { + let trans: string[] + if (Array.isArray(word.trans)) { + trans = word.trans.filter((item) => typeof item === 'string') + } else if (word.trans === null || word.trans === undefined || typeof word.trans === 'object') { + trans = [] + } else { + trans = [String(word.trans)] + } + return { + ...word, + index, + trans, + } + }) }, [isFirstChapter, wordList, currentChapter]) return { words: wordList === undefined ? undefined : words, isLoading, error }