diff options
Diffstat (limited to 'src/console/components')
-rw-r--r-- | src/console/components/AppContext.ts | 13 | ||||
-rw-r--r-- | src/console/components/CommandPrompt.tsx | 13 | ||||
-rw-r--r-- | src/console/components/Console.tsx | 36 | ||||
-rw-r--r-- | src/console/components/FindPrompt.tsx | 12 |
4 files changed, 34 insertions, 40 deletions
diff --git a/src/console/components/AppContext.ts b/src/console/components/AppContext.ts deleted file mode 100644 index a930e14..0000000 --- a/src/console/components/AppContext.ts +++ /dev/null @@ -1,13 +0,0 @@ -import React from "react"; -import { State, defaultState } from "../reducers/console"; -import { ConsoleAction } from "../actions/console"; - -const AppContext = React.createContext<{ - state: State; - dispatch: React.Dispatch<Promise<ConsoleAction> | ConsoleAction>; -}>({ - state: defaultState, - dispatch: () => null, -}); - -export default AppContext; diff --git a/src/console/components/CommandPrompt.tsx b/src/console/components/CommandPrompt.tsx index 24f46ae..1b6281b 100644 --- a/src/console/components/CommandPrompt.tsx +++ b/src/console/components/CommandPrompt.tsx @@ -1,12 +1,11 @@ import React from "react"; -import * as consoleActions from "../actions/console"; -import AppContext from "./AppContext"; import Completion from "./console/Completion"; import Input from "./console//Input"; import styled from "styled-components"; import { useCompletions, useSelectCompletion } from "../completion/hooks"; import useAutoResize from "../hooks/useAutoResize"; import { CompletionProvider } from "../completion/provider"; +import { useExecCommand, useHide } from "../app/hooks"; const COMPLETION_MAX_ITEMS = 33; @@ -19,7 +18,7 @@ interface Props { } const CommandPromptInner: React.FC<Props> = ({ initialInputValue }) => { - const { dispatch } = React.useContext(AppContext); + const hide = useHide(); const [inputValue, setInputValue] = React.useState(initialInputValue); const { completions, updateCompletions } = useCompletions(); const { @@ -28,11 +27,12 @@ const CommandPromptInner: React.FC<Props> = ({ initialInputValue }) => { selectNext, selectPrev, } = useSelectCompletion(); + const execCommand = useExecCommand(); useAutoResize(); const onBlur = () => { - dispatch(consoleActions.hideCommand()); + hide(); }; const isCancelKey = React.useCallback( @@ -63,10 +63,11 @@ const CommandPromptInner: React.FC<Props> = ({ initialInputValue }) => { const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { if (isCancelKey(e)) { - dispatch(consoleActions.hideCommand()); + hide(); } else if (isEnterKey(e)) { const value = (e.target as HTMLInputElement).value; - dispatch(consoleActions.enterCommand(value)); + execCommand(value); + hide(); } else if (isNextKey(e)) { selectNext(); } else if (isPrevKey(e)) { diff --git a/src/console/components/Console.tsx b/src/console/components/Console.tsx index c8642c8..db18fa0 100644 --- a/src/console/components/Console.tsx +++ b/src/console/components/Console.tsx @@ -3,31 +3,37 @@ import FindPrompt from "./FindPrompt"; import CommandPrompt from "./CommandPrompt"; import InfoMessage from "./InfoMessage"; import ErrorMessage from "./ErrorMessage"; -import AppContext from "./AppContext"; import { useColorSchemeRefresh } from "../colorscheme/hooks"; +import { + useCommandMode, + useErrorMessage, + useFindMode, + useInfoMessage, +} from "../app/hooks"; const Console: React.FC = () => { - const { state } = React.useContext(AppContext); const refreshColorScheme = useColorSchemeRefresh(); + const { visible: visibleCommand, initialInputValue } = useCommandMode(); + const { visible: visibleFind } = useFindMode(); + const { visible: visibleInfo, message: infoMessage } = useInfoMessage(); + const { visible: visibleError, message: errorMessage } = useErrorMessage(); React.useEffect(() => { - if (state.mode !== "") { + if (visibleCommand || visibleFind || visibleInfo || visibleError) { refreshColorScheme(); } - }, [state.mode]); + }, [visibleCommand, visibleFind, visibleInfo, visibleError]); - switch (state.mode) { - case "command": - return <CommandPrompt initialInputValue={state.consoleText} />; - case "find": - return <FindPrompt />; - case "info": - return <InfoMessage>{state.messageText}</InfoMessage>; - case "error": - return <ErrorMessage>{state.messageText}</ErrorMessage>; - default: - return null; + if (visibleCommand) { + return <CommandPrompt initialInputValue={initialInputValue} />; + } else if (visibleFind) { + return <FindPrompt />; + } else if (visibleInfo) { + return <InfoMessage>{infoMessage}</InfoMessage>; + } else if (visibleError) { + return <ErrorMessage>{errorMessage}</ErrorMessage>; } + return null; }; export default Console; diff --git a/src/console/components/FindPrompt.tsx b/src/console/components/FindPrompt.tsx index 10fa6c3..c437d16 100644 --- a/src/console/components/FindPrompt.tsx +++ b/src/console/components/FindPrompt.tsx @@ -1,20 +1,20 @@ import React from "react"; -import * as consoleActions from "../../console/actions/console"; -import AppContext from "./AppContext"; import Input from "./console/Input"; import styled from "styled-components"; import useAutoResize from "../hooks/useAutoResize"; +import { useExecFind, useHide } from "../app/hooks"; const ConsoleWrapper = styled.div` border-top: 1px solid gray; `; const FindPrompt: React.FC = () => { - const { dispatch } = React.useContext(AppContext); const [inputValue, setInputValue] = React.useState(""); + const hide = useHide(); + const execFind = useExecFind(); const onBlur = () => { - dispatch(consoleActions.hideCommand()); + hide(); }; useAutoResize(); @@ -24,13 +24,13 @@ const FindPrompt: React.FC = () => { e.preventDefault(); const value = (e.target as HTMLInputElement).value; - dispatch(consoleActions.enterFind(value === "" ? undefined : value)); + execFind(value === "" ? undefined : value); }; const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { switch (e.key) { case "Escape": - dispatch(consoleActions.hideCommand()); + hide(); break; case "Enter": doEnter(e); |