• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1081664682

22 Nov 2023 02:11PM UTC coverage: 82.798% (-17.2%) from 99.964%
1081664682

Pull #4214

gitlab-ci

tranchitella
fix: Fixed the infinite page redirects when the back button is pressed

Remove the location and navigate from the useLocationParams.setValue callback
dependencies as they change the set function that is presented in other
useEffect dependencies. This happens when the back button is clicked, which
leads to the location changing infinitely.

Changelog: Title
Ticket: MEN-6847
Ticket: MEN-6796

Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
Signed-off-by: Fabio Tranchitella <fabio.tranchitella@northern.tech>
Pull Request #4214: fix: Fixed the infinite page redirects when the back button is pressed

4319 of 6292 branches covered (0.0%)

8332 of 10063 relevant lines covered (82.8%)

191.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.44
/src/js/components/devices/troubleshoot/terminal.js
1
// Copyright 2020 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useCallback, useEffect, useMemo, useState } from 'react';
15

16
import { WebLinksAddon } from 'xterm-addon-web-links';
17

18
import { DEVICE_MESSAGE_TYPES as MessageTypes } from '../../../constants/deviceConstants';
19
import { toggle } from '../../../helpers';
20
import useWindowSize from '../../../utils/resizehook';
21
import XTerm from '../../common/xterm';
22

23
export const options = {
12✔
24
  cursorBlink: 'block',
25
  macOptionIsMeta: true
26
};
27

28
// only matching absolute paths, so: /here/there - but not ../here or ./here or here/there
29
const unixPathRegex = new RegExp('(\\/([^\\0\\s!$`&*()\\[\\]+\'":;\\\\])+)');
12✔
30

31
export const Terminal = ({ onDownloadClick, sendMessage, socketInitialized, textInput, xtermRef, ...xtermProps }) => {
12✔
32
  const [dimensions, setDimensions] = useState({});
7✔
33
  const [isVisible, setIsVisible] = useState(false);
7✔
34
  const [fitTrigger, setFitTrigger] = useState(false);
7✔
35
  const size = useWindowSize();
7✔
36

37
  const observer = useMemo(() => new IntersectionObserver(([entry]) => setIsVisible(entry.isIntersecting)), []);
7✔
38

39
  useEffect(() => {
7✔
40
    if (!socketInitialized) {
5✔
41
      return;
4✔
42
    }
43
    observer.observe(xtermRef.current.terminalRef.current);
1✔
44
    // Remove the observer as soon as the component is unmounted
45
    return () => {
1✔
46
      observer.disconnect();
1✔
47
    };
48
  }, [observer, sendMessage, socketInitialized, xtermRef]);
49

50
  useEffect(() => {
7✔
51
    if (!socketInitialized || !xtermRef.current?.terminal?.current) {
5✔
52
      return;
4✔
53
    }
54
    xtermRef.current.terminal.current.reset();
1✔
55
    const webLinksAddon = new WebLinksAddon((e, link) => onDownloadClick(link), { urlRegex: unixPathRegex }, true);
1✔
56
    xtermRef.current.terminal.current.loadAddon(webLinksAddon);
1✔
57
    setFitTrigger(toggle);
1✔
58
    xtermRef.current.terminal.current.focus();
1✔
59
    // eslint-disable-next-line react-hooks/exhaustive-deps
60
  }, [onDownloadClick, socketInitialized, sendMessage]);
61

62
  useEffect(() => {
7✔
63
    if (!socketInitialized || !isVisible) {
5!
64
      return;
5✔
65
    }
66
    setFitTrigger(toggle);
×
67
  }, [size, isVisible, socketInitialized]);
68

69
  useEffect(() => {
7✔
70
    if (!socketInitialized || !xtermRef.current.terminal || !textInput) {
5!
71
      return;
5✔
72
    }
73
    xtermRef.current.terminal.current.paste(textInput);
×
74
  }, [socketInitialized, textInput, xtermRef]);
75

76
  const onData = useCallback(data => sendMessage({ typ: MessageTypes.Shell, body: data }), [sendMessage]);
7✔
77

78
  const onResize = useCallback(
7✔
79
    newDimensions => {
80
      if (newDimensions.rows != dimensions.rows || newDimensions.cols != dimensions.cols) {
6✔
81
        sendMessage({
5✔
82
          typ: MessageTypes.Resize,
83
          props: { terminal_height: newDimensions.rows, terminal_width: newDimensions.cols }
84
        });
85
        setDimensions(newDimensions);
4✔
86
      }
87
    },
88
    [dimensions, sendMessage]
89
  );
90

91
  return <XTerm xtermRef={xtermRef} options={options} onData={onData} onResize={onResize} triggerResize={fitTrigger} {...xtermProps} />;
7✔
92
};
93

94
export default Terminal;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc