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 11:02:31 -07:00
|
|
|
} from "react";
|
|
|
|
|
import { useArtifactSelector } from "@/hooks/use-artifact";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-03-20 09:37:02 +00:00
|
|
|
import { Button } from "../ui/button";
|
|
|
|
|
import { Spinner } from "../ui/spinner";
|
2025-09-21 11:02:31 -07:00
|
|
|
import { CrossSmallIcon, TerminalWindowIcon } from "./icons";
|
2024-12-16 18:14:40 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
export type ConsoleOutputContent = {
|
|
|
|
|
type: "text" | "image";
|
2025-01-27 14:19:47 +05:30
|
|
|
value: string;
|
2025-09-21 11:02:31 -07:00
|
|
|
};
|
2025-01-27 14:19:47 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
export type ConsoleOutput = {
|
2025-01-27 14:19:47 +05:30
|
|
|
id: string;
|
2025-09-21 11:02:31 -07:00
|
|
|
status: "in_progress" | "loading_packages" | "completed" | "failed";
|
|
|
|
|
contents: ConsoleOutputContent[];
|
|
|
|
|
};
|
2025-01-27 14:19:47 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
type ConsoleProps = {
|
|
|
|
|
consoleOutputs: ConsoleOutput[];
|
|
|
|
|
setConsoleOutputs: Dispatch<SetStateAction<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);
|
|
|
|
|
|
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 11:02:31 -07:00
|
|
|
[isResizing]
|
2024-12-16 18:14:40 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-09-21 11:02:31 -07:00
|
|
|
window.addEventListener("mousemove", resize);
|
|
|
|
|
window.addEventListener("mouseup", stopResizing);
|
2024-12-16 18:14:40 +05:30
|
|
|
return () => {
|
2025-09-21 11:02:31 -07:00
|
|
|
window.removeEventListener("mousemove", resize);
|
|
|
|
|
window.removeEventListener("mouseup", stopResizing);
|
2024-12-16 18:14:40 +05:30
|
|
|
};
|
|
|
|
|
}, [resize, stopResizing]);
|
|
|
|
|
|
2026-03-20 09:37:02 +00:00
|
|
|
const consoleContainerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
2024-12-16 18:14:40 +05:30
|
|
|
useEffect(() => {
|
2026-03-20 09:37:02 +00:00
|
|
|
if (consoleOutputs.length > 0) {
|
|
|
|
|
consoleContainerRef.current?.scrollTo({ top: 0, behavior: "smooth" });
|
|
|
|
|
}
|
|
|
|
|
}, [consoleOutputs.length]);
|
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-21 11:02:31 -07:00
|
|
|
aria-label="Resize console"
|
|
|
|
|
aria-orientation="horizontal"
|
|
|
|
|
aria-valuemax={maxHeight}
|
|
|
|
|
aria-valuemin={minHeight}
|
|
|
|
|
aria-valuenow={height}
|
2025-09-09 15:44:07 -04:00
|
|
|
className="fixed z-50 h-2 w-full cursor-ns-resize"
|
2025-09-21 11:02:31 -07:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === "ArrowUp") {
|
|
|
|
|
setHeight((prev) => Math.min(prev + 10, maxHeight));
|
|
|
|
|
} else if (e.key === "ArrowDown") {
|
|
|
|
|
setHeight((prev) => Math.max(prev - 10, minHeight));
|
|
|
|
|
}
|
|
|
|
|
}}
|
2024-12-16 18:14:40 +05:30
|
|
|
onMouseDown={startResizing}
|
2025-09-21 12:03:29 +01:00
|
|
|
role="slider"
|
2025-09-21 11:02:31 -07:00
|
|
|
style={{ bottom: height - 4 }}
|
|
|
|
|
tabIndex={0}
|
2024-12-16 18:14:40 +05:30
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
2026-03-20 09:37:02 +00:00
|
|
|
"fixed bottom-0 z-40 flex w-full flex-col overflow-x-hidden overflow-y-auto border-t border-border/50 bg-background",
|
|
|
|
|
{ "select-none": isResizing }
|
2024-12-16 18:14:40 +05:30
|
|
|
)}
|
2026-03-20 09:37:02 +00:00
|
|
|
ref={consoleContainerRef}
|
2024-12-16 18:14:40 +05:30
|
|
|
style={{ height }}
|
|
|
|
|
>
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="sticky top-0 z-50 flex h-10 w-full items-center justify-between border-b border-border/50 bg-background px-3">
|
|
|
|
|
<div className="flex items-center gap-2.5 text-[13px] text-muted-foreground">
|
|
|
|
|
<TerminalWindowIcon />
|
|
|
|
|
<span>Console</span>
|
2024-12-16 18:14:40 +05:30
|
|
|
</div>
|
|
|
|
|
<Button
|
2026-03-20 09:37:02 +00:00
|
|
|
className="size-7 text-muted-foreground/50 hover:text-foreground"
|
2025-09-21 12:03:29 +01:00
|
|
|
onClick={() => setConsoleOutputs([])}
|
2026-03-20 09:37:02 +00:00
|
|
|
size="icon-sm"
|
2025-09-21 11:02:31 -07:00
|
|
|
variant="ghost"
|
2024-12-16 18:14:40 +05:30
|
|
|
>
|
|
|
|
|
<CrossSmallIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="bg-background">
|
|
|
|
|
{[...consoleOutputs].reverse().map((consoleOutput, index) => (
|
2024-12-16 18:14:40 +05:30
|
|
|
<div
|
2026-03-20 09:37:02 +00:00
|
|
|
className="flex border-b border-border/30 px-4 py-2.5 font-mono text-[12px] leading-relaxed"
|
2025-09-21 11:02:31 -07:00
|
|
|
key={consoleOutput.id}
|
2024-12-16 18:14:40 +05:30
|
|
|
>
|
|
|
|
|
<div
|
2026-03-20 09:37:02 +00:00
|
|
|
className={cn("w-10 shrink-0 tabular-nums", {
|
2025-09-21 11:02:31 -07:00
|
|
|
"text-muted-foreground": [
|
|
|
|
|
"in_progress",
|
|
|
|
|
"loading_packages",
|
2025-01-08 00:00:21 +05:30
|
|
|
].includes(consoleOutput.status),
|
2025-09-21 11:02:31 -07:00
|
|
|
"text-emerald-500": consoleOutput.status === "completed",
|
|
|
|
|
"text-red-400": consoleOutput.status === "failed",
|
2024-12-16 18:14:40 +05:30
|
|
|
})}
|
|
|
|
|
>
|
2026-03-20 09:37:02 +00:00
|
|
|
[{consoleOutputs.length - index}]
|
2024-12-16 18:14:40 +05:30
|
|
|
</div>
|
2025-09-21 11:02:31 -07:00
|
|
|
{["in_progress", "loading_packages"].includes(
|
|
|
|
|
consoleOutput.status
|
2025-01-08 00:00:21 +05:30
|
|
|
) ? (
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<Spinner className="size-3.5" />
|
|
|
|
|
<span className="text-muted-foreground">
|
2025-09-21 11:02:31 -07: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 11:02:31 -07:00
|
|
|
content.type === "text" ? content.value : null
|
2025-01-08 16:37:49 +05:30
|
|
|
)
|
2025-01-08 00:00:21 +05:30
|
|
|
: null}
|
2026-03-20 09:37:02 +00:00
|
|
|
</span>
|
2024-12-16 18:14:40 +05:30
|
|
|
</div>
|
|
|
|
|
) : (
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="no-scrollbar flex w-full min-w-0 flex-col gap-2 overflow-x-auto text-foreground">
|
2026-03-13 13:12:33 -07:00
|
|
|
{consoleOutput.contents.map((content) =>
|
2025-09-21 11:02:31 -07:00
|
|
|
content.type === "image" ? (
|
2026-03-20 09:37:02 +00:00
|
|
|
<picture
|
|
|
|
|
key={`${consoleOutput.id}-img-${content.value.slice(0, 32)}`}
|
|
|
|
|
>
|
2025-01-08 00:00:21 +05:30
|
|
|
<img
|
|
|
|
|
alt="output"
|
2026-03-20 09:37:02 +00:00
|
|
|
className="max-w-full rounded-md"
|
2025-09-21 11:02:31 -07:00
|
|
|
src={content.value}
|
2025-01-08 00:00:21 +05:30
|
|
|
/>
|
|
|
|
|
</picture>
|
|
|
|
|
) : (
|
|
|
|
|
<div
|
2025-08-28 14:15:36 +01:00
|
|
|
className="w-full whitespace-pre-line break-words"
|
2026-03-20 09:37:02 +00:00
|
|
|
key={`${consoleOutput.id}-txt-${content.value.slice(0, 32)}`}
|
2025-01-08 00:00:21 +05:30
|
|
|
>
|
|
|
|
|
{content.value}
|
|
|
|
|
</div>
|
2025-09-21 11:02:31 -07:00
|
|
|
)
|
2025-01-08 00:00:21 +05:30
|
|
|
)}
|
2024-12-16 18:14:40 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
) : null;
|
|
|
|
|
}
|