@@ -57,6 +57,8 @@ const Terminal = ({
5757 const [ history , setHistory ] = useState < string [ ] > ( [ ] ) ;
5858
5959 const [ currentLineInput , setCurrentLineInput ] = useState ( "" ) ;
60+ const [ tmpInputValue , setTmpInputValue ] = useState ( "" ) ;
61+
6062 const [ cursorPos , setCursorPos ] = useState ( 0 ) ;
6163
6264 const scrollIntoViewRef = useRef < HTMLDivElement > ( null ) ;
@@ -91,9 +93,17 @@ const Terminal = ({
9193
9294 // If we're not currently looking at history (oldIndex === -1) and user presses ArrowUp, jump to the last entry.
9395 if ( oldIndex === - 1 && direction === - 1 ) {
96+ setTmpInputValue ( currentLineInput ) ;
9497 return history . length - 1 ;
9598 }
9699
100+ // If we're at the most recent history entry and user presses ArrowDown, go back to the temporary input value.
101+ if ( oldIndex === history . length - 1 && direction === 1 ) {
102+ setCurrentLineInput ( tmpInputValue ) ;
103+ setTmpInputValue ( "" ) ;
104+ return - 1 ;
105+ }
106+
97107 // If oldIndex === -1 and direction === 1 (ArrowDown), keep -1 (nothing to go to).
98108 if ( oldIndex === - 1 && direction === 1 ) {
99109 return - 1 ;
@@ -119,15 +129,19 @@ const Terminal = ({
119129 setCursorPos ( 0 ) ;
120130
121131 // history update
122- setHistory ( ( previousHistory ) =>
123- currentLineInput . trim ( ) === "" ||
124- previousHistory [ previousHistory . length - 1 ] === currentLineInput . trim ( )
125- ? previousHistory
126- : [ ...previousHistory , currentLineInput ] ,
127- ) ;
128- setHistoryIndex ( - 1 ) ;
132+ if ( currentLineInput . trim ( ) !== "" ) {
133+ setHistory ( ( previousHistory ) =>
134+ previousHistory [ previousHistory . length - 1 ] ===
135+ currentLineInput . trim ( )
136+ ? previousHistory
137+ : [ ...previousHistory , currentLineInput ] ,
138+ ) ;
139+ }
129140
141+ setHistoryIndex ( - 1 ) ;
130142 setCurrentLineInput ( "" ) ;
143+ setTmpInputValue ( "" ) ;
144+
131145 setTimeout (
132146 ( ) =>
133147 scrollIntoViewRef ?. current ?. scrollIntoView ( {
0 commit comments