Skip to content

Commit

Permalink
integrate cleanup time from playground-mono
Browse files Browse the repository at this point in the history
  • Loading branch information
Aasif Khan authored and Aasif Khan committed Oct 25, 2024
1 parent 02369c1 commit 3574cf7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
20 changes: 15 additions & 5 deletions apps/playground-web/components/Playground/TerminalUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { useState } from 'react';
import Tooltip from '../Overlays/Tooltip';
export function TerminalUI({ initialCommandsLeft = 1000 }) {
const [commandsLeft, setCommandsLeft] = useState(initialCommandsLeft);
const handleCommandExecuted = (commands: number) => {
const [cleanupTimeLeft, setCleanupTimeLeft] = useState(15 * 60);
const handleCommandExecuted = (commands: number, cleanup: number) => {
setCommandsLeft(commands);
setCleanupTimeLeft(cleanup);
};

return (
Expand All @@ -27,13 +29,21 @@ export function TerminalUI({ initialCommandsLeft = 1000 }) {
<Shell onCommandExecuted={handleCommandExecuted} />
</div>
</div>
<TerminalCounter commandsLeft={commandsLeft} />
<TerminalCounter
commandsLeft={commandsLeft}
cleanupTimeLeft={cleanupTimeLeft}
/>
</>
);
}

function TerminalCounter({ commandsLeft }: { commandsLeft: number }) {
const { timeLeft } = useTimer(15 * 60);
function TerminalCounter({
commandsLeft,
cleanupTimeLeft,
}: {
commandsLeft: number;
cleanupTimeLeft: number;
}) {
return (
<div className="flex flex-col" data-testid="terminal-counter">
<div className="flex flex-row justify-between text-gray-900 mt-4">
Expand All @@ -42,7 +52,7 @@ function TerminalCounter({ commandsLeft }: { commandsLeft: number }) {
className="flex items-center justify-between border border-gray-400 text-sm bg-transparent p-3 rounded-lg"
data-testid="cleanup-timer"
>
<span>Cleanup in: {formatTime(timeLeft)} mins</span>
<span>Cleanup in: {formatTime(cleanupTimeLeft)} mins</span>
</div>
<Tooltip message="The time remaining until cleanup is initiated." />
</div>
Expand Down
2 changes: 1 addition & 1 deletion apps/playground-web/components/Shell/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { useShell } from './hooks/useShell';

interface ShellProps {
onCommandExecuted: (commandsLeft: number) => void;
onCommandExecuted: (commandsLeft: number, cleanupTimeLeft: number) => void;
}

export default function Shell({ onCommandExecuted }: ShellProps) {
Expand Down
4 changes: 3 additions & 1 deletion apps/playground-web/components/Shell/hooks/useShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { useState, useEffect, useRef, KeyboardEvent, ChangeEvent } from 'react';
import { handleCommand } from '@/shared/utils/shellUtils';
import blocklistedCommands from '@/shared/utils/blocklist';

export const useShell = (onCommandExecuted: (commandsLeft: number) => void) => {
export const useShell = (
onCommandExecuted: (commandsLeft: number, cleanupTimeLeft: number) => void,
) => {
// states
const [command, setCommand] = useState('');
const [output, setOutput] = useState<string[]>([]);
Expand Down
5 changes: 3 additions & 2 deletions apps/playground-web/shared/utils/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface HandleResultProps {
};
newOutput: string;
setOutput: (prevOutput: any) => any; // Adjust type as necessary

Check warning on line 15 in apps/playground-web/shared/utils/commonUtils.ts

View workflow job for this annotation

GitHub Actions / Build and Test

Unexpected any. Specify a different type

Check warning on line 15 in apps/playground-web/shared/utils/commonUtils.ts

View workflow job for this annotation

GitHub Actions / Build and Test

Unexpected any. Specify a different type
onCommandExecuted: (commandsLeft: number) => void; // Allow undefined
onCommandExecuted: (commandsLeft: number, cleanupTimeLeft: number) => void; // Allow undefined
}

export const handleResult = ({
Expand All @@ -37,5 +37,6 @@ export const handleResult = ({
}

const commandsLeft = Number(result.headers['x-ratelimit-remaining']);
onCommandExecuted(commandsLeft);
const cleanupTimeLeft = Number(result.headers['x-next-cleanup-time']);
onCommandExecuted(commandsLeft, cleanupTimeLeft);
};
2 changes: 1 addition & 1 deletion apps/playground-web/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import * as React from 'react';
export interface CommandHandler {
command: string;
setOutput: React.Dispatch<React.SetStateAction<string[]>>;
onCommandExecuted: (commandsLeft: number) => void;
onCommandExecuted: (commandsLeft: number, cleanupTimeLeft: number) => void;
}

0 comments on commit 3574cf7

Please sign in to comment.