mirror of
https://github.com/apache/superset.git
synced 2026-04-22 17:45:21 +00:00
refactor: useEffectEvent for efficient deps (#23871)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user