Skip to content

Commit

Permalink
chore: add debounce for translate word
Browse files Browse the repository at this point in the history
  • Loading branch information
ratmirslv committed Jan 26, 2024
1 parent 8c79c0d commit 4fd83e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions components/Word.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDisclosure } from '@mantine/hooks'

import TranslateWordPopup from './TranslateWordPopup'

import useDebounce from '@/hooks/useDebounce'
import { clearWord } from 'utils/clearWord'

interface WordProps {
Expand All @@ -12,7 +13,7 @@ interface WordProps {
function Word(props: WordProps) {
const [opened, { close, open }] = useDisclosure(false)

return (
return useDebounce(
<Popover
width={200}
position="top-start"
Expand All @@ -27,7 +28,8 @@ function Word(props: WordProps) {
</Popover.Dropdown>
</Text>
</Popover.Target>
</Popover>
</Popover>,
300,
)
}

Expand Down
12 changes: 12 additions & 0 deletions hooks/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState, useEffect } from 'react'

export default function useDebounce<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState(value)

useEffect(() => {
const handler = setTimeout(() => setDebouncedValue(value), delay)
return () => clearTimeout(handler)
}, [delay, value])

return debouncedValue
}

0 comments on commit 4fd83e3

Please sign in to comment.