|
1 | | -import React from 'react'; |
2 | | -import { observer } from 'mobx-react'; |
| 1 | +import React, { useRef, useEffect, useCallback } from 'react'; |
| 2 | +import { observer } from 'mobx-react-lite'; |
3 | 3 | import PropTypes from 'prop-types'; |
4 | 4 | import Box from '@mui/material/Box'; |
5 | 5 |
|
6 | | -@observer |
7 | | -class Console extends React.Component { |
8 | | - keepToBottom = true; |
| 6 | +const Console = ({ run }) => { |
| 7 | + const stdoutContainerRef = useRef(null); |
9 | 8 |
|
10 | | - onMountStdoutContainer = el => { |
11 | | - this.stdoutContainer = el; |
12 | | - }; |
13 | | - |
14 | | - componentDidUpdate() { |
15 | | - if (this.keepToBottom) { |
16 | | - this.scrollConsoleToBottom(); |
17 | | - } |
18 | | - } |
19 | | - |
20 | | - isAtBottom = () => { |
21 | | - const { scrollTop, scrollHeight, clientHeight } = this.stdoutContainer; |
| 9 | + const scrollConsoleToBottom = useCallback(() => { |
| 10 | + if (!stdoutContainerRef.current) return; |
| 11 | + const { scrollHeight, clientHeight } = stdoutContainerRef.current; |
22 | 12 | const diff = scrollHeight - clientHeight; |
23 | | - const scrollIsAtBottom = scrollTop === diff; |
24 | | - return scrollIsAtBottom; |
25 | | - }; |
| 13 | + stdoutContainerRef.current.scrollTop = diff; |
| 14 | + }, []); |
26 | 15 |
|
27 | | - scrollConsoleToBottom = () => { |
28 | | - const { scrollHeight, clientHeight } = this.stdoutContainer; |
29 | | - const diff = scrollHeight - clientHeight; |
30 | | - this.stdoutContainer.scrollTop = diff; |
31 | | - }; |
| 16 | + // Every time the component updates, scroll to the bottom |
| 17 | + useEffect(() => { |
| 18 | + scrollConsoleToBottom(); |
| 19 | + }); |
32 | 20 |
|
33 | | - render() { |
34 | | - const { run } = this.props; |
35 | | - return ( |
36 | | - <Box |
37 | | - ref={this.onMountStdoutContainer} |
38 | | - sx={{ |
39 | | - color: (theme) => theme.palette.console.contrastText, |
40 | | - background: (theme) => theme.palette.console.background, |
41 | | - padding: '10px', |
42 | | - width: '100%', |
43 | | - height: '100%', |
44 | | - overflowY: 'auto', |
45 | | - position: 'relative', |
46 | | - }} |
47 | | - > |
48 | | - <div> |
49 | | - {run.stdout && ( |
50 | | - <Box |
51 | | - component="code" |
52 | | - sx={{ |
53 | | - color: (theme) => theme.palette.console.contrastText, |
54 | | - fontFamily: 'Consolas, "Liberation Mono", Menlo, Courier, monospace', |
55 | | - fontSize: '12px', |
56 | | - height: '100%', |
57 | | - position: 'absolute', |
58 | | - width: '100%', |
59 | | - whiteSpace: 'pre-wrap', |
60 | | - wordBreak: 'break-all', |
61 | | - }} |
62 | | - > |
63 | | - {run.stdout} |
64 | | - </Box> |
65 | | - )} |
66 | | - {run.stderr && ( |
67 | | - <Box |
68 | | - component="code" |
69 | | - sx={{ |
70 | | - color: (theme) => theme.palette.console.contrastText, |
71 | | - fontFamily: 'Consolas, "Liberation Mono", Menlo, Courier, monospace', |
72 | | - fontSize: '12px', |
73 | | - height: '100%', |
74 | | - position: 'absolute', |
75 | | - width: '100%', |
76 | | - whiteSpace: 'pre-wrap', |
77 | | - wordBreak: 'break-all', |
78 | | - }} |
79 | | - > |
80 | | - {run.stderr} |
81 | | - </Box> |
82 | | - )} |
83 | | - </div> |
84 | | - </Box> |
85 | | - ); |
86 | | - } |
87 | | -} |
| 21 | + return ( |
| 22 | + <Box |
| 23 | + ref={stdoutContainerRef} |
| 24 | + sx={{ |
| 25 | + color: (theme) => theme.palette.console.contrastText, |
| 26 | + background: (theme) => theme.palette.console.background, |
| 27 | + padding: '10px', |
| 28 | + width: '100%', |
| 29 | + height: '100%', |
| 30 | + overflowY: 'auto', |
| 31 | + position: 'relative', |
| 32 | + }} |
| 33 | + > |
| 34 | + <div> |
| 35 | + {run.stdout && ( |
| 36 | + <Box |
| 37 | + component="code" |
| 38 | + sx={{ |
| 39 | + color: (theme) => theme.palette.console.contrastText, |
| 40 | + fontFamily: 'Consolas, "Liberation Mono", Menlo, Courier, monospace', |
| 41 | + fontSize: '12px', |
| 42 | + height: '100%', |
| 43 | + position: 'absolute', |
| 44 | + width: '100%', |
| 45 | + whiteSpace: 'pre-wrap', |
| 46 | + wordBreak: 'break-all', |
| 47 | + }} |
| 48 | + > |
| 49 | + {run.stdout} |
| 50 | + </Box> |
| 51 | + )} |
| 52 | + {run.stderr && ( |
| 53 | + <Box |
| 54 | + component="code" |
| 55 | + sx={{ |
| 56 | + color: (theme) => theme.palette.console.contrastText, |
| 57 | + fontFamily: 'Consolas, "Liberation Mono", Menlo, Courier, monospace', |
| 58 | + fontSize: '12px', |
| 59 | + height: '100%', |
| 60 | + position: 'absolute', |
| 61 | + width: '100%', |
| 62 | + whiteSpace: 'pre-wrap', |
| 63 | + wordBreak: 'break-all', |
| 64 | + }} |
| 65 | + > |
| 66 | + {run.stderr} |
| 67 | + </Box> |
| 68 | + )} |
| 69 | + </div> |
| 70 | + </Box> |
| 71 | + ); |
| 72 | +}; |
88 | 73 |
|
89 | 74 | Console.propTypes = { |
90 | 75 | run: PropTypes.object.isRequired |
91 | 76 | }; |
92 | 77 |
|
93 | | -export default Console; |
| 78 | +export default observer(Console); |
0 commit comments