"use client" import { useActionState, useEffect, useMemo, useRef, useState } from "react" import { useFormStatus } from "react-dom" 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, label: "Weak", barClassName: "h-2", indicatorClassName: "bg-red-500" }, medium: { value: 66, label: "Medium", barClassName: "h-2", indicatorClassName: "bg-yellow-500" }, strong: { value: 100, label: "Strong", barClassName: "h-2", indicatorClassName: "bg-green-500" }, } function SubmitButton() { const { pending } = useFormStatus() return ( ) } export function PasswordChangeForm() { 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 ?? "Password changed successfully") formRef.current?.reset() } else if (state?.message) { toast.error(state.message) } }, [state]) return ( Change Password Choose a strong password to keep your account secure.
setNewPassword("")}>
setNewPassword(e.target.value)} />
{newPassword.length > 0 && (
Password strength {meta.label}
)}
Password requirements:
    {PASSWORD_REQUIREMENT_HINTS.map((hint) => (
  • {hint}
  • ))}
) }