From 1bdc5e26eecec3c8a6ee96900389ee05422b8bfa Mon Sep 17 00:00:00 2001 From: helios2003 Date: Thu, 2 Jan 2025 18:35:25 +0530 Subject: [PATCH] show >dice after entering blocklisted command --- .../components/Shell/hooks/useShell.tsx | 7 ++----- apps/playground-web/shared/utils/shellUtils.ts | 14 +++++++++++++- apps/playground-web/types/index.d.ts | 5 +++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/apps/playground-web/components/Shell/hooks/useShell.tsx b/apps/playground-web/components/Shell/hooks/useShell.tsx index 9c46973..ed55e1c 100644 --- a/apps/playground-web/components/Shell/hooks/useShell.tsx +++ b/apps/playground-web/components/Shell/hooks/useShell.tsx @@ -2,7 +2,7 @@ import { useState, useEffect, useRef, KeyboardEvent, ChangeEvent } from 'react'; // utils -import { handleCommand } from '@/shared/utils/shellUtils'; +import { handleCommand, handleInvalidCommand } from '@/shared/utils/shellUtils'; import blocklistedCommands from '@/shared/utils/blocklist'; export const useShell = ( @@ -29,10 +29,7 @@ export const useShell = ( } if (blocklistedCommands.includes(commandName)) { - setOutput((prevOutput) => [ - ...prevOutput, - `(error) ERR unknown command '${commandName}'`, - ]); + handleInvalidCommand({ command, setOutput }); } else { handleCommand({ command, setOutput, onCommandExecuted }); // Execute if not blocklisted } diff --git a/apps/playground-web/shared/utils/shellUtils.ts b/apps/playground-web/shared/utils/shellUtils.ts index 269f94e..e3a4681 100644 --- a/apps/playground-web/shared/utils/shellUtils.ts +++ b/apps/playground-web/shared/utils/shellUtils.ts @@ -1,9 +1,21 @@ // src/shared/utils/shellUtils.ts import { executeShellCommandOnServer } from '@/lib/api'; -import { CommandHandler } from '@/types'; +import { CommandHandler, InvalidCommandHandler } from '@/types'; import { handleResult } from '@/shared/utils/commonUtils'; +export const handleInvalidCommand = async ({ + command, + setOutput +}: InvalidCommandHandler) => { + const newOutput = `dice > ${command}`; + setOutput((prevOutput: any) => [ + ...prevOutput, + newOutput, + `(error) ERR unknown command '${command}'`, + ]); +} + export const handleCommand = async ({ command, setOutput, diff --git a/apps/playground-web/types/index.d.ts b/apps/playground-web/types/index.d.ts index 93a2a35..75e12ea 100644 --- a/apps/playground-web/types/index.d.ts +++ b/apps/playground-web/types/index.d.ts @@ -5,3 +5,8 @@ export interface CommandHandler { setOutput: React.Dispatch>; onCommandExecuted: (commandsLeft: number, cleanupTimeLeft: number) => void; } + +export interface InvalidCommandHandler { + command: string; + setOutput: React.Dispatch>; +}