Spaces:
Paused
Paused
File size: 13,267 Bytes
5a81b95 | 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | /**
* ApprovalQueueWidget - Human-in-the-Loop Approval Interface
*
* Displays pending requests requiring human approval.
* Allows users to approve/reject operations in real-time.
*/
import React, { useState, useEffect } from 'react';
import {
Shield, Check, X, Clock, AlertTriangle, RefreshCw,
ChevronDown, ChevronUp, Info
} from 'lucide-react';
import { cn } from '@/lib/utils';
interface ApprovalRequest {
id: string;
operation: string;
description: string;
riskLevel: 'low' | 'medium' | 'high' | 'critical';
requestedBy: string;
requestedAt: number;
expiresAt: number;
status: 'pending' | 'approved' | 'rejected' | 'expired';
metadata: Record<string, any>;
}
const API_BASE = 'http://localhost:3001/api';
export default function ApprovalQueueWidget() {
const [requests, setRequests] = useState<ApprovalRequest[]>([]);
const [loading, setLoading] = useState(true);
const [expandedId, setExpandedId] = useState<string | null>(null);
const [processing, setProcessing] = useState<string | null>(null);
const fetchPending = async () => {
try {
const res = await fetch(`${API_BASE}/approvals/pending`);
const data = await res.json();
if (data.success) {
setRequests(data.requests);
}
} catch (error) {
console.error('Failed to fetch approvals:', error);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchPending();
const interval = setInterval(fetchPending, 5000); // Poll every 5s
return () => clearInterval(interval);
}, []);
const handleApprove = async (requestId: string) => {
setProcessing(requestId);
try {
const res = await fetch(`${API_BASE}/approvals/${requestId}/approve`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ approvedBy: 'Dashboard User' }),
});
const data = await res.json();
if (data.success) {
await fetchPending(); // Refresh list
} else {
alert(`Approval failed: ${data.error}`);
}
} catch (error: any) {
alert(`Error: ${error.message}`);
} finally {
setProcessing(null);
}
};
const handleReject = async (requestId: string) => {
const reason = prompt('Enter rejection reason:');
if (!reason) return;
setProcessing(requestId);
try {
const res = await fetch(`${API_BASE}/approvals/${requestId}/reject`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
rejectedBy: 'Dashboard User',
reason,
}),
});
const data = await res.json();
if (data.success) {
await fetchPending();
} else {
alert(`Rejection failed: ${data.error}`);
}
} catch (error: any) {
alert(`Error: ${error.message}`);
} finally {
setProcessing(null);
}
};
const getRiskColor = (level: string) => {
switch (level) {
case 'critical': return 'text-red-400 bg-red-400/10 border-red-400/30';
case 'high': return 'text-orange-400 bg-orange-400/10 border-orange-400/30';
case 'medium': return 'text-yellow-400 bg-yellow-400/10 border-yellow-400/30';
case 'low': return 'text-green-400 bg-green-400/10 border-green-400/30';
default: return 'text-gray-400 bg-gray-400/10 border-gray-400/30';
}
};
const getRiskIcon = (level: string) => {
switch (level) {
case 'critical': return <AlertTriangle className="w-4 h-4 text-red-400" />;
case 'high': return <AlertTriangle className="w-4 h-4 text-orange-400" />;
default: return <Shield className="w-4 h-4" />;
}
};
const formatTimeRemaining = (expiresAt: number) => {
const remaining = expiresAt - Date.now();
if (remaining < 0) return 'Expired';
const minutes = Math.floor(remaining / 60000);
const seconds = Math.floor((remaining % 60000) / 1000);
return `${minutes}m ${seconds}s`;
};
return (
<div className="h-full flex flex-col bg-card/50 backdrop-blur-sm border border-border/50 rounded-lg overflow-hidden">
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-border/30 bg-gradient-to-r from-orange-500/10 to-transparent">
<div className="flex items-center gap-2">
<Shield className="w-5 h-5 text-orange-400" />
<span className="font-display text-sm uppercase tracking-wider text-orange-400">
Approval Queue
</span>
</div>
<div className="flex items-center gap-2">
{requests.length > 0 && (
<span className="px-2 py-1 bg-orange-400/20 text-orange-400 rounded text-xs font-medium">
{requests.length} pending
</span>
)}
<button
onClick={fetchPending}
className="p-1.5 hover:bg-secondary/50 rounded transition-colors"
disabled={loading}
>
<RefreshCw className={cn("w-4 h-4 text-orange-400", loading && "animate-spin")} />
</button>
</div>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-4 space-y-3">
{loading ? (
<div className="flex items-center justify-center h-32">
<RefreshCw className="w-6 h-6 text-orange-400 animate-spin" />
</div>
) : requests.length === 0 ? (
<div className="text-center py-8">
<Check className="w-8 h-8 text-green-400 mx-auto mb-2" />
<p className="text-sm text-muted-foreground">No pending approvals</p>
</div>
) : (
requests.map(request => {
const isExpanded = expandedId === request.id;
const timeRemaining = formatTimeRemaining(request.expiresAt);
const isExpiringSoon = request.expiresAt - Date.now() < 60000; // < 1 min
return (
<div
key={request.id}
className={cn(
"border rounded-lg overflow-hidden transition-all",
getRiskColor(request.riskLevel)
)}
>
{/* Header */}
<div className="p-3">
<div className="flex items-start justify-between gap-3">
<div className="flex-1">
<div className="flex items-center gap-2 mb-1">
{getRiskIcon(request.riskLevel)}
<span className="text-xs font-medium uppercase tracking-wide opacity-70">
{request.riskLevel} risk
</span>
<span className="text-xs opacity-50">•</span>
<span className="text-xs font-mono opacity-70">{request.operation}</span>
</div>
<p className="text-sm mb-2">{request.description}</p>
<div className="flex items-center gap-3 text-xs opacity-60">
<div className="flex items-center gap-1">
<Clock className="w-3 h-3" />
<span className={cn(isExpiringSoon && "text-red-400 font-medium")}>
{timeRemaining}
</span>
</div>
<span>•</span>
<span>{request.requestedBy}</span>
</div>
</div>
<button
onClick={() => setExpandedId(isExpanded ? null : request.id)}
className="p-1 hover:bg-black/20 rounded transition-colors"
>
{isExpanded ? (
<ChevronUp className="w-4 h-4" />
) : (
<ChevronDown className="w-4 h-4" />
)}
</button>
</div>
{/* Expanded details */}
{isExpanded && (
<div className="mt-3 pt-3 border-t border-current/20">
<div className="space-y-2 text-xs">
{Object.entries(request.metadata).map(([key, value]) => (
<div key={key} className="flex items-start gap-2">
<Info className="w-3 h-3 mt-0.5 opacity-50" />
<span className="font-medium opacity-70">{key}:</span>
<span className="opacity-90">{String(value)}</span>
</div>
))}
</div>
</div>
)}
{/* Action buttons */}
<div className="flex gap-2 mt-3">
<button
onClick={() => handleApprove(request.id)}
disabled={processing === request.id}
className={cn(
"flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded text-xs font-medium transition-all",
"bg-green-500/20 text-green-400 hover:bg-green-500/30",
processing === request.id && "opacity-50 cursor-not-allowed"
)}
>
<Check className="w-3 h-3" />
Approve
</button>
<button
onClick={() => handleReject(request.id)}
disabled={processing === request.id}
className={cn(
"flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded text-xs font-medium transition-all",
"bg-red-500/20 text-red-400 hover:bg-red-500/30",
processing === request.id && "opacity-50 cursor-not-allowed"
)}
>
<X className="w-3 h-3" />
Reject
</button>
</div>
</div>
</div>
);
})
)}
</div>
{/* Footer */}
<div className="p-3 border-t border-border/30 bg-secondary/20">
<p className="text-xs text-muted-foreground text-center">
🔐 Human-in-the-Loop: Ingen handlinger udført uden din godkendelse
</p>
</div>
</div>
);
}
|