Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加打字数据监控 #623

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/pages/Analysis/hooks/useWordStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ export function useWordStats(startTimeStamp: number, endTimeStamp: number) {
return wordStats
}

export function useRawWordRecords() {
const [wordRecords, setWordRecords] = useState<IWordRecord[]>([])

useEffect(() => {
const fetchWordRecords = async () => {
const records = await db.wordRecords.toArray()
setWordRecords(records)
}

fetchWordRecords()
}, [])

return wordRecords
}

async function getChapterStats(startTimeStamp: number, endTimeStamp: number): Promise<IWordStats> {
// indexedDB查找某个数字范围内的数据
const records: IWordRecord[] = await db.wordRecords.where('timeStamp').between(startTimeStamp, endTimeStamp).toArray()
Expand Down
12 changes: 11 additions & 1 deletion src/pages/Analysis/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import HeatmapCharts from './components/HeatmapCharts'
import KeyboardWithBarCharts from './components/KeyboardWithBarCharts'
import LineCharts from './components/LineCharts'
import { useWordStats } from './hooks/useWordStats'
import { useRawWordRecords, useWordStats } from './hooks/useWordStats'
import Layout from '@/components/Layout'
import { isOpenDarkModeAtom } from '@/store'
import * as ScrollArea from '@radix-ui/react-scroll-area'
Expand Down Expand Up @@ -39,6 +39,9 @@ const Analysis = () => {
dayjs().subtract(1, 'year').unix(),
dayjs().unix(),
)
const wordRecords = useRawWordRecords()
const wordRecordsJson = JSON.stringify(wordRecords)
const wordRecordsHref = 'data:application/json;base64,' + btoa(wordRecordsJson) // can not avoid this btoa for Buffer is excluded by vite

return (
<Layout>
Expand Down Expand Up @@ -67,6 +70,13 @@ const Analysis = () => {
<div className="mx-4 my-8 h-80 w-auto overflow-hidden rounded-lg p-8 shadow-lg dark:bg-gray-700 dark:bg-opacity-50">
<KeyboardWithBarCharts title="按键错误次数排行" name="错误次数" data={wrongTimeRecord} />
</div>
<div className="mx-4 my-8 h-auto w-auto overflow-hidden rounded-lg p-8 shadow-lg dark:bg-gray-700 dark:bg-opacity-50">
<button>
<a href={wordRecordsHref} download="wordRecords.json">
单词记录下载
</a>
</button>
</div>
</>
)}
</ScrollArea.Viewport>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export default function WordComponent({ word, onFinish }: { word: Word; onFinish
)

const handleHoverWord = useCallback((checked: boolean) => {
if (checked && isShowAnswerOnHover) {
setWordState((state) => {
state.hasPeeked = true
})
}
setIsHoveringWord(checked)
}, [])

Expand Down Expand Up @@ -242,6 +247,8 @@ export default function WordComponent({ word, onFinish }: { word: Word; onFinish
wrongCount: wordState.wrongCount,
letterTimeArray: wordState.letterTimeArray,
letterMistake: wordState.letterMistake,
hidding: wordDictationConfig.isOpen ? wordDictationConfig.type : 'off',
hasPeeked: wordState.hasPeeked,
})

onFinish()
Expand Down
2 changes: 2 additions & 0 deletions src/pages/Typing/components/WordPanel/components/Word/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type WordState = {
letterMistake: LetterMistakes
// 用于随机隐藏字母功能
randomLetterVisible: boolean[]
hasPeeked: boolean
}

export const initialWordState: WordState = {
Expand All @@ -37,4 +38,5 @@ export const initialWordState: WordState = {
letterTimeArray: [],
letterMistake: {},
randomLetterVisible: [],
hasPeeked: false,
}
6 changes: 5 additions & 1 deletion src/utils/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,23 @@ export function useSaveWordRecord() {
wrongCount,
letterTimeArray,
letterMistake,
hidding,
hasPeeked,
}: {
word: string
wrongCount: number
letterTimeArray: number[]
letterMistake: LetterMistakes
hidding: string
hasPeeked: boolean
}) => {
const timing = []
for (let i = 1; i < letterTimeArray.length; i++) {
const diff = letterTimeArray[i] - letterTimeArray[i - 1]
timing.push(diff)
}

const wordRecord = new WordRecord(word, dictID, currentChapter, timing, wrongCount, letterMistake)
const wordRecord = new WordRecord(word, dictID, currentChapter, timing, wrongCount, letterMistake, hidding, hasPeeked)

let dbID = -1
try {
Expand Down
16 changes: 14 additions & 2 deletions src/utils/db/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@ export class WordRecord implements IWordRecord {
timing: number[]
wrongCount: number
mistakes: LetterMistakes

constructor(word: string, dict: string, chapter: number | null, timing: number[], wrongCount: number, mistakes: LetterMistakes) {
hidding: string
hasPeeked: boolean
constructor(
word: string,
dict: string,
chapter: number | null,
timing: number[],
wrongCount: number,
mistakes: LetterMistakes,
hidding: string,
hasPeeked: boolean,
) {
this.word = word
this.timeStamp = getUTCUnixTimestamp()
this.dict = dict
this.chapter = chapter
this.timing = timing
this.wrongCount = wrongCount
this.mistakes = mistakes
this.hidding = hidding
this.hasPeeked = hasPeeked
}

get totalTime() {
Expand Down
Loading