fix: add workaround if word.trans is not an array of string

This commit is contained in:
KaiyiWing
2023-12-25 21:35:15 +08:00
parent 9158181570
commit abcc9a8672
2 changed files with 18 additions and 3 deletions

View File

@@ -321,7 +321,9 @@
},
{
"name": "stun",
"trans": "使震惊,使晕倒;给(某人)以深刻印象,使深深感动",
"trans": [
"使震惊,使晕倒;给(某人)以深刻印象,使深深感动"
],
"usphone": "stʌn",
"ukphone": "stʌn"
},

View File

@@ -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 }