Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 46x 46x 46x 46x 46x 46x 46x 46x 46x 5x 5x 4x 4x 4x 4x 2x 2x 2x 2x 5x 1x 5x 3x 3x 5x 46x 4x 4x 3x 3x 3x 3x 1x 1x 1x 1x 4x 2x 2x 4x 46x 5x 3x 3x 3x 1x 1x 1x 1x 1x 1x 5x 2x 2x 5x 46x 3x 3x 3x 3x 2x 2x 2x 1x 1x 1x 1x 1x 3x 46x 46x 46x 46x 46x 46x 46x 46x 46x 8x 8x 8x 8x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x | import { useState } from "react";
import DashboardShell from "../components/layout/DashboardShell";
import { useAuthStore } from "../stores/authStore";
import { updateEmail, updatePassword, requestPasswordReset } from "../api/auth";
import { deleteAccount } from "../api/users";
// Helper to safely extract error messages
const getErrorMessage = (err: unknown): string => {
if (err instanceof Error) return err.message;
if (typeof err === "string") return err;
return "An unexpected error occurred";
};
export default function Settings() {
const { user, logout } = useAuthStore();
const [email, setEmail] = useState(user?.email || "");
const [password, setPassword] = useState("");
const [status, setStatus] = useState<{
type: "success" | "error";
msg: string;
} | null>(null);
const [isDeleting, setIsDeleting] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const handleUpdateEmail = async (e: React.FormEvent) => {
e.preventDefault();
if (email === user?.email) return;
setIsLoading(true);
setStatus(null);
try {
await updateEmail(email);
setStatus({
type: "success",
msg: "Verification link sent to new email.",
});
} catch (err) {
setStatus({ type: "error", msg: getErrorMessage(err) });
} finally {
setIsLoading(false);
}
};
const handleUpdatePassword = async (e: React.FormEvent) => {
e.preventDefault();
if (!password) return;
setIsLoading(true);
setStatus(null);
try {
await updatePassword(password);
setStatus({ type: "success", msg: "Password updated successfully." });
setPassword("");
} catch (err) {
setStatus({ type: "error", msg: getErrorMessage(err) });
} finally {
setIsLoading(false);
}
};
const handleResetRequest = async () => {
if (!user?.email) return;
setIsLoading(true);
try {
await requestPasswordReset(user.email);
setStatus({
type: "success",
msg: "Password reset instructions sent to your email.",
});
} catch (err) {
setStatus({ type: "error", msg: getErrorMessage(err) });
} finally {
setIsLoading(false);
}
};
const handleDeleteAccount = async () => {
const confirmed = window.confirm(
"Are you absolutely sure? This will delete your account, monitors, and ping history permanently.",
);
if (!confirmed) return;
setIsDeleting(true);
try {
await deleteAccount();
logout();
} catch (err) {
setStatus({ type: "error", msg: getErrorMessage(err) });
setIsDeleting(false);
}
};
return (
<DashboardShell>
<div className="w-full space-y-8">
<div>
<h1 className="text-2xl font-bold text-gray-900">Settings</h1>
<p className="mt-1 text-sm text-gray-600">
Manage your account credentials and security.
</p>
</div>
{status && (
<div
className={`p-4 rounded-md ${status.type === "success" ? "bg-green-50 text-green-700" : "bg-red-50 text-red-700"}`}
>
{status.msg}
</div>
)}
<div className="grid grid-cols-1 md:grid-cols-2 gap-8">
{/* Email Card */}
<div className="rounded-lg border border-gray-200 bg-white">
<div className="border-b border-gray-200 px-6 py-4">
<h2 className="text-lg font-medium text-gray-900">
Email Address
</h2>
</div>
<div className="p-6">
<form onSubmit={handleUpdateEmail} className="space-y-4">
<div>
<input
type="email"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
className="block w-full rounded-md border border-gray-300 px-3 py-2 sm:text-sm focus:ring-2 focus:ring-blue-500 focus:outline-none"
/>
</div>
<button
type="submit"
disabled={isLoading || email === user?.email}
className="rounded-md bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700 disabled:opacity-50 cursor-pointer"
>
Update Email
</button>
</form>
</div>
</div>
{/* Password Card */}
<div className="rounded-lg border border-gray-200 bg-white">
<div className="border-b border-gray-200 px-6 py-4">
<h2 className="text-lg font-medium text-gray-900">Password</h2>
</div>
<div className="p-6">
<form onSubmit={handleUpdatePassword} className="space-y-4">
<div>
<input
type="password"
placeholder="New password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="block w-full rounded-md border border-gray-300 px-3 py-2 sm:text-sm focus:ring-2 focus:ring-blue-500 focus:outline-none"
/>
</div>
<div className="flex gap-4">
<button
type="submit"
disabled={isLoading || !password}
className="rounded-md bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700 disabled:opacity-50 cursor-pointer"
>
Set Password
</button>
<button
type="button"
onClick={handleResetRequest}
disabled={isLoading}
className="text-sm font-medium text-blue-600 hover:text-blue-500 disabled:opacity-50 cursor-pointer"
>
Send Reset Link
</button>
</div>
</form>
</div>
</div>
</div>
{/* Danger Zone */}
<div className="rounded-lg border border-red-200 bg-white">
<div className="border-b border-red-200 bg-red-50 px-6 py-4 rounded-t-lg">
<h2 className="text-lg font-medium text-red-800">Danger Zone</h2>
</div>
<div className="p-6">
<p className="text-sm text-gray-600 mb-4">
Permanently delete your account and all monitoring data. This
action cannot be undone.
</p>
<button
onClick={handleDeleteAccount}
disabled={isDeleting}
className="rounded-md border border-red-300 bg-white px-4 py-2 text-sm font-medium text-red-700 hover:bg-red-50 disabled:opacity-50 cursor-pointer"
>
{isDeleting ? "Deleting..." : "Delete Account"}
</button>
</div>
</div>
</div>
</DashboardShell>
);
}
|