import React, { useState } from 'react'; import { useAuth0 } from '@auth0/auth0-react'; import { useLogout } from '../../../core/auth/useLogout'; import { GlassCard } from '../../../shared-minimal/components/mobile/GlassCard'; import { MobileContainer } from '../../../shared-minimal/components/mobile/MobileContainer'; import { useSettings } from '../hooks/useSettings'; import { useProfile, useUpdateProfile } from '../hooks/useProfile'; import { useExportUserData } from '../hooks/useExportUserData'; import { useVehicles } from '../../vehicles/hooks/useVehicles'; import { useAdminAccess } from '../../../core/auth/useAdminAccess'; import { useNavigationStore } from '../../../core/store'; import { DeleteAccountModal } from './DeleteAccountModal'; import { PendingDeletionBanner } from './PendingDeletionBanner'; import { ImportButton } from '../components/ImportButton'; import { ImportDialog } from '../components/ImportDialog'; interface ToggleSwitchProps { enabled: boolean; onChange: () => void; label: string; description?: string; } const ToggleSwitch: React.FC = ({ enabled, onChange, label, description }) => (

{label}

{description && (

{description}

)}
); interface ModalProps { isOpen: boolean; onClose: () => void; title: string; children: React.ReactNode; } const Modal: React.FC = ({ isOpen, onClose, title, children }) => { if (!isOpen) return null; return (

{title}

{children}
); }; export const MobileSettingsScreen: React.FC = () => { const { user } = useAuth0(); const { logout } = useLogout(); const { navigateToScreen } = useNavigationStore(); const { settings, updateSetting, isLoading, error } = useSettings(); const { data: profile, isLoading: profileLoading } = useProfile(); const updateProfileMutation = useUpdateProfile(); const exportMutation = useExportUserData(); const { data: vehicles, isLoading: vehiclesLoading } = useVehicles(); const { isAdmin, loading: adminLoading } = useAdminAccess(); const [showDataExport, setShowDataExport] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [isEditingProfile, setIsEditingProfile] = useState(false); const [editedDisplayName, setEditedDisplayName] = useState(''); const [editedNotificationEmail, setEditedNotificationEmail] = useState(''); const [showImportDialog, setShowImportDialog] = useState(false); const [importFile, setImportFile] = useState(null); // Initialize edit form when profile loads or edit mode starts React.useEffect(() => { if (profile && isEditingProfile) { setEditedDisplayName(profile.displayName || ''); setEditedNotificationEmail(profile.notificationEmail || ''); } }, [profile, isEditingProfile]); const handleLogout = () => { logout(); }; const handleExportData = () => { setShowDataExport(false); exportMutation.mutate(); }; const handleImportFileSelected = (file: File) => { setImportFile(file); setShowImportDialog(true); }; const handleImportDialogClose = () => { setShowImportDialog(false); setImportFile(null); }; const handleEditProfile = () => { setIsEditingProfile(true); }; const handleCancelEdit = () => { setIsEditingProfile(false); setEditedDisplayName(profile?.displayName || ''); setEditedNotificationEmail(profile?.notificationEmail || ''); }; const handleSaveProfile = async () => { const updates: { displayName?: string; notificationEmail?: string } = {}; if (editedDisplayName !== (profile?.displayName || '')) { updates.displayName = editedDisplayName; } if (editedNotificationEmail !== (profile?.notificationEmail || '')) { updates.notificationEmail = editedNotificationEmail || undefined; } if (Object.keys(updates).length === 0) { setIsEditingProfile(false); return; } try { await updateProfileMutation.mutateAsync(updates); setIsEditingProfile(false); } catch (error) { // Error is handled by the mutation hook } }; // Loading state if (isLoading) { return (
Loading settings...
); } // Error state if (error) { return (

Failed to load settings

{error}

); } return (
{/* Header */}

Settings

Manage your account and preferences

{/* Pending Deletion Banner */} {/* Profile Section */}

Profile

{!isEditingProfile && !profileLoading && ( )}
{profileLoading ? (
) : isEditingProfile ? (

Email is managed by your account provider

setEditedDisplayName(e.target.value)} placeholder="Enter your display name" className="w-full px-3 py-2 border border-slate-300 dark:border-silverstone rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500 dark:bg-nero dark:text-avus" style={{ fontSize: '16px', minHeight: '44px' }} />
setEditedNotificationEmail(e.target.value)} placeholder="Leave blank to use your primary email" className="w-full px-3 py-2 border border-slate-300 dark:border-silverstone rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500 dark:bg-nero dark:text-avus" style={{ fontSize: '16px', minHeight: '44px' }} />

Optional: Use a different email for notifications

) : (
{user?.picture ? ( Profile ) : (
{profile?.displayName?.charAt(0) || user?.name?.charAt(0) || user?.email?.charAt(0)}
)}

{profile?.displayName || user?.name || 'User'}

{profile?.email || user?.email}

Display Name

{profile?.displayName || 'Not set'}

Notification Email

{profile?.notificationEmail || 'Using primary email'}

)}
{/* My Vehicles Section */}

My Vehicles

{!vehiclesLoading && vehicles && ( ({vehicles.length}) )}
{vehiclesLoading ? (
) : !vehicles?.length ? (

No vehicles registered. Add your first vehicle to get started.

) : (
{vehicles.map((vehicle) => (

{vehicle.year} {vehicle.make} {vehicle.model}

{vehicle.nickname && (

{vehicle.nickname}

)}
))}
)}
{/* Notifications Section */}

Notifications

updateSetting('notifications', { ...settings.notifications, email: !settings.notifications.email })} label="Email Notifications" description="Receive updates via email" /> updateSetting('notifications', { ...settings.notifications, push: !settings.notifications.push })} label="Push Notifications" description="Receive mobile push notifications" /> updateSetting('notifications', { ...settings.notifications, maintenance: !settings.notifications.maintenance })} label="Maintenance Reminders" description="Get reminded about vehicle maintenance" />
{/* Appearance & Units Section */}

Appearance & Units

updateSetting('darkMode', !settings.darkMode)} label="Dark Mode" description="Switch to dark theme" />

Unit System

Currently using {settings.unitSystem === 'imperial' ? 'Miles, Gallons, MPG, USD' : 'Km, Liters, L/100km, EUR'}

{/* Data Management Section */}

Data Management

Restore your data from a previous export

Download a copy of all your vehicle and fuel data

{/* Security & Privacy Section */}

Security & Privacy

{/* Admin Console Section */} {!adminLoading && isAdmin && (

Admin Console

)} {/* Account Actions Section */}

Account Actions

{/* Data Export Modal */} setShowDataExport(false)} title="Export Data" >

This will create a downloadable file containing all your vehicle data, fuel logs, and preferences.

{/* Delete Account Modal */} setShowDeleteConfirm(false)} /> {/* Import Dialog */}
); };