diff --git a/client/src/javascript/components/sidebar/TransferData.tsx b/client/src/javascript/components/sidebar/TransferData.tsx index 10a1d811..623e33bb 100644 --- a/client/src/javascript/components/sidebar/TransferData.tsx +++ b/client/src/javascript/components/sidebar/TransferData.tsx @@ -1,108 +1,80 @@ -import {observer} from 'mobx-react'; +import {FC, useState, useRef, useEffect} from 'react'; import Measure from 'react-measure'; -import * as React from 'react'; +import {observer} from 'mobx-react'; +import {reaction} from 'mobx'; import ClientStatusStore from '../../stores/ClientStatusStore'; +import TransferDataStore from '../../stores/TransferDataStore'; import TransferRateDetails from './TransferRateDetails'; import TransferRateGraph from './TransferRateGraph'; import type {TransferRateGraphInspectorPoint} from './TransferRateGraph'; -interface TransferDataStates { - graphInspectorPoint: TransferRateGraphInspectorPoint | null; - sidebarWidth: number; -} +const TransferData: FC = observer(() => { + const [graphInspectorPoint, setGraphInspectorPoint] = useState(null); + const [sidebarWidth, setSidebarWidth] = useState(0); + const rateGraphRef = useRef(null); -@observer -class TransferData extends React.Component { - rateGraphRef: TransferRateGraph | null = null; - - constructor(props: unknown) { - super(props); - - this.state = { - graphInspectorPoint: null, - sidebarWidth: 0, - }; - } - - handleGraphHover = (graphInspectorPoint: TransferDataStates['graphInspectorPoint']) => { - this.setState({graphInspectorPoint}); - }; - - handleGraphMouseOut = () => { - this.setState({graphInspectorPoint: null}); - }; - - handleMouseMove = (event: React.MouseEvent) => { - if ( - this.rateGraphRef != null && - ClientStatusStore.isConnected && - event && - event.nativeEvent && - event.nativeEvent.clientX != null - ) { - this.rateGraphRef.handleMouseMove(event.nativeEvent.clientX); - } - }; - - handleMouseOut = () => { - if (this.rateGraphRef != null && ClientStatusStore.isConnected) { - this.rateGraphRef.handleMouseOut(); - } - }; - - handleMouseOver = () => { - if (this.rateGraphRef != null && ClientStatusStore.isConnected) { - this.rateGraphRef.handleMouseOver(); - } - }; - - renderTransferRateGraph() { - const {sidebarWidth} = this.state; - - if (!ClientStatusStore.isConnected) return null; - - return ( - { - this.rateGraphRef = ref; - }} - width={sidebarWidth} - /> + useEffect(() => { + const dispose = reaction( + () => TransferDataStore.transferRates, + () => { + if (rateGraphRef.current != null) { + rateGraphRef.current.handleTransferHistoryChange(); + } + }, ); - } - render() { - const {graphInspectorPoint} = this.state; + return dispose; + }, []); - return ( - { - if (contentRect.offset != null) { - this.setState({sidebarWidth: contentRect.offset.width}); - } - }}> - {({measureRef}) => ( -
-
- - {this.renderTransferRateGraph()} -
+ return ( + { + if (contentRect.offset != null) { + setSidebarWidth(contentRect.offset.width); + } + }}> + {({measureRef}) => ( +
+
{ + if (rateGraphRef.current != null && event?.nativeEvent?.clientX != null) { + rateGraphRef.current.handleMouseMove(event.nativeEvent.clientX); + } + }} + onMouseOut={() => { + if (rateGraphRef.current != null) { + rateGraphRef.current.handleMouseOut(); + } + }} + onMouseOver={() => { + if (rateGraphRef.current != null) { + rateGraphRef.current.handleMouseOver(); + } + }}> + + {ClientStatusStore.isConnected && ( + { + setGraphInspectorPoint(null); + }} + onHover={(inspectorPoint) => { + setGraphInspectorPoint(inspectorPoint); + }} + ref={rateGraphRef} + width={sidebarWidth} + /> + )}
- )} - - ); - } -} +
+ )} +
+ ); +}); export default TransferData; diff --git a/client/src/javascript/components/sidebar/TransferRateGraph.tsx b/client/src/javascript/components/sidebar/TransferRateGraph.tsx index 25b35aa5..e7791886 100644 --- a/client/src/javascript/components/sidebar/TransferRateGraph.tsx +++ b/client/src/javascript/components/sidebar/TransferRateGraph.tsx @@ -1,15 +1,20 @@ import {area, curveMonotoneX, line} from 'd3-shape'; +import {Component, FC} from 'react'; import {max} from 'd3-array'; -import {observer} from 'mobx-react'; -import {reaction} from 'mobx'; import {ScaleLinear, scaleLinear} from 'd3-scale'; import {Selection, select} from 'd3-selection'; -import * as React from 'react'; import type {TransferDirection} from '@shared/types/TransferData'; import TransferDataStore, {TRANSFER_DIRECTIONS} from '../../stores/TransferDataStore'; +const TransferRateGraphGradient: FC<{direction: TransferDirection}> = ({direction}: {direction: TransferDirection}) => ( + + + + +); + export interface TransferRateGraphInspectorPoint { uploadSpeed: number; downloadSpeed: number; @@ -24,17 +29,7 @@ interface TransferRateGraphProps { onMouseOut: () => void; } -@observer -class TransferRateGraph extends React.Component { - private static getGradient(slug: TransferDirection): React.ReactNode { - return ( - - - - - ); - } - +class TransferRateGraph extends Component { lastMouseX?: number; xScale?: ScaleLinear; yScale?: ScaleLinear; @@ -61,17 +56,6 @@ class TransferRateGraph extends React.Component { width: 240, }; - constructor(props: TransferRateGraphProps) { - super(props); - - reaction( - () => TransferDataStore.transferRates, - () => { - this.handleTransferHistoryChange(); - }, - ); - } - componentDidMount(): void { this.renderGraphData(); } @@ -263,8 +247,8 @@ class TransferRateGraph extends React.Component { this.graphRefs.graph = ref; }}> - {TransferRateGraph.getGradient('upload')} - {TransferRateGraph.getGradient('download')} + + );