import { TerminalWindowIcon, CrossSmallIcon } from './icons'; import { Loader } from './elements/loader'; import { Button } from './ui/button'; import { type Dispatch, type SetStateAction, useCallback, useEffect, useRef, useState, } from 'react'; import { cn } from '@/lib/utils'; import { useArtifactSelector } from '@/hooks/use-artifact'; export interface ConsoleOutputContent { type: 'text' | 'image'; value: string; } export interface ConsoleOutput { id: string; status: 'in_progress' | 'loading_packages' | 'completed' | 'failed'; contents: Array; } interface ConsoleProps { consoleOutputs: Array; setConsoleOutputs: Dispatch>>; } export function Console({ consoleOutputs, setConsoleOutputs }: ConsoleProps) { const [height, setHeight] = useState(300); const [isResizing, setIsResizing] = useState(false); const consoleEndRef = useRef(null); const isArtifactVisible = useArtifactSelector((state) => state.isVisible); 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]); useEffect(() => { if (!isArtifactVisible) { setConsoleOutputs([]); } }, [isArtifactVisible, setConsoleOutputs]); return consoleOutputs.length > 0 ? ( <>
Console
{consoleOutputs.map((consoleOutput, index) => (
[{index + 1}]
{['in_progress', 'loading_packages'].includes( consoleOutput.status, ) ? (
{consoleOutput.status === 'in_progress' ? 'Initializing...' : consoleOutput.status === 'loading_packages' ? consoleOutput.contents.map((content) => content.type === 'text' ? content.value : null, ) : null}
) : (
{consoleOutput.contents.map((content, index) => content.type === 'image' ? ( output ) : (
{content.value}
), )}
)}
))}
) : null; }