From 69c5e2644ca7634820ea8c481a5439e5e6aa3ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Fri, 24 Apr 2020 14:16:01 +0100 Subject: [PATCH] fix(ui): rewrite RenderNonLinkAnnotation with hooks --- .../AlertGrid/AlertGroup/Annotation/index.js | 134 ++++++++---------- 1 file changed, 57 insertions(+), 77 deletions(-) diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/Annotation/index.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/Annotation/index.js index 24e66346d..bb9ab86b3 100644 --- a/ui/src/Components/Grid/AlertGrid/AlertGroup/Annotation/index.js +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/Annotation/index.js @@ -1,8 +1,7 @@ -import React, { Component } from "react"; +import React, { useEffect, useRef } from "react"; import PropTypes from "prop-types"; -import { observable, action } from "mobx"; -import { observer } from "mobx-react"; +import { useLocalStore, observer } from "mobx-react"; import Linkify from "react-linkify"; @@ -17,91 +16,72 @@ import { AlertStore } from "Stores/AlertStore"; import { TooltipWrapper } from "Components/TooltipWrapper"; const RenderNonLinkAnnotation = observer( - class RenderNonLinkAnnotation extends Component { - static propTypes = { - alertStore: PropTypes.instanceOf(AlertStore).isRequired, - name: PropTypes.string.isRequired, - value: PropTypes.string.isRequired, - visible: PropTypes.bool.isRequired, - afterUpdate: PropTypes.func.isRequired, - }; + ({ alertStore, name, value, visible, afterUpdate }) => { + const mountRef = useRef(false); - // keep state of this annotation visibility, this is controlled by user - toggle = observable( - { - visible: true, - show(e) { - // Linkify only handles value, no need to check for links of - // collapsed annotation - this.visible = true; - }, - hide(e) { - this.visible = false; - }, + useEffect(() => { + if (mountRef.current) { + afterUpdate(); + } else { + mountRef.current = true; + } + }); + + const toggle = useLocalStore(() => ({ + visible: visible, + show(e) { + this.visible = true; }, - { - show: action.bound, - hide: action.bound, - } - ); + hide(e) { + this.visible = false; + }, + })); - constructor(props) { - super(props); - - this.toggle.visible = props.visible; - } - - componentDidUpdate() { - const { afterUpdate } = this.props; - - afterUpdate(); - } - - render() { - const { name, value } = this.props; - - const className = - "mb-1 p-1 bg-light d-inline-block rounded components-grid-annotation text-break mw-100"; - - if (!this.toggle.visible) { - return ( - -
- - {name} -
-
- ); - } + const className = + "mb-1 p-1 bg-light d-inline-block rounded components-grid-annotation text-break mw-100"; + if (!toggle.visible) { return ( - -
- - {name}: - - - {value} - - + +
+ + {name}
); } + + return ( + +
+ + {name}: + + + {value} + + +
+
+ ); } ); +RenderNonLinkAnnotation.propTypes = { + alertStore: PropTypes.instanceOf(AlertStore).isRequired, + name: PropTypes.string.isRequired, + value: PropTypes.string.isRequired, + visible: PropTypes.bool.isRequired, + afterUpdate: PropTypes.func.isRequired, +}; const RenderLinkAnnotation = ({ name, value }) => { return (