Skip to content

Commit

Permalink
Code cleanup (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun-29 authored Oct 2, 2024
1 parent 34b88f5 commit 4da16ab
Show file tree
Hide file tree
Showing 21 changed files with 1,795 additions and 4,357 deletions.
5,577 changes: 1,486 additions & 4,091 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"lucide-react": "^0.446.0",
"next": "14.2.13",
"package.json": "^2.0.1",
"prettier": "^3.3.3",
"react": "^18",
"react-dom": "^18",
"react-terminal-ui": "^1.3.0",
Expand Down
7 changes: 5 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// types
import { ReactNode } from "react";
import type { Metadata } from "next";

// styles
import "@/styles/globals.css";
import React from "react";

export const metadata: Metadata = {
title: "DiceDB Playground",
Expand All @@ -14,7 +17,7 @@ export const metadata: Metadata = {
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
children: ReactNode;
}>) {
return (
<html lang="en">
Expand Down
11 changes: 6 additions & 5 deletions src/app/page.tsx
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>
);
}
}
107 changes: 10 additions & 97 deletions src/components/CLI/CLI.tsx
Original file line number Diff line number Diff line change
@@ -1,109 +1,22 @@
// src/components/CLI/CLI.tsx

"use client";

import React, { useEffect, useRef, useState } from 'react';
import { handleCommand } from '@/shared/utils/cliUtils';
import blacklistedCommands from '@/shared/utils/blacklist'; // Assuming you added blacklist here
// hooks
import { useCli } from "./hooks/useCli";

interface CliProps {
decreaseCommandsLeft: () => void;
}

export default function Cli({ decreaseCommandsLeft }: CliProps) {
const [command, setCommand] = useState("");
const [tempCommand, setTempCommand] = useState("");
const [output, setOutput] = useState<string[]>([]);
const terminalRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);

//Initialise the command history with sessionStorage
const [commandHistory, setCommandHistory] = useState<string[]>([]);
const [historyIndex, setHistoryIndex] = useState<number>(-1);

const handleCommandWrapper = (e: React.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: React.ChangeEvent<HTMLInputElement>) => {
setCommand(e.target.value);
};

const handleKeyDown = (e: React.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]);
}
}
}
};

const {
handleInputChange,
handleKeyDown,
terminalRef,
inputRef,
output,
command,
} = useCli(decreaseCommandsLeft);
return (
<div
ref={terminalRef}
Expand Down
113 changes: 113 additions & 0 deletions src/components/CLI/hooks/useCli.tsx
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,
};
};
2 changes: 1 addition & 1 deletion src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
// icons
import { Button } from "@mui/material";
import { Twitter } from "@mui/icons-material";
import GitHub from "@mui/icons-material/GitHub";
Expand Down
6 changes: 2 additions & 4 deletions src/components/Header/Header.tsx
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>
);
}
}
30 changes: 10 additions & 20 deletions src/components/Playground/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
"use client";

import Image from "next/image";
import React, { useState, useEffect } from "react";

import { Dice1, Dice3, Dice5 } from "lucide-react";

// Components
import Cli from "@/components/CLI/CLI";
import SearchBox from "@/components/Search/SearchBox";
import { formatTime } from "@/shared/utils/commonUtils";

export default function Playground() {
const [search, setSearch] = useState("");
const [timeLeft, setTimeLeft] = useState<number>(15 * 60);
const [commandsLeft, setCommandsLeft] = useState<number>(1000);
import { Dice1, Dice3, Dice5 } from "lucide-react";

useEffect(() => {
const timer = setInterval(() => {
setTimeLeft((prev) => (prev > 0 ? prev - 1 : 0));
}, 1000);
// utils
import { formatTime } from "@/shared/utils/commonUtils";

return () => clearInterval(timer);
}, []);
// images and icons
import Image from "next/image";
import { usePlayground } from "./hooks/usePlayground";

const decreaseCommandsLeft = () => {
setCommandsLeft((prev) => (prev > 0 ? prev - 1 : 0));
};
export default function Playground() {
const { decreaseCommandsLeft, search, timeLeft, commandsLeft, setSearch } =
usePlayground();

return (
<div className="container mx-auto flex flex-col flex-grow min-h-screen bg-white text-gray-900">
Expand Down
29 changes: 29 additions & 0 deletions src/components/Playground/hooks/usePlayground.ts
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,
};
};
Loading

0 comments on commit 4da16ab

Please sign in to comment.