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