1- import React , { useState , useRef , useEffect } from "react" ;
1+ import React , { useRef , useEffect , useCallback } from "react" ;
22import { CommandHistory } from "../CommandHistory" ;
33import "./input.css" ;
4+ import { useInputStore } from "../hooks/useInputStore" ;
5+ import { InputActionType , setInputText , clearInputText } from "../InputStore" ;
46
57type SendFunction = ( text : string ) => void ;
68
@@ -13,8 +15,8 @@ const STORAGE_KEY = 'command_history';
1315const MAX_HISTORY = 1000 ;
1416
1517const CommandInput = ( { onSend, inputRef } : Props ) => {
16- const [ input , setInput ] = useState ( "" ) ;
1718 const commandHistoryRef = useRef ( new CommandHistory ( ) ) ;
19+ const [ inputState , dispatch ] = useInputStore ( ) ;
1820
1921 // Load saved history on component mount
2022 useEffect ( ( ) => {
@@ -41,38 +43,40 @@ const CommandInput = ({ onSend, inputRef }: Props) => {
4143 }
4244 } ;
4345
44- const handleKeyDown = ( e : React . KeyboardEvent < HTMLTextAreaElement > ) => {
46+ const handleKeyDown = useCallback ( ( e : React . KeyboardEvent < HTMLTextAreaElement > ) => {
4547 const commandHistory = commandHistoryRef . current ;
48+ const currentInput = inputState . text ;
4649
4750 if ( e . key === "Enter" && ! e . shiftKey ) {
4851 e . preventDefault ( ) ;
4952 handleSend ( ) ;
5053 } else if ( e . key === "ArrowUp" ) {
5154 e . preventDefault ( ) ;
52- const prevCommand = commandHistory . navigateUp ( input ) ;
53- setInput ( prevCommand ) ;
55+ const prevCommand = commandHistory . navigateUp ( currentInput ) ;
56+ setInputText ( prevCommand ) ;
5457 } else if ( e . key === "ArrowDown" ) {
5558 e . preventDefault ( ) ;
56- const nextCommand = commandHistory . navigateDown ( input ) ;
57- setInput ( nextCommand ) ;
59+ const nextCommand = commandHistory . navigateDown ( currentInput ) ;
60+ setInputText ( nextCommand ) ;
5861 }
59- } ;
62+ } , [ inputState . text ] ) ;
6063
61- const handleSend = ( ) => {
62- if ( input . trim ( ) ) {
63- onSend ( input ) ;
64- commandHistoryRef . current . addCommand ( input ) ;
64+ const handleSend = useCallback ( ( ) => {
65+ const currentInput = inputState . text ;
66+ if ( currentInput . trim ( ) ) {
67+ onSend ( currentInput ) ;
68+ commandHistoryRef . current . addCommand ( currentInput ) ;
6569 saveHistory ( ) ;
66- setInput ( "" ) ;
70+ clearInputText ( ) ;
6771 inputRef . current ?. focus ( ) ;
6872 }
69- } ;
73+ } , [ inputState . text , onSend , inputRef ] ) ;
7074
7175 return (
7276 < div className = "command-input-container" >
7377 < textarea
74- value = { input }
75- onChange = { ( e ) => setInput ( e . target . value ) }
78+ value = { inputState . text }
79+ onChange = { ( e ) => setInputText ( e . target . value ) }
7680 onKeyDown = { handleKeyDown }
7781 id = "command-input"
7882 ref = { inputRef }
0 commit comments