2024-12-16 18:14:40 +05:30
|
|
|
import { TerminalWindowIcon, LoaderIcon, CrossSmallIcon } from './icons';
|
|
|
|
|
import { Button } from './ui/button';
|
|
|
|
|
import {
|
|
|
|
|
Dispatch,
|
|
|
|
|
SetStateAction,
|
|
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
|
|
|
|
useRef,
|
|
|
|
|
useState,
|
|
|
|
|
} from 'react';
|
|
|
|
|
import { ConsoleOutput } from './block';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
2025-01-08 00:38:26 +05:30
|
|
|
import { useBlockSelector } from '@/hooks/use-block';
|
2024-12-16 18:14:40 +05:30
|
|
|
|
|
|
|
|
interface ConsoleProps {
|
|
|
|
|
consoleOutputs: Array<ConsoleOutput>;
|
|
|
|
|
setConsoleOutputs: Dispatch<SetStateAction<Array<ConsoleOutput>>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function Console({ consoleOutputs, setConsoleOutputs }: ConsoleProps) {
|
|
|
|
|
const [height, setHeight] = useState<number>(300);
|
|
|
|
|
const [isResizing, setIsResizing] = useState(false);
|
|
|
|
|
const consoleEndRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
2025-01-08 00:38:26 +05:30
|
|
|
const isBlockVisible = useBlockSelector((state) => state.isVisible);
|
|
|
|
|
|
2024-12-16 18:14:40 +05:30
|
|
|
const minHeight = 100;
|
|
|
|
|
const maxHeight = 800;
|
|
|
|
|
|
|
|
|
|
const startResizing = useCallback(() => {
|
|
|
|
|
setIsResizing(true);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const stopResizing = useCallback(() => {
|
|
|
|
|
setIsResizing(false);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const resize = useCallback(
|
|
|
|
|
(e: MouseEvent) => {
|
|
|
|
|
if (isResizing) {
|
|
|
|
|
const newHeight = window.innerHeight - e.clientY;
|
|
|
|
|
if (newHeight >= minHeight && newHeight <= maxHeight) {
|
|
|
|
|
setHeight(newHeight);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[isResizing],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener('mousemove', resize);
|
|
|
|
|
window.addEventListener('mouseup', stopResizing);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('mousemove', resize);
|
|
|
|
|
window.removeEventListener('mouseup', stopResizing);
|
|
|
|
|
};
|
|
|
|
|
}, [resize, stopResizing]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
consoleEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
}, [consoleOutputs]);
|
|
|
|
|
|
2025-01-08 00:38:26 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isBlockVisible) {
|
|
|
|
|
setConsoleOutputs([]);
|
|
|
|
|
}
|
|
|
|
|
}, [isBlockVisible, setConsoleOutputs]);
|
|
|
|
|
|
2024-12-16 18:14:40 +05:30
|
|
|
return consoleOutputs.length > 0 ? (
|
|
|
|
|
<>
|
|
|
|
|
<div
|
|
|
|
|
className="h-2 w-full fixed cursor-ns-resize z-50"
|
|
|
|
|
onMouseDown={startResizing}
|
|
|
|
|
style={{ bottom: height - 4 }}
|
2024-12-19 17:05:04 +05:30
|
|
|
role="slider"
|
|
|
|
|
aria-valuenow={minHeight}
|
2024-12-16 18:14:40 +05:30
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
2025-01-08 17:36:50 +05:30
|
|
|
'fixed flex flex-col bottom-0 dark:bg-zinc-900 bg-zinc-50 w-full border-t z-40 overflow-y-scroll overflow-x-hidden dark:border-zinc-700 border-zinc-200',
|
2024-12-16 18:14:40 +05:30
|
|
|
{
|
|
|
|
|
'select-none': isResizing,
|
|
|
|
|
},
|
|
|
|
|
)}
|
|
|
|
|
style={{ height }}
|
|
|
|
|
>
|
|
|
|
|
<div className="flex flex-row justify-between items-center w-full h-fit border-b dark:border-zinc-700 border-zinc-200 px-2 py-1 sticky top-0 z-50 bg-muted">
|
|
|
|
|
<div className="text-sm pl-2 dark:text-zinc-50 text-zinc-800 flex flex-row gap-3 items-center">
|
|
|
|
|
<div className="text-muted-foreground">
|
|
|
|
|
<TerminalWindowIcon />
|
|
|
|
|
</div>
|
|
|
|
|
<div>Console</div>
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
className="size-fit p-1 hover:dark:bg-zinc-700 hover:bg-zinc-200"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={() => setConsoleOutputs([])}
|
|
|
|
|
>
|
|
|
|
|
<CrossSmallIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
{consoleOutputs.map((consoleOutput, index) => (
|
|
|
|
|
<div
|
|
|
|
|
key={consoleOutput.id}
|
|
|
|
|
className="px-4 py-2 flex flex-row text-sm border-b dark:border-zinc-700 border-zinc-200 dark:bg-zinc-900 bg-zinc-50 font-mono"
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={cn('w-12 shrink-0', {
|
2025-01-08 00:00:21 +05:30
|
|
|
'text-muted-foreground': [
|
|
|
|
|
'in_progress',
|
|
|
|
|
'loading_packages',
|
|
|
|
|
].includes(consoleOutput.status),
|
2024-12-16 18:14:40 +05:30
|
|
|
'text-emerald-500': consoleOutput.status === 'completed',
|
|
|
|
|
'text-red-400': consoleOutput.status === 'failed',
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
[{index + 1}]
|
|
|
|
|
</div>
|
2025-01-08 00:00:21 +05:30
|
|
|
{['in_progress', 'loading_packages'].includes(
|
|
|
|
|
consoleOutput.status,
|
|
|
|
|
) ? (
|
|
|
|
|
<div className="flex flex-row gap-2">
|
2025-01-08 16:37:49 +05:30
|
|
|
<div className="animate-spin size-fit self-center mb-auto mt-0.5">
|
2025-01-08 00:00:21 +05:30
|
|
|
<LoaderIcon />
|
|
|
|
|
</div>
|
|
|
|
|
<div className="text-muted-foreground">
|
|
|
|
|
{consoleOutput.status === 'in_progress'
|
|
|
|
|
? 'Initializing...'
|
|
|
|
|
: consoleOutput.status === 'loading_packages'
|
2025-01-08 16:37:49 +05:30
|
|
|
? consoleOutput.contents.map((content) =>
|
|
|
|
|
content.type === 'text' ? content.value : null,
|
|
|
|
|
)
|
2025-01-08 00:00:21 +05:30
|
|
|
: null}
|
|
|
|
|
</div>
|
2024-12-16 18:14:40 +05:30
|
|
|
</div>
|
|
|
|
|
) : (
|
2025-01-08 17:36:50 +05:30
|
|
|
<div className="dark:text-zinc-50 text-zinc-900 w-full flex flex-col gap-2 overflow-x-scroll">
|
2025-01-08 00:00:21 +05:30
|
|
|
{consoleOutput.contents.map((content, index) =>
|
|
|
|
|
content.type === 'image' ? (
|
|
|
|
|
<picture key={`${consoleOutput.id}-${index}`}>
|
|
|
|
|
<img
|
|
|
|
|
src={content.value}
|
|
|
|
|
alt="output"
|
|
|
|
|
className="rounded-md max-w-[600px] w-full"
|
|
|
|
|
/>
|
|
|
|
|
</picture>
|
|
|
|
|
) : (
|
|
|
|
|
<div
|
|
|
|
|
key={`${consoleOutput.id}-${index}`}
|
2025-01-08 17:36:50 +05:30
|
|
|
className="whitespace-pre-line break-words w-full"
|
2025-01-08 00:00:21 +05:30
|
|
|
>
|
|
|
|
|
{content.value}
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
)}
|
2024-12-16 18:14:40 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
<div ref={consoleEndRef} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
) : null;
|
|
|
|
|
}
|