import React, { useEffect, useState } from "react";
import {
Eye,
EyeOff,
Globe,
Lock,
Moon,
Palette,
Save,
Shield,
Sun,
User,
CreditCard,
Smartphone,
} 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 { cn } from "../lib/utils";
import { SpinnerIcon } from "../components/ui/spinner-icon";
import { changeTeamMemberPassword } from "../api/team.api";
import { logoutToLogin } from "../lib/auth";
import { getMyProfile, updateProfile } from "../api/users.api";
import type { UserProfileData } from "../types/user.types";
import { toast } from "sonner";
import { AppVersionsTab } from "./settings/AppVersionsTab";
import { SubscriptionPlansTab } from "./settings/SubscriptionPlansTab";
import { ThemeModePreview } from "./settings/components/ThemeModePreview";
import { useTheme } from "../contexts/ThemeContext";
type SettingsTab =
| "subscription"
| "app-versions"
| "profile"
| "security"
| "appearance";
const tabs: { id: SettingsTab; label: string; icon: typeof User }[] = [
{ id: "subscription", label: "Subscription packages", icon: CreditCard },
{ id: "app-versions", label: "App versions", icon: Smartphone },
{ id: "profile", label: "Profile", icon: User },
{ id: "security", label: "Security", icon: Shield },
{ id: "appearance", label: "Appearance", icon: Palette },
];
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 (
Personal Information
Nickname
setNickName(e.target.value)}
className="rounded-[6px]"
/>
Preferences
Preferred Language
setLanguage(e.target.value)}
className="rounded-[6px]"
>
English
Amharic
Afan Oromo
Tigrinya
{saving ? (
) : (
)}
{saving ? "Saving…" : "Save Changes"}
);
}
function SecurityTab({ memberId }: { memberId: number }) {
const [currentPassword, setCurrentPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [showCurrent, setShowCurrent] = useState(false);
const [showNew, setShowNew] = useState(false);
const [showConfirm, setShowConfirm] = useState(false);
const [saving, setSaving] = useState(false);
const handleChangePassword = async () => {
if (!currentPassword.trim()) {
toast.error("Enter your current password.");
return;
}
if (newPassword.length < 8) {
toast.error("New password must be at least 8 characters.");
return;
}
if (newPassword !== confirmPassword) {
toast.error("New password and confirmation do not match.");
return;
}
if (currentPassword === newPassword) {
toast.error("New password must be different from your current password.");
return;
}
setSaving(true);
try {
await changeTeamMemberPassword(memberId, {
current_password: currentPassword,
new_password: newPassword,
});
logoutToLogin({ passwordChanged: true });
return;
} catch (e: unknown) {
const msg =
(e as { response?: { data?: { message?: string } } })?.response?.data
?.message ?? "Failed to update password.";
toast.error(msg);
} finally {
setSaving(false);
}
};
return (
Change Password
{saving ? (
) : (
)}
{saving ? "Updating…" : "Update Password"}
);
}
function NotificationsTab() {
const [emailNotifs, setEmailNotifs] = useState(true);
const [pushNotifs, setPushNotifs] = useState(true);
const [loginAlerts, setLoginAlerts] = useState(true);
return (
Notification Preferences
setEmailNotifs(!emailNotifs)}
/>
setPushNotifs(!pushNotifs)}
/>
setLoginAlerts(!loginAlerts)}
/>
);
}
function AppearanceTab() {
const { theme, setTheme, resolvedTheme, systemTheme } = useTheme();
const options = [
{
id: "light" as const,
label: "Light",
description: "Always bright UI",
icon: Sun,
preview: "light" as const,
},
{
id: "dark" as const,
label: "Dark",
description: "Always dark UI",
icon: Moon,
preview: "dark" as const,
},
{
id: "system" as const,
label: "System",
description: `Follows device (${systemTheme})`,
icon: Globe,
preview: "system" as const,
},
];
return (
Theme
Active appearance:{" "}
{resolvedTheme}
{theme === "system" ? " (from your device setting)" : null}
{theme === "light" ? " (fixed — not tied to device)" : null}
{options.map(({ id, label, description, icon: Icon, preview }) => {
const selected = theme === id;
return (
setTheme(id)}
className={cn(
"flex flex-col items-stretch gap-3 rounded-[8px] border-2 p-3 text-left transition-all",
selected
? "border-brand-500 bg-brand-500/10 shadow-sm ring-1 ring-brand-500/30"
: "border-grayScale-200 bg-grayScale-50 hover:border-grayScale-300 hover:bg-grayScale-100",
)}
>
);
})}
Light vs System: Light
always stays bright. System copies your Windows/macOS theme — if your device is in
light mode, System will match Light; switch your device to dark to see System use the
dark admin theme.
);
}
export function SettingsPage() {
const [activeTab, setActiveTab] = useState("subscription");
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 (
Settings
Manage your account preferences, subscriptions, and system
configurations with ease
{tabs.map((tab) => {
const Icon = tab.icon;
const active = activeTab === tab.id;
return (
setActiveTab(tab.id)}
className={cn(
"flex items-center gap-2.5 whitespace-nowrap rounded-[6px] px-3 py-2.5 text-left text-sm font-medium transition-colors",
active
? "bg-brand-50 text-brand-600"
: "text-grayScale-600 hover:bg-grayScale-50",
)}
>
{tab.label}
);
})}
{activeTab === "subscription" && }
{activeTab === "app-versions" && }
{activeTab === "profile" && }
{activeTab === "security" && }
{activeTab === "appearance" && }
);
}