Skip to content

Commit

Permalink
refactor: ResizeObserver in Terminal.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Elessar1802 committed Feb 27, 2024
1 parent 503ccbc commit bc4e2c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
26 changes: 0 additions & 26 deletions src/components/common/helpers/Helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1112,32 +1112,6 @@ export const reloadToastBody = () => {
)
}

export function useHeightObserver(callback): [RefObject<HTMLDivElement>] {
const ref = useRef(null)
const callbackRef = useRef(callback)

useLayoutEffect(() => {
callbackRef.current = callback
}, [callback])

const handleHeightChange = useCallback(() => {
callbackRef.current?.(ref.current.clientHeight)
}, [callbackRef])

useLayoutEffect(() => {
if (!ref.current) {
return
}
const observer = new ResizeObserver(handleHeightChange)
observer.observe(ref.current)
return () => {
observer.disconnect()
}
}, [handleHeightChange, ref])

return [ref]
}

export const getDeploymentAppType = (
allowedDeploymentTypes: DeploymentAppTypes[],
selectedDeploymentAppType: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as XtermWebfont from 'xterm-webfont'
import SockJS from 'sockjs-client'
import moment from 'moment'
import CopyToast, { handleSelectionChange } from '../CopyToast'
import { elementDidMount, useHeightObserver } from '../../../../../../common/helpers/Helpers'
import { elementDidMount } from '../../../../../../common/helpers/Helpers'
import { CLUSTER_STATUS, SocketConnectionType } from '../../../../../../ClusterNodes/constants'
import { TERMINAL_STATUS } from './constants'
import './terminal.scss'
Expand All @@ -27,6 +27,7 @@ export default function TerminalView({
dataTestId,
}: TerminalViewType) {
const socket = useRef(null)
const termDiv = useRef(null)
const [firstMessageReceived, setFirstMessageReceived] = useState(false)
const [isReconnection, setIsReconnection] = useState(false)
const [popupText, setPopupText] = useState<boolean>(false)
Expand All @@ -42,7 +43,13 @@ export default function TerminalView({
}
}

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

useEffect(() => {
if (!terminalRef.current) {
Expand Down Expand Up @@ -240,7 +247,7 @@ export default function TerminalView({
<div className="terminal-wrapper" data-testid={dataTestId}>
{renderConnectionStrip()}
<div
ref={myDivRef}
ref={termDiv}
id="terminal-id"
data-testid="terminal-editor-container"
className="mt-8 mb-4 terminal-component ml-20"
Expand Down

0 comments on commit bc4e2c4

Please sign in to comment.