Merge "fix: fix timer in InfoButton component"

This commit is contained in:
Zuul 2022-06-30 14:38:32 +00:00 committed by Gerrit Code Review
commit ec8df085e7

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { Button, Card, Tooltip, Switch } from 'antd'; import { Button, Card, Tooltip, Switch } from 'antd';
import { ExpandOutlined, CompressOutlined } from '@ant-design/icons'; import { ExpandOutlined, CompressOutlined } from '@ant-design/icons';
@ -32,14 +32,13 @@ export default function InfoButton(props) {
} = props; } = props;
const [collapsed, setCollapsed] = useState(defaultCollapsed); const [collapsed, setCollapsed] = useState(defaultCollapsed);
const [auto, setAuto] = useState(ableAuto); const [auto, setAuto] = useState(ableAuto);
const [timer, setTimer] = useState(null);
const [hover, setHover] = useState(false); const [hover, setHover] = useState(false);
const timer = useRef();
const clearTimer = () => { const clearTimer = () => {
if (timer) { if (timer.current) {
clearTimeout(timer); clearTimeout(timer.current);
} }
setTimer(null);
}; };
const open = () => { const open = () => {
@ -55,15 +54,14 @@ export default function InfoButton(props) {
if (collapsed) { if (collapsed) {
return; return;
} }
if (timer) { if (timer.current) {
clearTimer(); clearTimer();
} }
const newTimer = setTimeout(() => { timer.current = setTimeout(() => {
if (!collapsed) { if (!collapsed) {
close(); close();
} }
}, seconds * 1000); }, seconds * 1000);
setTimer(newTimer);
}; };
useEffect(() => { useEffect(() => {
@ -84,6 +82,9 @@ export default function InfoButton(props) {
open(); open();
startTimer(); startTimer();
} }
return () => {
clearTimer();
};
}, [checkValue]); }, [checkValue]);
const onChangeAuto = (checked) => { const onChangeAuto = (checked) => {