Spaces:
Sleeping
Sleeping
File size: 4,283 Bytes
7540aea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | /**
* CronManagerPanel — displays and manages scheduled cron jobs.
* Uses real JSON API endpoint /api/cron/:sessionId
*/
import { useState, useEffect, useCallback } from "react";
import { Timer, RefreshCw, Clock } from "lucide-react";
interface CronJob {
id: string;
schedule: string;
command: string;
description: string;
status: "active" | "paused" | "deleted";
createdAt: number;
lastRun?: number;
runCount: number;
output: string;
}
interface CronManagerPanelProps {
sessionId: number;
onSlashCommand: (command: string, args?: string) => Promise<string>;
}
export function CronManagerPanel({ sessionId }: CronManagerPanelProps) {
const [jobs, setJobs] = useState<CronJob[]>([]);
const [loading, setLoading] = useState(false);
const [expandedJob, setExpandedJob] = useState<string | null>(null);
const refreshJobs = useCallback(async () => {
setLoading(true);
try {
const res = await fetch(`/api/cron/${sessionId}`);
if (res.ok) {
const data = await res.json();
if (Array.isArray(data)) setJobs(data);
}
} catch {
// ignore fetch errors
} finally {
setLoading(false);
}
}, [sessionId]);
useEffect(() => {
refreshJobs();
// Auto-refresh every 5 seconds
const interval = setInterval(refreshJobs, 5000);
return () => clearInterval(interval);
}, [refreshJobs]);
const statusBadge = (status: string) => {
const colors: Record<string, string> = {
active: "bg-green-500/10 text-green-400 border-green-500/20",
paused: "bg-yellow-500/10 text-yellow-400 border-yellow-500/20",
deleted: "bg-red-500/10 text-red-400 border-red-500/20",
};
return (
<span className={`px-2 py-0.5 rounded-full text-xs border ${colors[status] || "bg-muted text-muted-foreground"}`}>
{status}
</span>
);
};
return (
<div className="flex flex-col h-full">
<div className="flex items-center justify-between px-4 py-3 border-b border-border">
<h3 className="text-sm font-semibold flex items-center gap-2">
<Timer className="w-4 h-4" />
Cron Jobs ({jobs.length})
</h3>
<button
onClick={refreshJobs}
disabled={loading}
className="p-1.5 rounded hover:bg-accent transition-colors"
>
<RefreshCw className={`w-4 h-4 ${loading ? "animate-spin" : ""}`} />
</button>
</div>
<div className="flex-1 overflow-y-auto">
{jobs.length === 0 ? (
<div className="flex flex-col items-center justify-center h-full text-muted-foreground text-sm gap-2 p-4">
<Clock className="w-8 h-8 opacity-40" />
<p>No scheduled cron jobs</p>
<p className="text-xs opacity-60">Jobs created via CronCreate tool will appear here</p>
</div>
) : (
<div className="divide-y divide-border">
{jobs.map((job) => (
<div key={job.id} className="p-3">
<button
onClick={() => setExpandedJob(expandedJob === job.id ? null : job.id)}
className="w-full flex items-start justify-between gap-2 text-left"
>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<p className="text-sm font-medium truncate">{job.description || job.command}</p>
{statusBadge(job.status)}
</div>
<p className="text-xs text-muted-foreground mt-1 font-mono">
{job.schedule}
</p>
<p className="text-xs text-muted-foreground">
Runs: {job.runCount} · {job.lastRun ? `Last: ${new Date(job.lastRun).toLocaleTimeString()}` : "Not yet run"}
</p>
</div>
</button>
{expandedJob === job.id && (
<pre className="mt-2 p-2 rounded bg-muted text-xs font-mono whitespace-pre-wrap max-h-40 overflow-y-auto">
{job.output || "(no output yet)"}
</pre>
)}
</div>
))}
</div>
)}
</div>
</div>
);
}
|