|
| 1 | +import { useEffect } from 'react'; |
| 2 | +import { Popover } from 'antd'; |
| 3 | +import clsx from 'clsx'; |
| 4 | +import { useAtom } from 'jotai'; |
| 5 | +import { MousePointerIcon } from 'lucide-react'; |
| 6 | +import { useTranslation } from 'react-i18next'; |
| 7 | + |
| 8 | +import { mouseJigglerModeAtom } from '@/jotai/mouse.ts'; |
| 9 | +import { mouseJiggler } from '@/libs/mouse-jiggler'; |
| 10 | +import * as storage from '@/libs/storage'; |
| 11 | + |
| 12 | +export const Jiggler = () => { |
| 13 | + const { t } = useTranslation(); |
| 14 | + const [jigglerMode, setJigglerMode] = useAtom(mouseJigglerModeAtom); |
| 15 | + |
| 16 | + const mouseJigglerModes: { name: string; value: 'enable' | 'disable' }[] = [ |
| 17 | + { name: t('mouse.jiggler.enable'), value: 'enable' }, |
| 18 | + { name: t('mouse.jiggler.disable'), value: 'disable' } |
| 19 | + ]; |
| 20 | + |
| 21 | + function update(mode: 'enable' | 'disable'): void { |
| 22 | + storage.setMouseJigglerMode(mode); |
| 23 | + setJigglerMode(mode); |
| 24 | + } |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + mouseJiggler.setMode(jigglerMode); |
| 28 | + }, [jigglerMode]); |
| 29 | + |
| 30 | + const content = ( |
| 31 | + <> |
| 32 | + {mouseJigglerModes.map((mode) => ( |
| 33 | + <div |
| 34 | + key={mode.value} |
| 35 | + className={clsx( |
| 36 | + 'my-1 flex cursor-pointer items-center space-x-1 rounded py-1 pr-5 pl-2 hover:bg-neutral-700/50', |
| 37 | + mode.value === jigglerMode ? 'text-blue-500' : 'text-neutral-300' |
| 38 | + )} |
| 39 | + onClick={() => update(mode.value)} |
| 40 | + > |
| 41 | + {mode.name} |
| 42 | + </div> |
| 43 | + ))} |
| 44 | + </> |
| 45 | + ); |
| 46 | + return ( |
| 47 | + <Popover content={content} placement="rightTop" arrow={false} align={{ offset: [13, 0] }}> |
| 48 | + <div className="flex h-[30px] cursor-pointer items-center space-x-1 rounded px-3 text-neutral-300 hover:bg-neutral-700/50"> |
| 49 | + <div className="flex h-[14px] w-[20px] items-end"> |
| 50 | + <MousePointerIcon size={16} /> |
| 51 | + </div> |
| 52 | + <span>{t('mouse.jiggler.title')}</span> |
| 53 | + </div> |
| 54 | + </Popover> |
| 55 | + ); |
| 56 | +}; |
0 commit comments