diff --git a/frontend/src/components/Consensus/ConsensusBlock.tsx b/frontend/src/components/Consensus/ConsensusBlock.tsx index 78332ae..02313be 100644 --- a/frontend/src/components/Consensus/ConsensusBlock.tsx +++ b/frontend/src/components/Consensus/ConsensusBlock.tsx @@ -125,9 +125,8 @@ export class ConsensusBlock extends React.Component { className="legend" key={'block_row_' + this.props.height + '_legend'} > - - {this.displayBlockNumber()} - + + {this.displayBlockNumber()} void; - className?: string; position?: 'left' | 'right' | 'center'; onInit?: (update: UpdateCallback) => void; } @@ -45,82 +44,75 @@ function copyToClipboard(text: string) { document.body.removeChild(el); } -export class Tooltip extends React.Component { - public state = { copied: false }; +export function Tooltip({ + text, + position, + copy, + onInit, +}: Tooltip.Props): JSX.Element { + const [copied, setCopied] = React.useState(false); + const [timer, setTimer] = React.useState(null); + const el = React.useRef(null); - private el: HTMLDivElement; - private timer: NodeJS.Timer | null = null; + const update = React.useCallback( + (newText: string) => { + if (el.current) { + el.current.textContent = newText; + } + }, + [el] + ); - public componentDidMount() { - if (this.props.onInit) { - this.props.onInit(this.update); - } - if (this.props.copy) { - this.props.copy(this.onClick); - } + function restore() { + setCopied(false); + setTimer(null); } - public componentWillUnmount() { - if (this.timer) { - clearTimeout(this.timer); - } - if (this.props.copy) { - this.props.copy(null); + function onClick() { + copyToClipboard(text); + + if (timer) { + clearTimeout(timer); } + + setCopied(true); + + setTimer(setTimeout(restore, 2000)); } - public shouldComponentUpdate( - nextProps: Tooltip.Props, - nextState: Tooltip.State - ) { - return ( - this.props.text !== nextProps.text || - this.state.copied !== nextState.copied - ); + React.useEffect(() => { + if (onInit) { + onInit(update); + } + + if (copy) { + copy(onClick); + } + + return () => { + if (timer) { + clearTimeout(timer); + } + + if (copy) { + copy(null); + } + }; + }, []); + + let tooltipClass = 'Tooltip'; + + if (position && position !== 'center') { + tooltipClass += ` Tooltip-${position}`; } - public render() { - const { text, className, position } = this.props; - const { copied } = this.state; - - let tooltipClass = 'Tooltip'; - - if (position && position !== 'center') { - tooltipClass += ` Tooltip-${position}`; - } - - if (copied) { - tooltipClass += ' Tooltip-copied'; - } - - return ( -
- {copied ? 'Copied to clipboard!' : text} -
- ); + if (copied) { + tooltipClass += ' Tooltip-copied'; } - private onRef = (el: HTMLDivElement) => { - this.el = el; - }; - - private update = (text: string) => { - this.el.textContent = text; - }; - - private onClick = () => { - copyToClipboard(this.props.text); - - if (this.timer) { - clearTimeout(this.timer); - } - - this.setState({ copied: true }); - this.timer = setTimeout(this.restore, 2000); - }; - - private restore = () => { - this.setState({ copied: false }); - this.timer = null; - }; + return ( +
+ {copied ? 'Copied to clipboard!' : text} +
+ ); }