From c9d7a74ebc131e369af25450fe28c5807d917e71 Mon Sep 17 00:00:00 2001 From: Ronnie Date: Wed, 20 Nov 2024 22:07:06 -0500 Subject: [PATCH] Added proper exit --- src/app/commands/exit.tsx | 11 ++++ src/components/Terminal.tsx | 126 +++++++++++++----------------------- 2 files changed, 57 insertions(+), 80 deletions(-) create mode 100644 src/app/commands/exit.tsx diff --git a/src/app/commands/exit.tsx b/src/app/commands/exit.tsx new file mode 100644 index 0000000..fa28150 --- /dev/null +++ b/src/app/commands/exit.tsx @@ -0,0 +1,11 @@ +const exitCommand = (setInputDisabled: (value: boolean) => void, setOutput: (value: JSX.Element[]) => void) => { + setInputDisabled(true); // Disable further input + setOutput([ +
+ Terminal exited. Refresh the page to restart. 🚀 +
, + ]); + return []; +}; + +export default exitCommand; diff --git a/src/components/Terminal.tsx b/src/components/Terminal.tsx index 9582cb8..ea55059 100644 --- a/src/components/Terminal.tsx +++ b/src/components/Terminal.tsx @@ -12,35 +12,33 @@ const Terminal: React.FC = () => { const [output, setOutput] = useState([]); const [history, setHistory] = useState([]); const [historyIndex, setHistoryIndex] = useState(null); - const [suggestion, setSuggestion] = useState(null); // For live suggestions + const [suggestion, setSuggestion] = useState(null); + const [inputDisabled, setInputDisabled] = useState(false); // Disable input after exit const terminalEndRef = useRef(null); const inputRef = useRef(null); const commandsList = ["help", "socials", "exit", "selfhosted"]; - useEffect(() => { - window.scrollTo(0, 0); // Automatically scroll to the top - }, []); - const handleInput = (e: React.FormEvent) => { e.preventDefault(); - // If the input is empty, add a blank line + if (inputDisabled) return; // Prevent handling input if disabled + if (input.trim() === "") { setOutput((prev) => [ ...prev, -
 
, // Blank line +
 
, ]); - setInput(""); // Clear the input field + setInput(""); return; } const normalizedInput = input.toLowerCase(); - const commands: { [key: string]: () => JSX.Element[] } = { + const commands: { [key: string]: (setInputDisabled: any, setOutput: any) => JSX.Element[] } = { help: helpCommand, socials: socialsCommand, - exit: exitCommand, + exit: () => exitCommand(setInputDisabled, setOutput), // Pass control functions selfhosted: selfhostedCommand, }; @@ -55,53 +53,12 @@ const Terminal: React.FC = () => { setHistory((prev) => [...prev, input]); setHistoryIndex(null); setInput(""); - setSuggestion(null); // Clear suggestion after submission - }; - - const handleKeyDown = (e: React.KeyboardEvent) => { - if (e.key === "Tab") { - e.preventDefault(); // Prevent browser default behavior - if (suggestion) { - setInput(suggestion); - setSuggestion(null); // Clear suggestion after Tab - } - } - - if (history.length === 0) return; - - if (e.key === "ArrowUp") { - e.preventDefault(); - if (historyIndex === null) { - setHistoryIndex(history.length - 1); - setInput(history[history.length - 1]); - } else if (historyIndex > 0) { - setHistoryIndex((prev) => { - const newIndex = prev! - 1; - setInput(history[newIndex]); - return newIndex; - }); - } - } - - if (e.key === "ArrowDown") { - e.preventDefault(); - if (historyIndex !== null && historyIndex < history.length - 1) { - setHistoryIndex((prev) => { - const newIndex = prev! + 1; - setInput(history[newIndex]); - return newIndex; - }); - } else { - setHistoryIndex(null); - setInput(""); - } - } + setSuggestion(null); }; const handleInputChange = (value: string) => { setInput(value); - // Suggest commands dynamically const normalizedInput = value.toLowerCase(); const matchedCommand = commandsList.find((command) => command.startsWith(normalizedInput) @@ -125,40 +82,49 @@ const Terminal: React.FC = () => { ))} -
- $ -
- handleInputChange(e.target.value)} - onKeyDown={handleKeyDown} - autoFocus - /> - {suggestion && input.trim() !== suggestion && ( -
- {input} - - {suggestion.slice(input.length)} - -
- )} -
+ + {!inputDisabled && ( // Hide the prompt if input is disabled + $ + )} + {!inputDisabled && ( +
+ handleInputChange(e.target.value)} + autoFocus + disabled={inputDisabled} // Disable input when exited + /> + {suggestion && input.trim() !== suggestion && ( +
+ {input} + + {suggestion.slice(input.length)} + +
+ )} +
+ )}
+
); }; + const MOTD = () => { const socialsOutput = socialsCommand(false); const selfHostedOutput = selfhostedCommand(false);