refactor: useEffectEvent for efficient deps (#23871)

This commit is contained in:
JUST.in DO IT
2023-05-03 11:52:04 -07:00
committed by GitHub
parent e639cebdd1
commit 842659dbfe
4 changed files with 78 additions and 29 deletions

View File

@@ -25,6 +25,7 @@ import React, {
useRef,
useCallback,
} from 'react';
import useEffectEvent from 'src/hooks/useEffectEvent';
import { CSSTransition } from 'react-transition-group';
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
import PropTypes from 'prop-types';
@@ -354,39 +355,24 @@ const SqlEditor = ({
return base;
}, [dispatch, queryEditor.sql, startQuery, stopQuery]);
const handleWindowResize = useCallback(() => {
setHeight(getSqlEditorHeight());
}, []);
const handleWindowResizeWithThrottle = useMemo(
() => throttle(handleWindowResize, WINDOW_RESIZE_THROTTLE_MS),
[handleWindowResize],
);
const onBeforeUnload = useCallback(
event => {
if (
database?.extra_json?.cancel_query_on_windows_unload &&
latestQuery?.state === 'running'
) {
event.preventDefault();
stopQuery();
}
},
[
database?.extra_json?.cancel_query_on_windows_unload,
latestQuery?.state,
stopQuery,
],
);
const onBeforeUnload = useEffectEvent(event => {
if (
database?.extra_json?.cancel_query_on_windows_unload &&
latestQuery?.state === 'running'
) {
event.preventDefault();
stopQuery();
}
});
useEffect(() => {
// We need to measure the height of the sql editor post render to figure the height of
// the south pane so it gets rendered properly
setHeight(getSqlEditorHeight());
if (!database || isEmpty(database)) {
setShowEmptyState(true);
}
const handleWindowResizeWithThrottle = throttle(
() => setHeight(getSqlEditorHeight()),
WINDOW_RESIZE_THROTTLE_MS,
);
window.addEventListener('resize', handleWindowResizeWithThrottle);
window.addEventListener('beforeunload', onBeforeUnload);
@@ -395,7 +381,14 @@ const SqlEditor = ({
window.removeEventListener('resize', handleWindowResizeWithThrottle);
window.removeEventListener('beforeunload', onBeforeUnload);
};
}, [database, handleWindowResizeWithThrottle, onBeforeUnload]);
// TODO: Remove useEffectEvent deps once https://github.com/facebook/react/pull/25881 is released
}, [onBeforeUnload]);
useEffect(() => {
if (!database || isEmpty(database)) {
setShowEmptyState(true);
}
}, [database]);
useEffect(() => {
// setup hotkeys