From 15b54e922a654bb1e6434eddae0f73d3ce7f9e43 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Thu, 12 Dec 2024 15:57:49 +0000 Subject: [PATCH] fix initial evaluate --- .../compass-shell/tab-compass-shell.tsx | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/compass-shell/src/components/compass-shell/tab-compass-shell.tsx b/packages/compass-shell/src/components/compass-shell/tab-compass-shell.tsx index d7e7d45c74f..fc8d330988f 100644 --- a/packages/compass-shell/src/components/compass-shell/tab-compass-shell.tsx +++ b/packages/compass-shell/src/components/compass-shell/tab-compass-shell.tsx @@ -57,14 +57,20 @@ type CompassShellProps = { initialInput?: string; }; -function useInitialEval(initialEvaluate?: string | string[]) { +function useInitialEval( + initialEvaluate: string | string[] | undefined, + isRender: boolean +) { const [initialEvalApplied, setInitialEvalApplied] = useTabState( 'initialEvalApplied', false ); useEffect(() => { - setInitialEvalApplied(true); - }, [setInitialEvalApplied]); + // as soon as we render the first time, set it to true + if (isRender && !initialEvalApplied) { + setInitialEvalApplied(true); + } + }, [initialEvalApplied, setInitialEvalApplied, isRender]); return initialEvalApplied ? undefined : initialEvaluate; } @@ -75,14 +81,19 @@ export const CompassShell: React.FC = ({ initialEvaluate: _initialEvaluate, initialInput, }) => { + const enableShell = usePreference('enableShell'); + const canRenderShell = !!(enableShell && initialHistory && runtime); + + // initialEvaluate will only be set on the first render + const initialEvaluate = useInitialEval(_initialEvaluate, canRenderShell); + const editorRef = useRef(null); - const initialEvaluate = useInitialEval(_initialEvaluate); + const [isOperationInProgress, setIsOperationInProgress] = useTabState( 'isOperationInProgress', false ); - const enableShell = usePreference('enableShell'); const [infoModalVisible, setInfoModalVisible] = useState(false); const [shellOutput, setShellOutput] = useTabState( 'shellOutput', @@ -121,13 +132,13 @@ export const CompassShell: React.FC = ({ setIsOperationInProgress(false); }, [setIsOperationInProgress]); - const canRenderShell = enableShell && initialHistory && runtime; - useEffect(() => { - return rafraf(() => { - editorRef.current?.focus(); - }); - }, []); + if (canRenderShell) { + return rafraf(() => { + editorRef.current?.focus(); + }); + } + }, [canRenderShell]); if (!enableShell) { return (