"use client"; import { Button } from "../ui/button"; import { Input } from "../ui/input"; import { useState, useEffect } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../ui/dialog"; import { Label } from "../ui/label"; import { LuCheck, LuX } from "react-icons/lu"; type SetPasswordProps = { show: boolean; onSave: (password: string) => void; onCancel: () => void; username?: string; }; export default function SetPasswordDialog({ show, onSave, onCancel, username, }: SetPasswordProps) { const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [passwordStrength, setPasswordStrength] = useState(0); const [error, setError] = useState(null); // Reset state when dialog opens/closes useEffect(() => { if (show) { setPassword(""); setConfirmPassword(""); setError(null); } }, [show]); // Simple password strength calculation useEffect(() => { if (!password) { setPasswordStrength(0); return; } let strength = 0; // Length check if (password.length >= 8) strength += 1; // Contains number if (/\d/.test(password)) strength += 1; // Contains special char if (/[!@#$%^&*(),.?":{}|<>]/.test(password)) strength += 1; // Contains uppercase if (/[A-Z]/.test(password)) strength += 1; setPasswordStrength(strength); }, [password]); const handleSave = () => { if (!password) { setError("Password cannot be empty"); return; } if (password !== confirmPassword) { setError("Passwords do not match"); return; } onSave(password); }; const getStrengthLabel = () => { if (!password) return ""; if (passwordStrength <= 1) return "Weak"; if (passwordStrength === 2) return "Medium"; if (passwordStrength === 3) return "Strong"; return "Very Strong"; }; const getStrengthColor = () => { if (!password) return "bg-gray-200"; if (passwordStrength <= 1) return "bg-red-500"; if (passwordStrength === 2) return "bg-yellow-500"; if (passwordStrength === 3) return "bg-green-500"; return "bg-green-600"; }; return ( {username ? `Update Password for ${username}` : "Set Password"} Create a strong password to secure this account.
{ setPassword(event.target.value); setError(null); }} placeholder="Enter new password" autoFocus /> {/* Password strength indicator */} {password && (

Password strength:{" "} {getStrengthLabel()}

)}
{ setConfirmPassword(event.target.value); setError(null); }} placeholder="Confirm new password" /> {/* Password match indicator */} {password && confirmPassword && (
{password === confirmPassword ? ( <> Passwords match ) : ( <> Passwords don't match )}
)}
{error && (
{error}
)}
); }