All files / src/app/admin page.tsx

60.97% Statements 25/41
75% Branches 30/40
87.5% Functions 14/16
60% Lines 24/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461                                                          2x                                                       2x                                                                                                                                           55x 55x 55x   55x 29x   25x   25x     25x   25x 23x 3x   6x     3x 20x 1x         26x     55x                                                                                                                                                 104x                                                                                 60x                                                                                                                 130x                                                     104x                                           104x                                                                                                                                                                    
"use client";
 
import { fetchApi } from "@/lib/api";
import { useEffect, useState } from "react";
import {
  Activity,
  Building2,
  Download,
  FileKey2,
  LayoutDashboard,
  Loader2,
  ScrollText,
  ShieldCheck,
  Users,
  Key,
  Shield,
  RefreshCw,
  Terminal,
  ClipboardList,
  Gauge,
} from "lucide-react";
import Link from "next/link";
 
interface Summary {
  totalTenants: number;
  totalParticipants: number;
  byRole: Record<string, number>;
}
 
const ACTIVITY_LOG = [
  {
    icon: Key,
    color: "text-[var(--accent)]",
    title: "Security keys rotated",
    sub: "Admin • 12m ago",
  },
  {
    icon: Shield,
    color: "text-[var(--success-text)]",
    title: "New node authorized",
    sub: "Frankfurt-Cluster-04 • 45m ago",
  },
  {
    icon: ShieldCheck,
    color: "text-[var(--danger-text)]",
    title: "DDoS attempt mitigated",
    sub: "Auto-Defense System • 1h ago",
  },
  {
    icon: RefreshCw,
    color: "text-[var(--text-secondary)]",
    title: "Scheduled Backup Complete",
    sub: "System • 3h ago",
    dim: true,
  },
];
 
const QUICK_OPS = [
  { icon: RefreshCw, label: "Deploy Patch", href: "/admin/components" },
  { icon: Terminal, label: "Flush Cache", href: "/admin/components" },
  { icon: ClipboardList, label: "Audit Logs", href: "/admin/audit" },
  { icon: Gauge, label: "Test Latency", href: "/admin/components" },
];
 
function generateReport(summary: Summary | null, policyCount: number | null) {
  const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
  const lines = [
    "# EHDS Health Dataspace — Operator Report",
    "",
    `**Generated:** ${new Date().toLocaleString("en-GB", {
      dateStyle: "full",
      timeStyle: "short",
    })}`,
    `**Protocol:** DSP 2025-1 · EHDS Regulation (EU) 2025/327`,
    "",
    "## Dataspace Summary",
    "",
    `| Metric | Value |`,
    `| --- | --- |`,
    `| Total Tenants | ${summary?.totalTenants ?? "—"} |`,
    `| Total Participants | ${summary?.totalParticipants ?? "—"} |`,
    `| ODRL Policies | ${policyCount ?? "—"} |`,
    "",
    "## Participants by EHDS Role",
    "",
  ];
  if (summary?.byRole && Object.keys(summary.byRole).length > 0) {
    lines.push("| Role | Count |", "| --- | --- |");
    for (const [role, count] of Object.entries(summary.byRole)) {
      lines.push(`| ${role} | ${count} |`);
    }
  } else {
    lines.push("_No role data available._");
  }
  lines.push(
    "",
    "## European Data Flow",
    "",
    "| Country | Organisation | EHDS Role |",
    "| --- | --- | --- |",
    "| DE | AlphaKlinik Berlin | Data Holder |",
    "| DE | PharmaCo Research AG | Data User |",
    "| DE | MedReg DE | Health Data Access Body |",
    "| NL | Limburg Medical Centre | Data Holder |",
    "| FR | Institut de Recherche Santé | Health Data Access Body |",
    "",
    "## Compliance Status",
    "",
    "- DSP 2025-1: Conformant (contract negotiation + transfer)",
    "- DCP v1.0: Conformant (VC attestation via IdentityHub)",
    "- EHDS Art. 33/50-51: Secondary use data permits implemented",
    "- GDPR Art. 15-22: Patient access rights via /patient portal",
    "",
    "---",
    "_Report generated by EHDS Health Dataspace Operator Console._",
  );
 
  const blob = new Blob([lines.join("\n")], { type: "text/markdown" });
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = `ehds-report-${ts}.md`;
  a.click();
  URL.revokeObjectURL(url);
}
 
export default function AdminDashboard() {
  const [summary, setSummary] = useState<Summary | null>(null);
  const [policyCount, setPolicyCount] = useState<number | null>(null);
  const [loading, setLoading] = useState(true);
 
  useEffect(() => {
    Promise.all([
      fetchApi("/api/admin/tenants")
        .then((r) => (r.ok ? r.json() : ({} as Record<string, unknown>)))
        .then((d: Record<string, unknown>) => {
          setSummary((d.summary as Summary) || null);
        }),
      fetchApi("/api/admin/policies")
        .then((r) => (r.ok ? r.json() : null))
        .then((d: Record<string, unknown> | null) => {
          if (!d) return;
          if (Array.isArray(d.participants)) {
            const total = (d.participants as { policies?: unknown[] }[]).reduce(
              (sum, p) =>
                sum + (Array.isArray(p.policies) ? p.policies.length : 0),
              0,
            );
            setPolicyCount(total);
          } else if (Array.isArray(d.policies)) {
            setPolicyCount(d.policies.length);
          }
        }),
    ])
      .catch(() => {})
      .finally(() => setLoading(false));
  }, []);
 
  return (
    <div className="min-h-screen bg-[var(--bg)]">
      <main className="px-8 py-10 max-w-7xl mx-auto space-y-8">
        {/* ── Page header ── */}
        <div className="flex flex-col md:flex-row md:items-end justify-between gap-4">
          <div>
            <h1 className="page-header">System Overview</h1>
            <p className="text-[var(--text-secondary)] text-lg mt-1">
              EHDS Health Dataspace — Operator Control
            </p>
          </div>
          <div className="flex items-center gap-3">
            <div className="flex items-center gap-2 px-4 py-2 bg-[var(--success)]/10 text-[var(--success-text)] rounded-full border border-[var(--success)]/20 text-sm font-bold tracking-tight">
              <span className="w-2 h-2 rounded-full bg-[var(--success)] animate-pulse" />
              SYSTEMS NOMINAL
            </div>
            <button
              onClick={() => generateReport(summary, policyCount)}
              className="btn-gradient flex items-center gap-2 text-sm"
            >
              <Download size={16} />
              Generate Report
            </button>
          </div>
        </div>
 
        {loading ? (
          <div className="flex items-center gap-2 text-[var(--text-secondary)]">
            <Loader2 size={16} className="animate-spin" />
            Loading dashboard…
          </div>
        ) : (
          <>
            {/* ── Bento stat grid ── */}
            <div className="grid grid-cols-1 md:grid-cols-4 gap-6">
              {[
                {
                  href: "/admin/tenants",
                  icon: Building2,
                  iconColor: "text-[var(--accent)]",
                  iconBg: "bg-[var(--accent)]/5",
                  label: "Tenants",
                  value: summary?.totalTenants ?? "—",
                  border: "border-l-[var(--accent)]",
                },
                {
                  href: "/admin/tenants",
                  icon: Users,
                  iconColor: "text-[var(--success-text)]",
                  iconBg: "bg-[var(--success)]/5",
                  label: "Participants",
                  value: summary?.totalParticipants ?? "—",
                  border: "border-l-[var(--success-text)]",
                },
                {
                  href: "/admin/policies",
                  icon: ShieldCheck,
                  iconColor: "text-[var(--layer5-text)]",
                  iconBg: "bg-[var(--layer5)]/5",
                  label: "Policies",
                  value: policyCount ?? "—",
                  border: "border-l-[var(--layer5-text)]",
                },
                {
                  href: "/admin/audit",
                  icon: ScrollText,
                  iconColor: "text-[var(--warning-text)]",
                  iconBg: "bg-[var(--warning)]/5",
                  label: "Audit Logs",
                  value: "→",
                  border: "border-l-[var(--warning-text)]",
                },
              ].map((c) => (
                <Link
                  key={c.label}
                  href={c.href}
                  className={`stat-card ${c.border} hover:shadow-md transition-shadow`}
                >
                  <div className="flex justify-between items-start mb-4">
                    <div className={`p-2 ${c.iconBg} rounded-lg`}>
                      <c.icon size={20} className={c.iconColor} />
                    </div>
                  </div>
                  <p className="text-sm font-medium text-[var(--text-secondary)]">
                    {c.label}
                  </p>
                  <p className="text-3xl font-black text-[var(--text-primary)] mt-1 tabular-nums">
                    {c.value}
                  </p>
                </Link>
              ))}
            </div>
 
            {/* ── Main content grid ── */}
            <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
              {/* EHDS Role breakdown + EU data flow */}
              <div className="lg:col-span-2 bg-[var(--surface)] rounded-xl p-8 relative overflow-hidden min-h-[320px]">
                <div className="flex justify-between items-start mb-6">
                  <div>
                    <h2 className="text-xl font-bold tracking-tight text-[var(--text-primary)]">
                      Participants by EHDS Role
                    </h2>
                    <p className="text-sm text-[var(--text-secondary)] mt-0.5">
                      Access control breakdown across the dataspace
                    </p>
                  </div>
                  <LayoutDashboard
                    size={32}
                    className="text-[var(--border)] shrink-0"
                  />
                </div>
                {summary?.byRole && Object.keys(summary.byRole).length > 0 ? (
                  <div className="grid grid-cols-2 md:grid-cols-3 gap-4">
                    {Object.entries(summary.byRole).map(([role, count]) => (
                      <div
                        key={role}
                        className="surface-card p-4 border border-[var(--border)]"
                      >
                        <p className="text-2xl font-black text-[var(--text-primary)] tabular-nums">
                          {count}
                        </p>
                        <p className="text-xs text-[var(--text-secondary)] mt-1 break-all">
                          {role}
                        </p>
                      </div>
                    ))}
                  </div>
                ) : (
                  <div className="flex items-center gap-3 text-[var(--text-secondary)]">
                    <FileKey2 size={18} />
                    <span className="text-sm">
                      No role data available — connect to live stack
                    </span>
                  </div>
                )}
                {/* EU data flow indicator strip */}
                <div className="mt-6 pt-4 border-t border-[var(--border)]">
                  <p className="section-label mb-3">European data flow</p>
                  <div className="flex items-center gap-2 flex-wrap">
                    {[
                      {
                        id: "DE",
                        label: "AlphaKlinik Berlin",
                        color: "var(--role-holder-text)",
                        bg: "var(--role-holder-bg)",
                      },
                      {
                        id: "NL",
                        label: "Limburg Medical",
                        color: "var(--role-holder-text)",
                        bg: "var(--role-holder-bg)",
                      },
                      {
                        id: "FR",
                        label: "Institut Santé",
                        color: "var(--role-hdab-text)",
                        bg: "var(--role-hdab-bg)",
                      },
                      {
                        id: "DE",
                        label: "MedReg DE",
                        color: "var(--role-hdab-text)",
                        bg: "var(--role-hdab-bg)",
                      },
                      {
                        id: "DE",
                        label: "PharmaCo AG",
                        color: "var(--role-user-text)",
                        bg: "var(--role-user-bg)",
                      },
                    ].map((node, i) => (
                      <span
                        key={i}
                        className="flex items-center gap-1.5 text-[10px] font-bold px-2.5 py-1 rounded-full border"
                        style={{
                          color: node.color,
                          background: node.bg,
                          borderColor: node.color,
                        }}
                      >
                        <span className="font-mono">{node.id}</span>
                        {node.label}
                      </span>
                    ))}
                    <span className="text-[10px] text-[var(--text-secondary)]">
                      ↔ DSP 2025-1
                    </span>
                  </div>
                </div>
              </div>
 
              {/* Quick Operations + Activity */}
              <div className="flex flex-col gap-6">
                {/* Quick ops */}
                <div className="bg-[var(--surface-2)] rounded-xl p-6">
                  <p className="section-label">Quick Operations</p>
                  <div className="grid grid-cols-2 gap-3">
                    {QUICK_OPS.map(({ icon: Icon, label, href }) => (
                      <Link
                        key={label}
                        href={href}
                        className="flex flex-col items-center justify-center p-4 bg-[var(--surface-card)] rounded-xl hover:bg-[var(--accent)] hover:text-white transition-all group shadow-sm"
                      >
                        <Icon
                          size={20}
                          className="text-[var(--accent)] group-hover:text-white mb-2"
                        />
                        <span className="text-xs font-bold text-center leading-tight">
                          {label}
                        </span>
                      </Link>
                    ))}
                  </div>
                </div>
 
                {/* Activity feed — Stitch activity-timeline pattern */}
                <div className="flex-1 surface-card p-6 border border-[var(--border)]">
                  <p className="section-label">Administrative Activity</p>
                  <div className="activity-timeline space-y-5 pl-7">
                    {ACTIVITY_LOG.map((item, i) => (
                      <div key={i} className="flex gap-3 items-start relative">
                        {/* Timeline node */}
                        <div className="absolute -left-7 top-1 w-8 h-8 rounded-full bg-[var(--surface)] border border-[var(--border)] flex items-center justify-center shrink-0 z-10">
                          <item.icon size={14} className={item.color} />
                        </div>
                        <div>
                          <p
                            className={`text-sm font-bold ${
                              item.dim
                                ? "text-gray-500 dark:text-gray-400"
                                : "text-[var(--text-primary)]"
                            }`}
                          >
                            {item.title}
                          </p>
                          <p
                            className={`text-xs ${
                              item.dim
                                ? "text-gray-500 dark:text-gray-400"
                                : "text-[var(--text-secondary)]"
                            }`}
                          >
                            {item.sub}
                          </p>
                        </div>
                      </div>
                    ))}
                  </div>
                </div>
              </div>
            </div>
 
            {/* ── Sub-page quick links ── */}
            <div className="grid md:grid-cols-2 gap-6">
              <div className="bg-[var(--surface)] rounded-xl p-6 border border-[var(--border)] flex items-center gap-6">
                <div className="w-14 h-14 rounded-2xl bg-[var(--success)]/10 flex items-center justify-center shrink-0">
                  <ShieldCheck
                    size={28}
                    className="text-[var(--success-text)]"
                  />
                </div>
                <div>
                  <h3 className="font-bold text-lg text-[var(--text-primary)]">
                    GDPR Compliance Check
                  </h3>
                  <p className="text-sm text-[var(--text-secondary)] mb-3">
                    Next automated audit scheduled for next month.
                  </p>
                  <Link
                    href="/compliance"
                    className="text-sm font-black text-[var(--success-text)] hover:underline uppercase tracking-wider"
                  >
                    RUN PRE-AUDIT NOW
                  </Link>
                </div>
              </div>
              <div className="bg-[var(--surface)] rounded-xl p-6 border border-[var(--border)] flex items-center gap-6">
                <div className="w-14 h-14 rounded-2xl bg-[var(--accent)]/10 flex items-center justify-center shrink-0">
                  <Activity size={28} className="text-[var(--accent)]" />
                </div>
                <div>
                  <h3 className="font-bold text-lg text-[var(--text-primary)]">
                    EDC Components
                  </h3>
                  <p className="text-sm text-[var(--text-secondary)] mb-3">
                    Monitor health, CPU &amp; memory per service.
                  </p>
                  <Link
                    href="/admin/components"
                    className="text-sm font-black text-[var(--accent)] hover:underline uppercase tracking-wider"
                  >
                    VIEW COMPONENTS
                  </Link>
                </div>
              </div>
            </div>
          </>
        )}
      </main>
    </div>
  );
}