"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"; import { useTranslation } from "react-i18next"; type SetPasswordProps = { show: boolean; onSave: (password: string) => void; onCancel: () => void; username?: string; }; export default function SetPasswordDialog({ show, onSave, onCancel, username, }: SetPasswordProps) { const { t } = useTranslation(["views/settings"]); 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 t("users.dialog.form.password.strength.weak"); if (passwordStrength === 2) return t("users.dialog.form.password.strength.medium"); if (passwordStrength === 3) return t("users.dialog.form.password.strength.strong"); return t("users.dialog.form.password.strength.veryStrong"); }; 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 ? t("users.dialog.passwordSetting.updatePassword", { username, ns: "views/settings", }) : t("users.dialog.passwordSetting.setPassword")} {t("users.dialog.passwordSetting.desc")}
{ setPassword(event.target.value); setError(null); }} placeholder={t("users.dialog.form.newPassword.placeholder")} autoFocus /> {/* Password strength indicator */} {password && (

{t("users.dialog.form.password.strength.title")} {getStrengthLabel()}

)}
{ setConfirmPassword(event.target.value); setError(null); }} placeholder={t( "users.dialog.form.newPassword.confirm.placeholder", )} /> {/* Password match indicator */} {password && confirmPassword && (
{password === confirmPassword ? ( <> {t("users.dialog.form.password.match")} ) : ( <> {t("users.dialog.form.password.notMatch")} )}
)}
{error && (
{error}
)}
); }