import React, { useEffect, useState } from "react"; import { Bell, 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 { Separator } from "../components/ui/separator"; 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" | "notifications" | "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: "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: any; title: string; description: string; children: React.ReactNode; }) { return (

{title}

{description}

{children}
); } 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
setFirstName(e.target.value)} className="rounded-[6px]" />
setLastName(e.target.value)} className="rounded-[6px]" />
setNickName(e.target.value)} className="rounded-[6px]" />
Preferences
); } 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
setCurrentPassword(e.target.value)} disabled={saving} />
setNewPassword(e.target.value)} disabled={saving} />
setConfirmPassword(e.target.value)} disabled={saving} />
); } 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 ( ); })}

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

{activeTab === "subscription" && } {activeTab === "app-versions" && } {activeTab === "profile" && } {activeTab === "security" && } {activeTab === "notifications" && } {activeTab === "appearance" && }
); }