@@ -14,6 +14,7 @@ import {
1414 fontFamilies ,
1515 useDarkMode ,
1616 cx ,
17+ rafraf ,
1718} from '@mongodb-js/compass-components' ;
1819import type {
1920 Runtime ,
@@ -202,10 +203,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
202203 const darkMode = useDarkMode ( ) ;
203204
204205 const editorRef = useRef < EditorRef | null > ( null ) ;
205- const outputRef = useRef ( output ) ;
206- const historyRef = useRef ( history ) ;
207206 const shellInputContainerRef = useRef < HTMLDivElement > ( null ) ;
208- const initialEvaluateRef = useRef ( initialEvaluate ) ;
209207
210208 useImperativeHandle (
211209 ref ,
@@ -254,7 +252,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
254252 return {
255253 onPrint : ( result : RuntimeEvaluationResult [ ] ) : void => {
256254 const newOutput = [
257- ...( outputRef . current ?? [ ] ) ,
255+ ...( output ?? [ ] ) ,
258256 ...result . map (
259257 ( entry ) : ShellOutputEntry => ( {
260258 format : 'output' ,
@@ -301,7 +299,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
301299 onOutputChanged ?.( [ ] ) ;
302300 } ,
303301 } ;
304- } , [ focusEditor , maxOutputLength , onOutputChanged ] ) ;
302+ } , [ focusEditor , maxOutputLength , onOutputChanged , output ] ) ;
305303
306304 const updateShellPrompt = useCallback ( async ( ) : Promise < void > => {
307305 let newShellPrompt = '>' ;
@@ -368,8 +366,8 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
368366
369367 const onInput = useCallback (
370368 async ( code : string ) => {
371- const newOutput = [ ...( outputRef . current ?? [ ] ) ] ;
372- const newHistory = [ ...( historyRef . current ?? [ ] ) ] ;
369+ const newOutput = [ ...( output ?? [ ] ) ] ;
370+ const newHistory = [ ...( history ?? [ ] ) ] ;
373371
374372 // don't evaluate empty input, but do add it to the output
375373 if ( ! code || code . trim ( ) === '' ) {
@@ -407,6 +405,8 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
407405 onHistoryChanged ?.( newHistory ) ;
408406 } ,
409407 [
408+ output ,
409+ history ,
410410 onOutputChanged ,
411411 evaluate ,
412412 redactInfo ,
@@ -446,19 +446,29 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
446446 return Promise . resolve ( false ) ;
447447 } , [ isOperationInProgress , runtime ] ) ;
448448
449+ const [ isFirstRun , setIsFirstRun ] = useState ( true ) ;
450+
449451 useEffect ( ( ) => {
452+ if ( ! isFirstRun ) {
453+ return ;
454+ }
455+
456+ setIsFirstRun ( false ) ;
457+
450458 void updateShellPrompt ( ) . then ( async ( ) => {
451- if ( initialEvaluateRef . current ) {
452- const evalLines = normalizeInitialEvaluate ( initialEvaluateRef . current ) ;
459+ if ( initialEvaluate ) {
460+ const evalLines = normalizeInitialEvaluate ( initialEvaluate ) ;
453461 for ( const input of evalLines ) {
454462 await onInput ( input ) ;
455463 }
456464 }
457465 } ) ;
458- } , [ onInput , updateShellPrompt ] ) ;
466+ } , [ initialEvaluate , isFirstRun , onInput , updateShellPrompt ] ) ;
459467
460468 useEffect ( ( ) => {
461- scrollToBottom ( ) ;
469+ rafraf ( ( ) => {
470+ scrollToBottom ( ) ;
471+ } ) ;
462472 } ) ;
463473
464474 /* eslint-disable jsx-a11y/no-static-element-interactions */
0 commit comments