Skip to content

Commit 471fc53

Browse files
committed
refactor: ResizeObserver in Terminal.tsx
1 parent 39f898d commit 471fc53

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
@@ -1104,32 +1104,6 @@ export const reloadToastBody = () => {
11041104
)
11051105
}
11061106

1107-
export function useHeightObserver(callback): [RefObject<HTMLDivElement>] {
1108-
const ref = useRef(null)
1109-
const callbackRef = useRef(callback)
1110-
1111-
useLayoutEffect(() => {
1112-
callbackRef.current = callback
1113-
}, [callback])
1114-
1115-
const handleHeightChange = useCallback(() => {
1116-
callbackRef.current?.(ref.current.clientHeight)
1117-
}, [callbackRef])
1118-
1119-
useLayoutEffect(() => {
1120-
if (!ref.current) {
1121-
return
1122-
}
1123-
const observer = new ResizeObserver(handleHeightChange)
1124-
observer.observe(ref.current)
1125-
return () => {
1126-
observer.disconnect()
1127-
}
1128-
}, [handleHeightChange, ref])
1129-
1130-
return [ref]
1131-
}
1132-
11331107
export const getDeploymentAppType = (
11341108
allowedDeploymentTypes: DeploymentAppTypes[],
11351109
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)