Files
qwerty-learner/src/pages/Typing/components/Switcher/index.tsx
2023-07-16 15:58:48 +08:00

96 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { TypingContext, TypingStateActionType } from '../../store'
import HandPositionIllustration from '../HandPositionIllustration'
import LoopWordSwitcher from '../LoopWordSwitcher'
import Setting from '../Setting'
import SoundSwitcher from '../SoundSwitcher'
import WordDictationSwitcher from '../WordDictationSwitcher'
import Tooltip from '@/components/Tooltip'
import { isOpenDarkModeAtom } from '@/store'
import { useAtom } from 'jotai'
import { useContext } from 'react'
import { useHotkeys } from 'react-hotkeys-hook'
import IconMoon from '~icons/heroicons/moon-solid'
import IconSun from '~icons/heroicons/sun-solid'
import IconLanguage from '~icons/tabler/language'
import IconLanguageOff from '~icons/tabler/language-off'
export default function Switcher() {
const [isOpenDarkMode, setIsOpenDarkMode] = useAtom(isOpenDarkModeAtom)
const { state, dispatch } = useContext(TypingContext) ?? {}
const changeDarkModeState = () => {
setIsOpenDarkMode((old) => !old)
}
const changeTransVisibleState = () => {
if (dispatch) {
dispatch({ type: TypingStateActionType.TOGGLE_TRANS_VISIBLE })
}
}
useHotkeys(
'ctrl+d',
() => {
changeDarkModeState()
},
{ enableOnFormTags: true, preventDefault: true },
[],
)
useHotkeys(
'ctrl+shift+v',
() => {
changeTransVisibleState()
},
{ enableOnFormTags: true, preventDefault: true },
[],
)
return (
<div className="flex items-center justify-center gap-2">
<Tooltip content="音效设置">
<SoundSwitcher />
</Tooltip>
<Tooltip className="h-7 w-7" content="设置单个单词循环">
<LoopWordSwitcher />
</Tooltip>
<Tooltip className="h-7 w-7" content="开关默写模式Ctrl + V">
<WordDictationSwitcher />
</Tooltip>
<Tooltip className="h-7 w-7" content="开关释义显示Ctrl + Shift + V">
<button
className={`p-[2px] ${state?.isTransVisible ? 'text-indigo-500' : 'text-gray-500'} text-lg focus:outline-none`}
type="button"
onClick={(e) => {
changeTransVisibleState()
e.currentTarget.blur()
}}
aria-label="开关释义显示Ctrl + T修改测试"
>
{state?.isTransVisible ? <IconLanguage /> : <IconLanguageOff />}
</button>
</Tooltip>
<Tooltip className="h-7 w-7" content="开关深色模式Ctrl + D">
<button
className={`p-[2px] text-lg text-indigo-500 focus:outline-none`}
type="button"
onClick={(e) => {
changeDarkModeState()
e.currentTarget.blur()
}}
aria-label="开关深色模式Ctrl + D"
>
{isOpenDarkMode ? <IconMoon className="icon" /> : <IconSun className="icon" />}
</button>
</Tooltip>
<Tooltip className="h-7 w-7" content="指法图示">
<HandPositionIllustration></HandPositionIllustration>
</Tooltip>
<Tooltip content="设置">
<Setting />
</Tooltip>
</div>
)
}