Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate cleanup time from playground-mono #76

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions apps/playground-web/components/Playground/TerminalUI.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Dice1, Dice3, Dice5, Info } from 'lucide-react';
import Shell from '../Shell/Shell';
import { formatTime } from '@/shared/utils/commonUtils';
import { useTimer } from '@/shared/hooks/useTimer';
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);
if (cleanup !== -1) {
setCleanupTimeLeft(cleanup);
}
};

return (
Expand All @@ -27,13 +30,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 +53,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 @@ -12,8 +12,8 @@
body: MonoResponse; // MonoResponse for body
};
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 @@ -23,13 +23,13 @@
onCommandExecuted,
}: HandleResultProps) => {
if (result?.body?.data) {
setOutput((prevOutput: any) => [

Check warning on line 26 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
...prevOutput,
newOutput,
result?.body?.data,
]);
} else if (result?.body?.error) {
setOutput((prevOutput: any) => [

Check warning on line 32 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
...prevOutput,
newOutput,
result?.body?.error,
Expand All @@ -37,5 +37,6 @@
}

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;
}
Loading