import { useEffect, useState } from "react";
import {
Bell,
Eye,
EyeOff,
Globe,
KeyRound,
Languages,
Lock,
Moon,
Palette,
Save,
Shield,
Sun,
User,
} from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "../components/ui/card";
import { Input } from "../components/ui/input";
import { Button } from "../components/ui/button";
import { Select } from "../components/ui/select";
import { Separator } from "../components/ui/separator";
import { cn } from "../lib/utils";
import { SpinnerIcon } from "../components/ui/spinner-icon";
import { getMyProfile, updateProfile } from "../api/users.api";
import type { UserProfileData } from "../types/user.types";
import { toast } from "sonner";
type SettingsTab = "profile" | "security" | "notifications" | "appearance";
const tabs: { id: SettingsTab; label: string; icon: typeof User }[] = [
{ id: "profile", label: "Profile", icon: User },
{ id: "security", label: "Security", icon: Shield },
{ id: "notifications", label: "Notifications", icon: Bell },
{ id: "appearance", label: "Appearance", icon: Palette },
];
function Toggle({
enabled,
onToggle,
}: {
enabled: boolean;
onToggle: () => void;
}) {
return (
);
}
function SettingRow({
icon: Icon,
title,
description,
children,
}: {
icon: typeof User;
title: string;
description: string;
children: React.ReactNode;
}) {
return (
);
}
function LoadingSkeleton() {
return (
{[1, 2, 3, 4].map((i) => (
))}
{[1, 2, 3, 4].map((j) => (
))}
);
}
function ProfileTab({ profile }: { profile: UserProfileData }) {
const [firstName, setFirstName] = useState(profile.first_name);
const [lastName, setLastName] = useState(profile.last_name);
const [nickName, setNickName] = useState(profile.nick_name || "");
const [language, setLanguage] = useState(profile.preferred_language || "en");
const [saving, setSaving] = useState(false);
const handleSave = async () => {
setSaving(true);
try {
await updateProfile({
first_name: firstName,
last_name: lastName,
nick_name: nickName,
preferred_language: language,
});
toast.success("Profile settings saved");
} catch {
toast.error("Failed to save profile settings.");
} finally {
setSaving(false);
}
};
return (
setNickName(e.target.value)} />
);
}
function SecurityTab() {
const [showCurrent, setShowCurrent] = useState(false);
const [showNew, setShowNew] = useState(false);
const [showConfirm, setShowConfirm] = useState(false);
const [saving, setSaving] = useState(false);
const handleChangePassword = async () => {
setSaving(true);
try {
await new Promise((r) => setTimeout(r, 600));
toast.success("Password updated successfully");
} finally {
setSaving(false);
}
};
return (
Two-Factor Authentication
toast.info("2FA coming soon")} />
);
}
function NotificationsTab() {
const [emailNotifs, setEmailNotifs] = useState(true);
const [pushNotifs, setPushNotifs] = useState(true);
const [loginAlerts, setLoginAlerts] = useState(true);
const [weeklyDigest, setWeeklyDigest] = useState(false);
return (
setEmailNotifs(!emailNotifs)} />
setPushNotifs(!pushNotifs)} />
setLoginAlerts(!loginAlerts)} />
setWeeklyDigest(!weeklyDigest)} />
);
}
function AppearanceTab() {
const [theme, setTheme] = useState<"light" | "dark" | "system">("light");
return (
{(
[
{ id: "light", label: "Light", icon: Sun },
{ id: "dark", label: "Dark", icon: Moon },
{ id: "system", label: "System", icon: Globe },
] as const
).map(({ id, label, icon: Icon }) => (
))}
);
}
export function SettingsPage() {
const [activeTab, setActiveTab] = useState("profile");
const [profile, setProfile] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const fetchProfile = async () => {
try {
const res = await getMyProfile();
setProfile(res.data.data);
} catch (err) {
console.error("Failed to fetch profile", err);
setError("Failed to load settings. Please try again later.");
} finally {
setLoading(false);
}
};
fetchProfile();
}, []);
if (loading) return ;
if (error || !profile) {
return (
{error || "Settings not available"}
Please check your connection and try again.
);
}
return (
{/* Page header */}
Settings
Manage your account preferences and configuration
{/* Tab navigation */}
{tabs.map(({ id, label, icon: Icon }) => (
))}
{/* Tab content */}
{activeTab === "profile" &&
}
{activeTab === "security" &&
}
{activeTab === "notifications" &&
}
{activeTab === "appearance" &&
}
);
}