-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,795 additions
and
4,357 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
// components | ||
import Playground from "@/components/Playground/Playground"; | ||
import Footer from "@/components/Footer/Footer"; // comment this to hide footer | ||
|
||
export default function Home() { | ||
return ( | ||
<main> | ||
<Playground /> | ||
<Footer /> | ||
</main> | ||
<main> | ||
<Playground /> | ||
<Footer /> | ||
</main> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// react | ||
import { useState, useEffect, useRef, KeyboardEvent, ChangeEvent } from "react"; | ||
|
||
// utils | ||
import { handleCommand } from "@/shared/utils/cliUtils"; | ||
import blacklistedCommands from "@/shared/utils/blacklist"; // Assuming you added blacklist here | ||
|
||
export const useCli = (decreaseCommandsLeft: () => void) => { | ||
// states | ||
const [command, setCommand] = useState(""); | ||
const [output, setOutput] = useState<string[]>([]); | ||
const [tempCommand, setTempCommand] = useState(""); | ||
//Initialise the command history with sessionStorage | ||
const [commandHistory, setCommandHistory] = useState<string[]>([]); | ||
const [historyIndex, setHistoryIndex] = useState<number>(-1); | ||
|
||
// useRefs | ||
const terminalRef = useRef<HTMLDivElement>(null); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const handleCommandWrapper = (e: KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === "Enter") { | ||
const commandName = command.trim().split(" ")[0].toUpperCase(); // Extract the command | ||
|
||
if (blacklistedCommands.includes(commandName)) { | ||
setOutput((prevOutput) => [ | ||
...prevOutput, | ||
`(error) ERR unknown command '${commandName}'`, | ||
]); | ||
} else { | ||
handleCommand({ command, setOutput }); // Execute if not blacklisted | ||
} | ||
|
||
setCommand(""); // Clear input | ||
decreaseCommandsLeft(); // Call to update remaining commands | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (terminalRef.current) { | ||
terminalRef.current.scrollTop = terminalRef.current.scrollHeight; | ||
} | ||
}, [output]); | ||
|
||
//Load initial command history if present | ||
useEffect(() => { | ||
const savedHistory = sessionStorage.getItem("terminalHistory"); | ||
const commandHistory = savedHistory ? JSON.parse(savedHistory) : []; | ||
setCommandHistory(commandHistory); | ||
}, []); | ||
|
||
// Save to session storage whenever commandHistory changes | ||
useEffect(() => { | ||
sessionStorage.setItem("terminalHistory", JSON.stringify(commandHistory)); | ||
}, [commandHistory]); | ||
|
||
useEffect(() => { | ||
if (inputRef.current) { | ||
inputRef.current.focus(); | ||
} | ||
}, []); | ||
|
||
const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
setCommand(e.target.value); | ||
}; | ||
|
||
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === "Enter") { | ||
handleCommandWrapper(e); | ||
if (command.trim().length !== 0) { | ||
setCommandHistory((prev) => [...prev, command]); | ||
setHistoryIndex(-1); | ||
} | ||
} | ||
|
||
if (e.key === "ArrowUp") { | ||
e.preventDefault(); | ||
if (historyIndex < commandHistory.length - 1) { | ||
if (historyIndex === -1) { | ||
// Save current input when starting to navigate history | ||
setTempCommand(command); | ||
} | ||
const newIndex = historyIndex + 1; | ||
setHistoryIndex(newIndex); | ||
setCommand(commandHistory[commandHistory.length - 1 - newIndex]); | ||
} | ||
} else if (e.key === "ArrowDown") { | ||
e.preventDefault(); | ||
if (historyIndex > -1) { | ||
const newIndex = historyIndex - 1; | ||
setHistoryIndex(newIndex); | ||
if (newIndex === -1) { | ||
// Restore the saved input when reaching the bottom | ||
setCommand(tempCommand); | ||
} else { | ||
setCommand(commandHistory[commandHistory.length - 1 - newIndex]); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
handleInputChange, | ||
handleKeyDown, | ||
terminalRef, | ||
inputRef, | ||
output, | ||
setOutput, | ||
command, | ||
tempCommand, | ||
setTempCommand, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
'use client'; | ||
|
||
|
||
"use client"; | ||
export default function Header() { | ||
return ( | ||
<header className="flex items-center mb-4"> | ||
<h1 className="text-2xl font-bold">DiceDB PlayGround</h1> | ||
</header> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
export const usePlayground = () => { | ||
const [search, setSearch] = useState(""); | ||
const [timeLeft, setTimeLeft] = useState<number>(15 * 60); | ||
const [commandsLeft, setCommandsLeft] = useState<number>(1000); | ||
|
||
useEffect(() => { | ||
const timer = setInterval(() => { | ||
setTimeLeft((prev) => (prev > 0 ? prev - 1 : 0)); | ||
}, 1000); | ||
|
||
return () => clearInterval(timer); | ||
}, []); | ||
|
||
const decreaseCommandsLeft = () => { | ||
setCommandsLeft((prev) => (prev > 0 ? prev - 1 : 0)); | ||
}; | ||
|
||
return { | ||
decreaseCommandsLeft, | ||
search, | ||
timeLeft, | ||
commandsLeft, | ||
setSearch, | ||
setTimeLeft, | ||
setCommandsLeft, | ||
}; | ||
}; |
Oops, something went wrong.