mango-ui-v3/components/ManualRefresh.tsx

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-09-09 17:23:02 -07:00
import { useEffect, useState } from 'react'
import { RefreshClockwiseIcon } from './icons'
import useMangoStore from '../stores/useMangoStore'
import Tooltip from './Tooltip'
2021-07-29 06:19:32 -07:00
import { IconButton } from './Button'
import { useTranslation } from 'next-i18next'
const ManualRefresh = ({ className = '' }) => {
const { t } = useTranslation('common')
const [spin, setSpin] = useState(false)
const actions = useMangoStore((s) => s.actions)
2021-12-17 01:51:54 -08:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const handleRefreshData = async () => {
setSpin(true)
2021-09-09 17:23:02 -07:00
await actions.fetchMangoGroup()
2021-12-17 01:51:54 -08:00
if (mangoAccount) {
await actions.reloadMangoAccount()
actions.fetchTradeHistory()
}
}
2021-09-09 17:23:02 -07:00
useEffect(() => {
let timer
if (spin) {
timer = setTimeout(() => setSpin(false), 5000)
2021-09-09 17:23:02 -07:00
}
return () => {
clearTimeout(timer)
}
}, [spin])
return (
<div className={`inline-flex relative ${className}`}>
<Tooltip content={t('refresh-data')} className="text-xs py-1">
2021-09-09 17:23:02 -07:00
<IconButton onClick={handleRefreshData} disabled={spin}>
<RefreshClockwiseIcon
className={`w-4 h-4 ${spin ? 'animate-spin' : null}`}
/>
2021-07-29 06:19:32 -07:00
</IconButton>
</Tooltip>
</div>
)
}
export default ManualRefresh