Skip to content

Commit

Permalink
first draft of changes to integrate rate-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
aasifkhan7 authored Oct 10, 2024
1 parent 1feedd2 commit bc60e1a
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 13 deletions.
3 changes: 2 additions & 1 deletion apps/playground-web/components/CLI/hooks/useCli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const useCli = (decreaseCommandsLeft: () => void) => {
//Initialise the command history with sessionStorage
const [commandHistory, setCommandHistory] = useState<string[]>([]);
const [historyIndex, setHistoryIndex] = useState<number>(-1);
const [newCommandsLeft, setNewCommandsLeft] = useState<number>(1000);

Check warning on line 16 in apps/playground-web/components/CLI/hooks/useCli.tsx

View workflow job for this annotation

GitHub Actions / Build and Test

'newCommandsLeft' is assigned a value but never used

// useRefs
const terminalRef = useRef<HTMLDivElement>(null);
Expand All @@ -27,7 +28,7 @@ export const useCli = (decreaseCommandsLeft: () => void) => {
`(error) ERR unknown command '${commandName}'`,
]);
} else {
handleCommand({ command, setOutput }); // Execute if not blocklisted
handleCommand({ command, setOutput, setNewCommandsLeft }); // Execute if not blocklisted
}

setCommand(''); // Clear input
Expand Down
2 changes: 1 addition & 1 deletion apps/playground-web/components/Playground/TerminalUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function TerminalUI() {
</div>
</div>
<div className="h-64 md:h-[30rem] bg-gray-100 rounded-lg overflow-hidden shadow-md">
<Shell decreaseCommandsLeft={decreaseCommandsLeft} />
<Shell/>
</div>
</div>
<TerminalCounter commandsLeft={commandsLeft} />
Expand Down
4 changes: 2 additions & 2 deletions apps/playground-web/components/Shell/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ interface ShellProps {
decreaseCommandsLeft: () => void;
}

export default function Shell({ decreaseCommandsLeft }: ShellProps) {
export default function Shell({ }: ShellProps) {

Check warning on line 11 in apps/playground-web/components/Shell/Shell.tsx

View workflow job for this annotation

GitHub Actions / Build and Test

Unexpected empty object pattern
const {
handleInputChange,
handleKeyDown,
terminalRef,
inputRef,
output,
command,
} = useShell(decreaseCommandsLeft);
} = useShell();
return (
<div
ref={terminalRef}
Expand Down
6 changes: 3 additions & 3 deletions apps/playground-web/components/Shell/hooks/useShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { useState, useEffect, useRef, KeyboardEvent, ChangeEvent } from 'react';
import { handleCommand } from '@/shared/utils/shellUtils';
import blocklistedCommands from '@/shared/utils/blocklist';

export const useShell = (decreaseCommandsLeft: () => void) => {
export const useShell = () => {
// 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);
const [newCommandsLeft, setNewCommandsLeft] = useState<number>(1000);

Check warning on line 16 in apps/playground-web/components/Shell/hooks/useShell.tsx

View workflow job for this annotation

GitHub Actions / Build and Test

'newCommandsLeft' is assigned a value but never used

// useRefs
const terminalRef = useRef<HTMLDivElement>(null);
Expand All @@ -32,11 +33,10 @@ export const useShell = (decreaseCommandsLeft: () => void) => {
`(error) ERR unknown command '${commandName}'`,
]);
} else {
handleCommand({ command, setOutput }); // Execute if not blocklisted
handleCommand({ command, setOutput, setNewCommandsLeft }); // Execute if not blocklisted
}

setCommand(''); // Clear input
decreaseCommandsLeft(); // Call to update remaining commands
};

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions apps/playground-web/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export const executeShellCommandOnServer = async (

// Check if the response contains data or if it's an error response
if (response?.data) {
return response.data;
return response;
} else if (response?.error) {
return response.error;
return response;
} else {
throw new Error('Unexpected response structure');
}
Expand Down
7 changes: 5 additions & 2 deletions apps/playground-web/shared/utils/cliUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import { executeShellCommandOnServer } from '@/lib/api';
import { CommandHandler } from '@/types';

export const handleCommand = async ({ command, setOutput }: CommandHandler) => {
export const handleCommand = async ({ command, setOutput, setNewCommandsLeft }: CommandHandler) => {
const newOutput = `dice > ${command}`;
let result: string;
let result: any;

Check warning on line 8 in apps/playground-web/shared/utils/cliUtils.ts

View workflow job for this annotation

GitHub Actions / Build and Test

Unexpected any. Specify a different type

const [cmd, ...args] = command.split(' ');
if (!cmd) {
Expand All @@ -27,6 +27,7 @@ export const handleCommand = async ({ command, setOutput }: CommandHandler) => {
const cmdOptions = { key: key };
result = await executeShellCommandOnServer(cmd, cmdOptions);
setOutput((prevOutput) => [...prevOutput, newOutput, result]);
setNewCommandsLeft((prevOutput) => (result.headers['x-ratelimit-remaining']));

Check warning on line 30 in apps/playground-web/shared/utils/cliUtils.ts

View workflow job for this annotation

GitHub Actions / Build and Test

'prevOutput' is defined but never used
} catch (error: unknown) {
console.error('Error executing command:', error);
result = 'Error executing command';
Expand All @@ -41,6 +42,7 @@ export const handleCommand = async ({ command, setOutput }: CommandHandler) => {
const cmdOptions = { key: key, value: value };
result = await executeShellCommandOnServer(cmd, cmdOptions);
setOutput((prevOutput) => [...prevOutput, newOutput, result]);
setNewCommandsLeft((prevOutput) => (result.headers['x-ratelimit-remaining']));

Check warning on line 45 in apps/playground-web/shared/utils/cliUtils.ts

View workflow job for this annotation

GitHub Actions / Build and Test

'prevOutput' is defined but never used
} catch (error: unknown) {
console.error('Error executing command:', error);
result = 'Error executing command';
Expand All @@ -60,6 +62,7 @@ export const handleCommand = async ({ command, setOutput }: CommandHandler) => {
const cmdOptions = { keys: [keys] };
result = await executeShellCommandOnServer(cmd, cmdOptions);
setOutput((prevOutput) => [...prevOutput, newOutput, result]);
setNewCommandsLeft((prevOutput) => (result.headers['x-ratelimit-remaining']));

Check warning on line 65 in apps/playground-web/shared/utils/cliUtils.ts

View workflow job for this annotation

GitHub Actions / Build and Test

'prevOutput' is defined but never used
} catch (error: unknown) {
console.error('Error executing command:', error);
result = 'Error executing command';
Expand Down
5 changes: 3 additions & 2 deletions apps/playground-web/shared/utils/shellUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import { executeShellCommandOnServer } from '@/lib/api';
import { CommandHandler } from '@/types';

export const handleCommand = async ({ command, setOutput }: CommandHandler) => {
export const handleCommand = async ({ command, setOutput, setNewCommandsLeft }: CommandHandler) => {
const newOutput = `dice > ${command}`;
let result: string;
let result: any;

Check warning on line 8 in apps/playground-web/shared/utils/shellUtils.ts

View workflow job for this annotation

GitHub Actions / Build and Test

Unexpected any. Specify a different type

const [cmd, ...args] = command.split(' ');

Expand All @@ -15,6 +15,7 @@ export const handleCommand = async ({ command, setOutput }: CommandHandler) => {
try {
result = await executeShellCommandOnServer(cmd, args);
setOutput((prevOutput) => [...prevOutput, newOutput, result]);
setNewCommandsLeft((prevOutput) => (result.headers['x-ratelimit-remaining']));

Check warning on line 18 in apps/playground-web/shared/utils/shellUtils.ts

View workflow job for this annotation

GitHub Actions / Build and Test

'prevOutput' is defined but never used
} catch (error: unknown) {
console.error('Error executing command:', error);
result = 'Error executing command';
Expand Down
1 change: 1 addition & 0 deletions apps/playground-web/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import * as React from 'react';
export interface CommandHandler {
command: string;
setOutput: React.Dispatch<React.SetStateAction<string[]>>;
setNewCommandsLeft: React.Dispatch<React.SetStateAction<number>>;
}

0 comments on commit bc60e1a

Please sign in to comment.