import React, { useState, useEffect } from 'react';
import { useLanguage } from '../contexts/LanguageContext';
import {
Bell, Moon, Globe, Shield, Smartphone, Trash2, LogOut,
Info, ChevronRight, Save, Database, Palette, Volume2,
VolumeX, Radio, Zap, Lock, Eye, EyeOff, ShieldCheck,
HelpCircle, User, MessageSquare, Headphones, Construction,
Clock, Share2, Award, Heart, Mic, Camera, MapPin, Signal,
Gamepad2, History, ArrowDownLeft, ArrowUpRight, Coins, BookOpen, Crown, Star
} from 'lucide-react';
import { StorageService } from '../services/storageService';
import { useNavigate } from 'react-router-dom';
import { Button } from '../components/ui/Button';
import { AppSettings, UserProfile, Transaction } from '../types';
import { requestMicPermission } from '../services/geminiService';
import confetti from 'canvas-confetti';
const SettingSection = ({ title, icon: Icon, children, className = "" }: { title: string, icon?: any, children?: React.ReactNode, className?: string }) => {
const { t } = useLanguage();
return (
{Icon && }
{t(title, title)}
{children}
);
};
const SettingItem = ({
icon: Icon,
label,
description,
action,
onClick,
badge
}: {
icon: any,
label: string,
description?: string,
action?: React.ReactNode,
onClick?: () => void,
badge?: string
}) => {
const { t } = useLanguage();
return (
{t(label, label)}
{badge &&
{badge}}
{description &&
{t(description, description)}
}
{action}
);
};
const Toggle = ({ checked, onChange }: { checked: boolean, onChange: () => void }) => (
);
const BadgeItem = ({ name, desc, icon: Icon, unlocked, color }: { name: string, desc: string, icon: any, unlocked: boolean, color: string }) => (
{name}
{unlocked ? desc : 'Locked'}
);
const SubscriptionCard = ({ tier, price, karmaCost, perks, active, onBuy }: any) => (
{active &&
Current Plan
}
{tier}
Support Us!!
{perks.map((p: string, i: number) => (
-
{p}
))}
);
import { CheckCircle } from 'lucide-react';
const Settings: React.FC = () => {
const { t, language, setLanguage } = useLanguage();
const navigate = useNavigate();
const [profile, setProfile] = useState(null);
const [settings, setSettings] = useState(null);
const [transactions, setTransactions] = useState([]);
const [loading, setLoading] = useState(true);
const [activeTab, setActiveTab] = useState<'general' | 'account' | 'privacy'>('general');
useEffect(() => {
const init = async () => {
const [p, s] = await Promise.all([
StorageService.getProfile(),
StorageService.getSettings()
]);
setProfile(p);
setSettings(s);
if (p) {
const txs = await StorageService.getTransactions(p.id);
setTransactions(txs);
}
setLoading(false);
};
init();
}, []);
const updateSettings = async (updates: Partial) => {
if (!settings) return;
const newSettings = { ...settings, ...updates };
setSettings(newSettings);
await StorageService.saveSettings(newSettings);
if (updates.language) setLanguage(updates.language);
};
const updatePermission = async (key: keyof AppSettings['permissions']) => {
if (!settings) return;
const currentState = settings.permissions[key];
if (!currentState) {
if (key === 'microphone') await requestMicPermission();
// Other permissions would be requested here
}
const newPermissions = { ...settings.permissions, [key]: !currentState };
await updateSettings({ permissions: newPermissions });
};
const handleLogout = async () => {
if (confirm("Sign out of Rudraksha?")) {
await StorageService.logout();
navigate('/auth');
}
};
const handleSubscription = async (tier: 'weekly' | 'monthly' | 'lifetime', cost: number, currency: 'karma' | 'money') => {
const res = await StorageService.purchaseSubscription(tier, cost, currency);
if (res.success) {
confetti({ particleCount: 150, spread: 70, origin: { y: 0.6 } });
const p = await StorageService.getProfile();
setProfile(p);
alert(`Successfully subscribed to ${tier} plan!`);
} else {
alert(res.error || "Transaction Failed");
}
};
const clearCache = () => {
if (confirm("Clear local cache? This will reset some local preferences but keep your account data.")) {
alert("Cache cleared successfully!");
}
};
if (loading || !settings) return
;
const hasScholarBadge = profile ? (profile.unlockedItems || []).some(item => item === 'badge_scholar') : false;
const hasCustomTheme = profile ? (profile.unlockedItems || []).some(item => item.startsWith('theme_')) : false;
const hasDonated = profile ? (profile.unlockedItems || []).some(item => item.startsWith('donate_')) : false;
return (
{t("Control Room", "Control Room")}
{t("Optimize your Nepali companion experience.", "Optimize your Nepali companion experience.")}
{[
{ id: 'general', label: 'App', icon: Smartphone },
{ id: 'account', label: 'Account', icon: User },
{ id: 'privacy', label: 'Safety', icon: ShieldCheck }
].map(tab => (
))}
{activeTab === 'general' && (
}
onClick={() => navigate('/settings/themes')}
/>
}
/>
updateSettings({ dataSaver: !settings.dataSaver })} />}
/>
updateSettings({ soundEnabled: !settings.soundEnabled })} />}
/>
updateSettings({ autoFocusMode: !settings.autoFocusMode })} />}
/>
RESET}
/>
)}
{activeTab === 'account' && (
{/* Profile Header */}
{profile?.name}
{profile?.email}
{profile?.role}
Level {Math.floor((profile?.xp || 0)/500)+1}
{/* Membership Plans */}
Membership Tiers
handleSubscription('weekly', 500, currency)}
/>
handleSubscription('monthly', 1500, currency)}
/>
handleSubscription('lifetime', 10000, currency)}
/>
{transactions.length === 0 ? (
) : (
transactions.map(tx => (
0 ? 'bg-green-100 text-green-600' : 'bg-red-100 text-red-600'}`}>
{tx.amount > 0 ?
: }
{tx.description || tx.type}
{new Date(tx.timestamp).toLocaleDateString()}
0 ? 'text-green-600' : 'text-red-500'}`}>
{tx.amount > 0 ? '+' : ''}{tx.amount}
))
)}
Danger Zone
)}
{activeTab === 'privacy' && (
updatePermission('microphone')} />} />
updatePermission('camera')} />} />
updatePermission('location')} />} />
)}
);
};
export default Settings;