Skip to content

Commit bc4e2c4

Browse files
committed
refactor: ResizeObserver in Terminal.tsx
1 parent 503ccbc commit bc4e2c4

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

src/components/common/helpers/Helpers.tsx

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,32 +1112,6 @@ export const reloadToastBody = () => {
11121112
)
11131113
}
11141114

1115-
export function useHeightObserver(callback): [RefObject<HTMLDivElement>] {
1116-
const ref = useRef(null)
1117-
const callbackRef = useRef(callback)
1118-
1119-
useLayoutEffect(() => {
1120-
callbackRef.current = callback
1121-
}, [callback])
1122-
1123-
const handleHeightChange = useCallback(() => {
1124-
callbackRef.current?.(ref.current.clientHeight)
1125-
}, [callbackRef])
1126-
1127-
useLayoutEffect(() => {
1128-
if (!ref.current) {
1129-
return
1130-
}
1131-
const observer = new ResizeObserver(handleHeightChange)
1132-
observer.observe(ref.current)
1133-
return () => {
1134-
observer.disconnect()
1135-
}
1136-
}, [handleHeightChange, ref])
1137-
1138-
return [ref]
1139-
}
1140-
11411115
export const getDeploymentAppType = (
11421116
allowedDeploymentTypes: DeploymentAppTypes[],
11431117
selectedDeploymentAppType: string,

src/components/v2/appDetails/k8Resource/nodeDetail/NodeDetailTabs/terminal/Terminal.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as XtermWebfont from 'xterm-webfont'
55
import SockJS from 'sockjs-client'
66
import moment from 'moment'
77
import CopyToast, { handleSelectionChange } from '../CopyToast'
8-
import { elementDidMount, useHeightObserver } from '../../../../../../common/helpers/Helpers'
8+
import { elementDidMount } from '../../../../../../common/helpers/Helpers'
99
import { CLUSTER_STATUS, SocketConnectionType } from '../../../../../../ClusterNodes/constants'
1010
import { TERMINAL_STATUS } from './constants'
1111
import './terminal.scss'
@@ -27,6 +27,7 @@ export default function TerminalView({
2727
dataTestId,
2828
}: TerminalViewType) {
2929
const socket = useRef(null)
30+
const termDiv = useRef(null)
3031
const [firstMessageReceived, setFirstMessageReceived] = useState(false)
3132
const [isReconnection, setIsReconnection] = useState(false)
3233
const [popupText, setPopupText] = useState<boolean>(false)
@@ -42,7 +43,13 @@ export default function TerminalView({
4243
}
4344
}
4445

45-
const [myDivRef] = useHeightObserver(resizeSocket)
46+
useEffect(() => {
47+
/* requestAnimationFrame: will defer the resizeSocket callback to the next repaint;
48+
* sparing us from - ResizeObserver loop completed with undelivered notifications */
49+
const observer = new ResizeObserver(() => window.requestAnimationFrame(resizeSocket))
50+
observer.observe(termDiv.current)
51+
return () => observer.disconnect()
52+
}, [])
4653

4754
useEffect(() => {
4855
if (!terminalRef.current) {
@@ -240,7 +247,7 @@ export default function TerminalView({
240247
<div className="terminal-wrapper" data-testid={dataTestId}>
241248
{renderConnectionStrip()}
242249
<div
243-
ref={myDivRef}
250+
ref={termDiv}
244251
id="terminal-id"
245252
data-testid="terminal-editor-container"
246253
className="mt-8 mb-4 terminal-component ml-20"

0 commit comments

Comments
 (0)