import React, { useState, useEffect } from 'react'; import { Card, Row, Col, Statistic, Typography, Space, Alert, Spin } from 'antd'; import { AppstoreOutlined, KeyOutlined, UserOutlined, CheckCircleOutlined, ExclamationCircleOutlined, } from '@ant-design/icons'; import { apiService } from '../services/apiService'; const { Title } = Typography; interface DashboardStats { totalApplications: number; totalTokens: number; healthStatus: 'healthy' | 'unhealthy'; readinessStatus: 'ready' | 'not-ready'; } const Dashboard: React.FC = () => { const [stats, setStats] = useState({ totalApplications: 0, totalTokens: 0, healthStatus: 'unhealthy', readinessStatus: 'not-ready', }); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); useEffect(() => { loadDashboardData(); }, []); const loadDashboardData = async () => { try { setLoading(true); setError(''); // Load health status const [healthResponse, readinessResponse] = await Promise.allSettled([ apiService.healthCheck(), apiService.readinessCheck(), ]); const healthStatus = healthResponse.status === 'fulfilled' ? 'healthy' : 'unhealthy'; const readinessStatus = readinessResponse.status === 'fulfilled' ? 'ready' : 'not-ready'; // Load applications count let totalApplications = 0; let totalTokens = 0; try { const appsResponse = await apiService.getApplications(100, 0); totalApplications = appsResponse.count; // Count tokens across all applications for (const app of appsResponse.data) { try { const tokensResponse = await apiService.getTokensForApplication(app.app_id, 100, 0); totalTokens += tokensResponse.count; } catch (tokenError) { console.warn(`Failed to load tokens for app ${app.app_id}:`, tokenError); } } } catch (appsError) { console.warn('Failed to load applications:', appsError); } setStats({ totalApplications, totalTokens, healthStatus, readinessStatus, }); } catch (err) { console.error('Dashboard error:', err); setError('Failed to load dashboard data. Please check your connection.'); } finally { setLoading(false); } }; if (loading) { return (
Loading dashboard...
); } return (
Dashboard

Welcome to the Key Management System dashboard. Monitor your applications, tokens, and system health.

{error && ( setError('')} /> )} {/* System Status */} ) : ( ) } valueStyle={{ color: stats.healthStatus === 'healthy' ? '#52c41a' : '#ff4d4f', }} /> ) : ( ) } valueStyle={{ color: stats.readinessStatus === 'ready' ? '#52c41a' : '#ff4d4f', }} /> {/* Statistics */} } valueStyle={{ color: '#1890ff' }} /> } valueStyle={{ color: '#52c41a' }} /> } valueStyle={{ color: '#722ed1' }} /> {/* Quick Actions */} window.location.pathname = '/applications'} style={{ textAlign: 'center', cursor: 'pointer' }} >
Manage Applications
window.location.pathname = '/tokens'} style={{ textAlign: 'center', cursor: 'pointer' }} >
Manage Tokens
window.location.pathname = '/audit'} style={{ textAlign: 'center', cursor: 'pointer' }} >
View Audit Log
); }; export default Dashboard;