"use client" import React, { useState, useRef, useEffect } from "react" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { Label } from "@/components/ui/label" import { CheckCircleIcon, Key, Loader2, XCircleIcon } from "lucide-react" import Link from "next/link" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog" import { toast } from "sonner" import { motion, useAnimationControls } from "framer-motion" export function ChangeEmailPassword() { const [newPassword, setNewPassword] = useState("") const [loading, setLoading] = useState(false) const [open, setOpen] = useState(false) const holdTimeoutRef = useRef(null) const intervalRef = useRef(null) const controls = useAnimationControls() const [isHolding, setIsHolding] = useState(false) const holdDuration = 10 const [remainingTime, setRemainingTime] = useState(holdDuration) const submitPasswordChange = async () => { setLoading(true) try { const response = await fetch("/api/mail/password", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ password: newPassword }), }) const resData = await response.json() if (response.ok && resData.success) { toast("Password updated successfully!", { icon: , style: { backgroundColor: "oklch(var(--success))", color: "oklch(var(--success-foreground))", }, }) setTimeout(() => { setOpen(false) setNewPassword("") controls.set({ "--progress": "0%" }) }, 1500) } else if (resData.error) { toast("An error occurred", { description: resData.error, icon: , style: { backgroundColor: "oklch(var(--error))", color: "oklch(var(--error-foreground))", }, }) controls.set({ "--progress": "0%" }) } else { toast("Failed to Update", { description: "An unknown error occurred [1]", icon: , style: { backgroundColor: "oklch(var(--error))", color: "oklch(var(--error-foreground))", }, }) controls.set({ "--progress": "0%" }) } } catch (error) { console.log(error) toast("Failed to Update", { description: "An unknown error occurred [2]", icon: , style: { backgroundColor: "oklch(var(--error))", color: "oklch(var(--error-foreground))", }, }) controls.set({ "--progress": "0%" }) } finally { setLoading(false) setIsHolding(false) if (holdTimeoutRef.current) { clearTimeout(holdTimeoutRef.current) holdTimeoutRef.current = null } if (intervalRef.current) { clearInterval(intervalRef.current) intervalRef.current = null } } } const handleFormSubmit = (e: React.FormEvent) => { e.preventDefault() } const holdDurationMs = holdDuration * 1000 const handleHoldStart = () => { if (loading || newPassword.length < 8) return setIsHolding(true) controls.set({ "--progress": "0%" }) controls.start( { "--progress": "100%" }, { duration: holdDuration, ease: "linear" } ) holdTimeoutRef.current = setTimeout(() => { console.log("[i] Hold complete, submitting...") if (intervalRef.current) { clearInterval(intervalRef.current) intervalRef.current = null } submitPasswordChange() holdTimeoutRef.current = null }, holdDurationMs) setRemainingTime(holdDuration) if (intervalRef.current) clearInterval(intervalRef.current) intervalRef.current = setInterval(() => { setRemainingTime((prevTime) => { if (prevTime <= 1) { if (intervalRef.current) clearInterval(intervalRef.current) return 0 } return prevTime - 1 }) }, 1000) } const handleHoldEnd = () => { if (holdTimeoutRef.current) { clearTimeout(holdTimeoutRef.current) holdTimeoutRef.current = null } if (isHolding) { console.log("[i] Hold interrupted") controls.stop() controls.set({ "--progress": "0%" }) setIsHolding(false) if (intervalRef.current) { clearInterval(intervalRef.current) intervalRef.current = null } } } useEffect(() => { return () => { if (holdTimeoutRef.current) { clearTimeout(holdTimeoutRef.current) } if (intervalRef.current) { clearInterval(intervalRef.current) } } }, []) useEffect(() => { if (!open) { handleHoldEnd() setLoading(false) setNewPassword("") } }, [open]) return ( Change Your Password This only applies to your email account. Make sure it's secure, and consider using LibreCloud Pass to keep it safe!
setNewPassword(e.target.value)} className="mt-1.5" />

Password must be at least 8 characters long.

) } export default ChangeEmailPassword