|
| 1 | +import { FieldTimeOutlined } from '@ant-design/icons'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | +import { Button, Dropdown, Tooltip, MenuProps } from 'antd'; |
| 4 | +import { ItemType } from 'antd/lib/menu/hooks/useItems'; |
| 5 | +import { usePrefixedTranslation } from 'hooks'; |
| 6 | +import React, { useCallback, useEffect, useMemo, useState } from 'react'; |
| 7 | +import { useStoreActions, useStoreState } from 'store'; |
| 8 | +import { AutoMineMode, Network } from 'types'; |
| 9 | + |
| 10 | +const Styled = { |
| 11 | + Button: styled(Button)` |
| 12 | + margin-left: 8px; |
| 13 | + `, |
| 14 | + RemainingBar: styled.div` |
| 15 | + transition: width 400ms ease-in-out; |
| 16 | + background: #d46b08; |
| 17 | + position: absolute; |
| 18 | + width: 100%; |
| 19 | + height: 3px; |
| 20 | + bottom: 0; |
| 21 | + left: 0; |
| 22 | + `, |
| 23 | +}; |
| 24 | + |
| 25 | +interface Props { |
| 26 | + network: Network; |
| 27 | +} |
| 28 | + |
| 29 | +const getRemainingPercentage = (mode: AutoMineMode, startTime: number) => { |
| 30 | + if (mode === AutoMineMode.AutoOff) return 0; |
| 31 | + else { |
| 32 | + const elapsedTime = Date.now() - startTime; |
| 33 | + const autoMineInterval = 1000 * mode; |
| 34 | + const remainingTime = autoMineInterval - (elapsedTime % autoMineInterval); |
| 35 | + const remainingPercentage = (100 * remainingTime) / autoMineInterval; |
| 36 | + |
| 37 | + return remainingPercentage; |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +const AutoMineButton: React.FC<Props> = ({ network }) => { |
| 42 | + const { l } = usePrefixedTranslation('cmps.designer.AutoMineButton'); |
| 43 | + const { autoMine } = useStoreActions(s => s.network); |
| 44 | + const autoMiner = useStoreState(s => s.network.autoMiners[network.id]); |
| 45 | + const [remainingPercentage, setRemainingPercentage] = useState(0); |
| 46 | + const [tickTimer, setTickTimer] = useState<NodeJS.Timer | undefined>(undefined); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + return () => { |
| 50 | + clearInterval(tickTimer); |
| 51 | + setTickTimer(undefined); |
| 52 | + }; |
| 53 | + }, []); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + let tt = tickTimer; |
| 57 | + clearInterval(tt); |
| 58 | + |
| 59 | + const setPercentage = () => { |
| 60 | + setRemainingPercentage( |
| 61 | + getRemainingPercentage(network.autoMineMode, autoMiner?.startTime || 0), |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + if (network.autoMineMode === AutoMineMode.AutoOff) { |
| 66 | + setPercentage(); |
| 67 | + setTickTimer(undefined); |
| 68 | + } else { |
| 69 | + tt = setInterval(() => { |
| 70 | + setPercentage(); |
| 71 | + }, 1000); |
| 72 | + setTickTimer(tt); |
| 73 | + } |
| 74 | + return () => { |
| 75 | + clearInterval(tt); |
| 76 | + }; |
| 77 | + }, [network.autoMineMode]); |
| 78 | + |
| 79 | + const autoMineStatusShortMap = useMemo(() => { |
| 80 | + return { |
| 81 | + [AutoMineMode.AutoOff]: l('autoOff'), |
| 82 | + [AutoMineMode.Auto30s]: l('autoThirtySecondsShort'), |
| 83 | + [AutoMineMode.Auto1m]: l('autoOneMinuteShort'), |
| 84 | + [AutoMineMode.Auto5m]: l('autoFiveMinutesShort'), |
| 85 | + [AutoMineMode.Auto10m]: l('autoTenMinutesShort'), |
| 86 | + }; |
| 87 | + }, [l]); |
| 88 | + |
| 89 | + const handleAutoMineModeChanged: MenuProps['onClick'] = useCallback( |
| 90 | + info => { |
| 91 | + info.key == AutoMineMode.AutoOff |
| 92 | + ? setRemainingPercentage(0) |
| 93 | + : setRemainingPercentage(100); |
| 94 | + autoMine({ |
| 95 | + mode: +info.key, |
| 96 | + id: network.id, |
| 97 | + }); |
| 98 | + }, |
| 99 | + [network, autoMine], |
| 100 | + ); |
| 101 | + |
| 102 | + function createMenuItem(key: AutoMineMode): ItemType { |
| 103 | + return { |
| 104 | + label: autoMineStatusShortMap[key], |
| 105 | + key: String(key), |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + const menu: MenuProps = { |
| 110 | + selectedKeys: [String(network.autoMineMode || AutoMineMode.AutoOff)], |
| 111 | + onClick: handleAutoMineModeChanged, |
| 112 | + items: [ |
| 113 | + createMenuItem(AutoMineMode.AutoOff), |
| 114 | + { |
| 115 | + type: 'divider', |
| 116 | + }, |
| 117 | + createMenuItem(AutoMineMode.Auto30s), |
| 118 | + createMenuItem(AutoMineMode.Auto1m), |
| 119 | + createMenuItem(AutoMineMode.Auto5m), |
| 120 | + createMenuItem(AutoMineMode.Auto10m), |
| 121 | + ], |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <Tooltip title={l('autoMineBtnTip')}> |
| 126 | + <Dropdown menu={menu} trigger={['hover']} overlayClassName="polar-context-menu"> |
| 127 | + <Styled.Button icon={<FieldTimeOutlined />} loading={autoMiner?.mining}> |
| 128 | + {l('autoMine')}: {autoMineStatusShortMap[network.autoMineMode]} |
| 129 | + <Styled.RemainingBar |
| 130 | + style={{ |
| 131 | + width: `${remainingPercentage}%`, |
| 132 | + }} |
| 133 | + /> |
| 134 | + </Styled.Button> |
| 135 | + </Dropdown> |
| 136 | + </Tooltip> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +export default AutoMineButton; |
0 commit comments