2025-11-23 18:23:51 +01:00
|
|
|
'use client';
|
|
|
|
|
|
2025-11-23 19:41:21 +01:00
|
|
|
import { useState } from 'react';
|
2025-11-23 19:46:47 +01:00
|
|
|
import { Play, Square, RotateCw, Activity, Zap, Terminal } from 'lucide-react';
|
2025-11-23 18:23:51 +01:00
|
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
import { Badge } from '@/components/ui/badge';
|
2025-11-23 18:43:55 +01:00
|
|
|
import { ProcessInfo, ProcessStateCode, getProcessStateClass, formatUptime, canStartProcess, canStopProcess } from '@/lib/supervisor/types';
|
2025-11-23 18:23:51 +01:00
|
|
|
import { useStartProcess, useStopProcess, useRestartProcess } from '@/lib/hooks/useSupervisor';
|
2025-11-23 19:41:21 +01:00
|
|
|
import { SignalSender } from './SignalSender';
|
2025-11-23 19:46:47 +01:00
|
|
|
import { StdinInput } from './StdinInput';
|
2025-11-23 18:23:51 +01:00
|
|
|
import { cn } from '@/lib/utils/cn';
|
|
|
|
|
|
|
|
|
|
interface ProcessCardProps {
|
|
|
|
|
process: ProcessInfo;
|
2025-11-23 19:14:04 +01:00
|
|
|
isSelected?: boolean;
|
|
|
|
|
onSelectionChange?: (processId: string, selected: boolean) => void;
|
2025-11-23 18:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-23 19:14:04 +01:00
|
|
|
export function ProcessCard({ process, isSelected = false, onSelectionChange }: ProcessCardProps) {
|
2025-11-23 19:41:21 +01:00
|
|
|
const [showSignalModal, setShowSignalModal] = useState(false);
|
2025-11-23 19:46:47 +01:00
|
|
|
const [showStdinModal, setShowStdinModal] = useState(false);
|
2025-11-23 18:23:51 +01:00
|
|
|
const startMutation = useStartProcess();
|
|
|
|
|
const stopMutation = useStopProcess();
|
|
|
|
|
const restartMutation = useRestartProcess();
|
|
|
|
|
|
|
|
|
|
const fullName = `${process.group}:${process.name}`;
|
|
|
|
|
const isLoading = startMutation.isPending || stopMutation.isPending || restartMutation.isPending;
|
|
|
|
|
const uptime = process.state === 20 ? formatUptime(process.start, process.now) : 'Not running';
|
|
|
|
|
|
|
|
|
|
const handleStart = () => startMutation.mutate({ name: fullName });
|
|
|
|
|
const handleStop = () => stopMutation.mutate({ name: fullName });
|
|
|
|
|
const handleRestart = () => restartMutation.mutate(fullName);
|
|
|
|
|
|
2025-11-23 19:14:04 +01:00
|
|
|
const handleCardClick = () => {
|
|
|
|
|
if (onSelectionChange) {
|
|
|
|
|
onSelectionChange(fullName, !isSelected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-23 18:23:51 +01:00
|
|
|
return (
|
2025-11-23 19:14:04 +01:00
|
|
|
<Card
|
|
|
|
|
className={cn(
|
|
|
|
|
'transition-all hover:shadow-lg animate-fade-in',
|
|
|
|
|
onSelectionChange && 'cursor-pointer',
|
|
|
|
|
isSelected && 'ring-2 ring-primary ring-offset-2'
|
|
|
|
|
)}
|
|
|
|
|
onClick={onSelectionChange ? handleCardClick : undefined}
|
|
|
|
|
>
|
2025-11-23 18:23:51 +01:00
|
|
|
<CardHeader className="pb-3">
|
|
|
|
|
<div className="flex items-start justify-between">
|
2025-11-23 19:14:04 +01:00
|
|
|
<div className="flex items-center gap-3 flex-1">
|
|
|
|
|
{onSelectionChange && (
|
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
checked={isSelected}
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
onSelectionChange(fullName, e.target.checked);
|
|
|
|
|
}}
|
|
|
|
|
className="h-4 w-4 rounded border-input text-primary focus:ring-2 focus:ring-primary focus:ring-offset-2"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
<CardTitle className="text-lg">{process.name}</CardTitle>
|
|
|
|
|
<p className="text-sm text-muted-foreground mt-1">{process.group}</p>
|
|
|
|
|
</div>
|
2025-11-23 18:23:51 +01:00
|
|
|
</div>
|
|
|
|
|
<Badge
|
|
|
|
|
className={cn(
|
|
|
|
|
'ml-2',
|
2025-11-23 18:43:55 +01:00
|
|
|
getProcessStateClass(process.state as ProcessStateCode),
|
2025-11-23 18:23:51 +01:00
|
|
|
'border px-3 py-1 font-mono text-xs'
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{process.statename}
|
|
|
|
|
</Badge>
|
|
|
|
|
</div>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
|
|
<CardContent className="space-y-4">
|
|
|
|
|
{/* Metrics */}
|
|
|
|
|
<div className="grid grid-cols-2 gap-3 text-sm">
|
|
|
|
|
<div>
|
|
|
|
|
<span className="text-muted-foreground">PID:</span>
|
|
|
|
|
<span className="ml-2 font-mono">{process.pid || 'N/A'}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<span className="text-muted-foreground">Uptime:</span>
|
|
|
|
|
<span className="ml-2 font-mono">{uptime}</span>
|
|
|
|
|
</div>
|
|
|
|
|
{process.exitstatus !== 0 && (
|
|
|
|
|
<div className="col-span-2">
|
|
|
|
|
<span className="text-muted-foreground">Exit Status:</span>
|
|
|
|
|
<span className="ml-2 font-mono text-destructive">{process.exitstatus}</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Error Description */}
|
|
|
|
|
{process.spawnerr && (
|
|
|
|
|
<div className="rounded-md bg-destructive/10 p-2 text-xs text-destructive border border-destructive/20">
|
|
|
|
|
{process.spawnerr}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Actions */}
|
2025-11-23 19:14:04 +01:00
|
|
|
<div className="flex gap-2" onClick={(e) => e.stopPropagation()}>
|
2025-11-23 18:23:51 +01:00
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="success"
|
|
|
|
|
onClick={handleStart}
|
2025-11-23 18:43:55 +01:00
|
|
|
disabled={!canStartProcess(process.state as ProcessStateCode) || isLoading}
|
2025-11-23 18:23:51 +01:00
|
|
|
className="flex-1"
|
|
|
|
|
>
|
|
|
|
|
<Play className="h-4 w-4" />
|
|
|
|
|
Start
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="warning"
|
|
|
|
|
onClick={handleStop}
|
2025-11-23 18:43:55 +01:00
|
|
|
disabled={!canStopProcess(process.state as ProcessStateCode) || isLoading}
|
2025-11-23 18:23:51 +01:00
|
|
|
className="flex-1"
|
|
|
|
|
>
|
|
|
|
|
<Square className="h-4 w-4" />
|
|
|
|
|
Stop
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={handleRestart}
|
|
|
|
|
disabled={process.state === 0 || isLoading}
|
|
|
|
|
>
|
|
|
|
|
<RotateCw className={cn('h-4 w-4', isLoading && 'animate-spin-slow')} />
|
|
|
|
|
</Button>
|
2025-11-23 19:41:21 +01:00
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => setShowSignalModal(true)}
|
|
|
|
|
disabled={process.state === 0}
|
|
|
|
|
title="Send Signal"
|
|
|
|
|
>
|
|
|
|
|
<Zap className="h-4 w-4" />
|
|
|
|
|
</Button>
|
2025-11-23 19:46:47 +01:00
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => setShowStdinModal(true)}
|
|
|
|
|
disabled={process.state !== 20}
|
|
|
|
|
title="Send Stdin"
|
|
|
|
|
>
|
|
|
|
|
<Terminal className="h-4 w-4" />
|
|
|
|
|
</Button>
|
2025-11-23 18:23:51 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Description */}
|
|
|
|
|
{process.description && process.description !== 'pid ' + process.pid && (
|
|
|
|
|
<p className="text-xs text-muted-foreground italic">
|
|
|
|
|
{process.description}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</CardContent>
|
2025-11-23 19:41:21 +01:00
|
|
|
|
|
|
|
|
{/* Signal Modal */}
|
|
|
|
|
{showSignalModal && (
|
|
|
|
|
<SignalSender processName={fullName} onClose={() => setShowSignalModal(false)} />
|
|
|
|
|
)}
|
2025-11-23 19:46:47 +01:00
|
|
|
|
|
|
|
|
{/* Stdin Modal */}
|
|
|
|
|
{showStdinModal && (
|
|
|
|
|
<StdinInput processName={fullName} onClose={() => setShowStdinModal(false)} />
|
|
|
|
|
)}
|
2025-11-23 18:23:51 +01:00
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|