"use client"; import { useState } from "react"; import { CheckOrgUserAccessResponse } from "@server/routers/org"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Progress } from "@/components/ui/progress"; import { CheckCircle2, XCircle, Shield } from "lucide-react"; import Enable2FaDialog from "./Enable2FaDialog"; import { useTranslations } from "next-intl"; import { useUserContext } from "@app/hooks/useUserContext"; import { useRouter } from "next/navigation"; type OrgPolicyResultProps = { orgId: string; userId: string; accessRes: CheckOrgUserAccessResponse; }; type PolicyItem = { id: string; name: string; description: string; compliant: boolean; action?: () => void; actionText?: string; }; export default function OrgPolicyResult({ orgId, userId, accessRes }: OrgPolicyResultProps) { const [show2FaDialog, setShow2FaDialog] = useState(false); const t = useTranslations(); const { user } = useUserContext(); const router = useRouter(); // Determine if user is compliant with 2FA policy const isTwoFactorCompliant = user?.twoFactorEnabled || false; const policyKeys = Object.keys(accessRes.policies || {}); const policies: PolicyItem[] = []; // Only add 2FA policy if the organization has it enforced if (policyKeys.includes("requiredTwoFactor")) { policies.push({ id: "two-factor", name: t("twoFactorAuthentication"), description: t("twoFactorDescription"), compliant: isTwoFactorCompliant, action: !isTwoFactorCompliant ? () => setShow2FaDialog(true) : undefined, actionText: !isTwoFactorCompliant ? t("enableTwoFactor") : undefined }); // policies.push({ // id: "reauth-required", // name: "Re-authentication", // description: // "It's been 30 days since you last verified your identity. Please log out and log back in to continue.", // compliant: false, // action: () => {}, // actionText: "Log Out" // }); // // policies.push({ // id: "password-rotation", // name: "Password Rotation", // description: // "It's been 30 days since you last changed your password. Please update your password to continue.", // compliant: false, // action: () => {}, // actionText: "Change Password" // }); } const nonCompliantPolicies = policies.filter((policy) => !policy.compliant); const allCompliant = policies.length === 0 || nonCompliantPolicies.length === 0; // Calculate progress const completedPolicies = policies.filter( (policy) => policy.compliant ).length; const totalPolicies = policies.length; const progressPercentage = totalPolicies > 0 ? (completedPolicies / totalPolicies) * 100 : 100; // If no policies are enforced, show a simple success message if (policies.length === 0) { return (
{t("noSecurityRequirements")}
{policy.description}
{policy.action && policy.actionText && ({t("allRequirementsMet")}
{t("youCanNowAccessOrganization")}