import React, { useState, useEffect } from "react"; import { isAxiosError } from "axios"; import { Tabs, Loader, Alert, Stack, Text, Button, Accordion, } from "@mantine/core"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import auditService, { AuditSystemStatus as AuditStatus, } from "@app/services/auditService"; import AuditSystemStatus from "@app/components/shared/config/configSections/audit/AuditSystemStatus"; import AuditStatsCards from "@app/components/shared/config/configSections/audit/AuditStatsCards"; import AuditChartsSection from "@app/components/shared/config/configSections/audit/AuditChartsSection"; import AuditEventsTable from "@app/components/shared/config/configSections/audit/AuditEventsTable"; import AuditExportSection from "@app/components/shared/config/configSections/audit/AuditExportSection"; import AuditClearDataSection from "@app/components/shared/config/configSections/audit/AuditClearDataSection"; import { useLoginRequired } from "@app/hooks/useLoginRequired"; import LoginRequiredBanner from "@app/components/shared/config/LoginRequiredBanner"; import { useAppConfig } from "@app/contexts/AppConfigContext"; import EnterpriseRequiredBanner from "@app/components/shared/config/EnterpriseRequiredBanner"; import LocalIcon from "@app/components/shared/LocalIcon"; const AdminAuditSection: React.FC = () => { const { t } = useTranslation(); const navigate = useNavigate(); const { loginEnabled } = useLoginRequired(); const { config } = useAppConfig(); const licenseType = config?.license ?? "NORMAL"; const hasEnterpriseLicense = licenseType === "ENTERPRISE"; const showDemoData = !loginEnabled || !hasEnterpriseLicense; const [systemStatus, setSystemStatus] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [timePeriod, setTimePeriod] = useState<"day" | "week" | "month">( "week", ); useEffect(() => { const fetchSystemStatus = async () => { try { setLoading(true); setError(null); const status = await auditService.getSystemStatus(); setSystemStatus(status); } catch (err: unknown) { // Check if this is a permission/license error (403/404) const status = isAxiosError(err) ? err.response?.status : undefined; if (status === 403 || status === 404) { setError("enterprise-license-required"); } else { setError( err instanceof Error ? err.message : "Failed to load audit system status", ); } } finally { setLoading(false); } }; if (!showDemoData) { fetchSystemStatus(); } else { // Provide example audit system status when running in demo mode setError(null); setSystemStatus({ enabled: true, level: "INFO", retentionDays: 90, totalEvents: 1234, pdfMetadataEnabled: true, captureFileHash: true, capturePdfAuthor: true, captureOperationResults: false, }); setLoading(false); } }, [loginEnabled, showDemoData]); // Override loading state when showing demo data const actualLoading = showDemoData ? false : loading; if (actualLoading) { return (
); } if (error) { if (error === "enterprise-license-required") { return ( {t( "audit.enterpriseRequiredMessage", "The audit logging system is an enterprise feature. Please upgrade to an enterprise license to access audit logs and analytics.", )} ); } return ( {error} ); } if (!systemStatus) { return ( {t( "audit.notAvailableMessage", "The audit system is not configured or not available.", )} ); } const isEnabled = loginEnabled && hasEnterpriseLicense; return ( {/* Info banner about audit settings */} {isEnabled && ( } title={t("audit.configureAudit", "Configure Audit Logging")} color="blue" variant="light" > {t( "audit.configureAuditMessage", "Adjust audit logging level, retention period, and other settings in the Security & Authentication section.", )} )} {systemStatus?.enabled ? ( {t("audit.tabs.dashboard", "Dashboard")} {t("audit.tabs.events", "Audit Events")} {t("audit.tabs.export", "Export")} {t("audit.tabs.clearData", "Clear Data")} {/* Stats Cards - Always Visible */} {/* Charts in Accordion - Collapsible */} {t("audit.charts.overTime", "Events Over Time")} ) : ( {t( "audit.disabledMessage", "Enable audit logging in your application configuration to track system events.", )} )} ); }; export default AdminAuditSection;