'use client';
import { IconCheck, IconX, IconLoader2, IconTerminal } from '@tabler/icons-react';
import { cn } from '@/lib/utils';
import type { Message } from '@/lib/types/http';
import { Badge } from '@kandev/ui/badge';
interface ScriptExecutionMetadata {
script_type: 'setup' | 'cleanup';
command: string;
status: 'starting' | 'running' | 'exited' | 'failed';
exit_code?: number;
started_at?: string;
completed_at?: string;
process_id?: string;
error?: string;
}
export function ScriptExecutionMessage({ comment }: { comment: Message }) {
const metadata = comment.metadata as ScriptExecutionMetadata | undefined;
// Show fallback if metadata is missing or incomplete
if (!metadata || !metadata.script_type) {
return (
Script Execution (metadata unavailable)
{comment.content && (
{comment.content}
)}
);
}
const { script_type, command, status, exit_code, error } = metadata;
const isSetup = script_type === 'setup';
const isRunning = status === 'starting' || status === 'running';
const isSuccess = status === 'exited' && exit_code === 0;
// Status icon and styling
const StatusIcon = isRunning
? IconLoader2
: isSuccess
? IconCheck
: IconX;
const statusText = isRunning
? 'Running...'
: isSuccess
? 'Completed'
: 'Failed';
const statusColor = isRunning
? 'text-blue-500'
: isSuccess
? 'text-green-500'
: 'text-red-500';
const borderColor = isRunning
? 'border-blue-500/40'
: isSuccess
? 'border-green-500/40'
: 'border-red-500/40';
const bgColor = isRunning
? 'bg-blue-500/5'
: isSuccess
? 'bg-green-500/5'
: 'bg-red-500/5';
return (
{/* Header */}
{isSetup ? 'Setup Script' : 'Cleanup Script'}
{statusText}
{/* Command */}
$ {command}
{/* Output */}
{comment.content && comment.content.trim() !== '' && (
)}
{/* Error message */}
{error && (
{error}
)}
{/* Footer with exit code */}
{!isRunning && exit_code !== undefined && (
Exit code: {exit_code}
{metadata.started_at && metadata.completed_at && (
Duration: {calculateDuration(metadata.started_at, metadata.completed_at)}
)}
)}
);
}
// Helper function to calculate duration
function calculateDuration(startedAt: string, completedAt: string): string {
try {
const start = new Date(startedAt).getTime();
const end = new Date(completedAt).getTime();
const durationMs = end - start;
if (durationMs < 1000) {
return `${durationMs}ms`;
} else if (durationMs < 60000) {
return `${(durationMs / 1000).toFixed(1)}s`;
} else {
const minutes = Math.floor(durationMs / 60000);
const seconds = Math.floor((durationMs % 60000) / 1000);
return `${minutes}m ${seconds}s`;
}
} catch {
return 'N/A';
}
}