import { useEffect, useState } from 'react'; import { IconAlertCircle, IconPalette, IconUser, IconX } from '@tabler/icons-react'; import { ActionIcon, Alert, Button, ColorSwatch, Divider, Group, Modal, NavLink, PasswordInput, rem, Stack, Text, TextInput, Title, useMantineTheme, } from '@mantine/core'; interface User { id: string; username: string; } interface SettingsModalProps { opened: boolean; close: () => void; primaryColor: string; setPrimaryColor: (color: string) => void; } export function SettingsModal({ opened, close, primaryColor, setPrimaryColor, }: SettingsModalProps) { const theme = useMantineTheme(); const [activeTab, setActiveTab] = useState<'appearance' | 'account'>('appearance'); // Account State const [user, setUser] = useState(null); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [isLoginMode, setIsLoginMode] = useState(true); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); // Check login status on mount useEffect(() => { if (opened) { fetchUser(); } }, [opened]); const fetchUser = async () => { try { const res = await fetch('/api/auth/me'); const data = await res.json(); if (data.user) { setUser(data.user); } else { setUser(null); } } catch (e) { console.error(e); } }; const handleAuth = async () => { setError(''); setLoading(true); const endpoint = isLoginMode ? '/api/auth/login' : '/api/auth/register'; try { const res = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }), }); const data = await res.json(); if (!res.ok) { throw new Error(data.error || 'Something went wrong'); } // Refresh user state await fetchUser(); setUsername(''); setPassword(''); } catch (err: any) { setError(err.message); } finally { setLoading(false); } }; const handleLogout = async () => { await fetch('/api/auth/logout', { method: 'POST' }); setUser(null); }; const colors = Object.keys(theme.colors).filter( (color) => color !== 'dark' && color !== 'gray' && color !== 'white' && color !== 'black' ); return ( {/* Left Sidebar */} } variant="light" color={primaryColor} onClick={() => setActiveTab('appearance')} style={{ borderRadius: 'var(--mantine-radius-lg)' }} /> } variant="light" color={primaryColor} onClick={() => setActiveTab('account')} style={{ borderRadius: 'var(--mantine-radius-lg)' }} /> {/* Right Content */} {activeTab === 'appearance' && ( <> Appearance Customize the look and feel of the application. Accent Color {colors.map((color) => ( setPrimaryColor(color)} style={{ color: '#fff', cursor: 'pointer' }} withShadow > {primaryColor === color && } ))} )} {activeTab === 'account' && ( <> Account Manage your account and chat history. {user ? ( Logged in as {user.username} ) : ( {error && ( } title="Error" color="red"> {error} )} setUsername(e.target.value)} /> setPassword(e.target.value)} /> setIsLoginMode(!isLoginMode)} > {isLoginMode ? 'Need an account? Register' : 'Have an account? Login'} )} )} ); }