"use client" import { useActionState, useEffect, useMemo, useRef, useState } from "react" import { useFormStatus } from "react-dom" import { useTranslations } from "next-intl" import { Eye, EyeOff, KeyRound, Loader2 } from "lucide-react" import { toast } from "sonner" import { Button } from "@/shared/components/ui/button" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/shared/components/ui/card" import { Input } from "@/shared/components/ui/input" import { Label } from "@/shared/components/ui/label" import { Progress } from "@/shared/components/ui/progress" import { changePasswordAction } from "@/modules/settings/actions-password" import { getPasswordStrength, PASSWORD_REQUIREMENT_HINTS, type PasswordStrength, } from "@/shared/lib/password-policy" import type { ActionState } from "@/shared/types/action-state" const STRENGTH_META: Record = { weak: { value: 33, labelKey: "security.changePassword.strengthWeak", barClassName: "h-2", indicatorClassName: "bg-red-500" }, medium: { value: 66, labelKey: "security.changePassword.strengthMedium", barClassName: "h-2", indicatorClassName: "bg-yellow-500" }, strong: { value: 100, labelKey: "security.changePassword.strengthStrong", barClassName: "h-2", indicatorClassName: "bg-green-500" }, } function SubmitButton() { const { pending } = useFormStatus() const t = useTranslations("settings.security.changePassword") return ( ) } export function PasswordChangeForm() { const t = useTranslations("settings.security.changePassword") const [state, formAction] = useActionState, FormData>( changePasswordAction, { success: false, data: null } ) const [newPassword, setNewPassword] = useState("") const [showCurrent, setShowCurrent] = useState(false) const [showNew, setShowNew] = useState(false) const [showConfirm, setShowConfirm] = useState(false) const formRef = useRef(null) const strength = useMemo(() => getPasswordStrength(newPassword), [newPassword]) const meta = STRENGTH_META[strength] useEffect(() => { if (state?.success) { toast.success(state.message ?? t("success")) formRef.current?.reset() } else if (state?.message) { toast.error(state.message) } }, [state, t]) return ( {t("title")} {t("description")}
setNewPassword("")}>
setNewPassword(e.target.value)} />
{newPassword.length > 0 && (
{t("strength")} {t(meta.labelKey)}
)}
{t("requirements")}
    {PASSWORD_REQUIREMENT_HINTS.map((hint) => (
  • {hint}
  • ))}
) }