import React, { useState } from 'react'; import { View, Text, TextInput, TouchableOpacity, StyleSheet, ScrollView, ActivityIndicator, Alert, } from 'react-native'; import { useTheme } from '@react-navigation/native'; import { useRouter, Stack } from 'expo-router'; import { Ionicons } from '@expo/vector-icons'; import { useAuth } from '~/context/AuthProvider'; import { useEmailsStore } from '~/store/emailsStore'; import { useAppTheme } from '~/theme/ThemeProvider'; import { accountsApi } from '~/utils/api'; export default function AccountsScreen() { const { colors } = useTheme(); const { isDarkMode } = useAppTheme(); const router = useRouter(); const { getToken } = useAuth(); const { accounts, fetchAccounts, selectAccount } = useEmailsStore(); const [showAddForm, setShowAddForm] = useState(false); const [loading, setLoading] = useState(false); const [formData, setFormData] = useState({ name: '', email: '', imapHost: '', imapPort: '993', smtpHost: '', smtpPort: '587', password: '', }); const handleAddAccount = async () => { if (!formData.email || !formData.password || !formData.imapHost) { Alert.alert('Error', 'Please fill in all required fields'); return; } setLoading(true); const token = await getToken(); if (!token) { setLoading(false); return; } const result = await accountsApi.create( { name: formData.name || formData.email.split('@')[0], email: formData.email, provider: 'imap', imapHost: formData.imapHost, imapPort: parseInt(formData.imapPort, 10), smtpHost: formData.smtpHost || formData.imapHost.replace('imap', 'smtp'), smtpPort: parseInt(formData.smtpPort, 10), password: formData.password, }, token ); setLoading(false); if (result.error) { Alert.alert('Error', result.error.message); return; } await fetchAccounts(token); setShowAddForm(false); setFormData({ name: '', email: '', imapHost: '', imapPort: '993', smtpHost: '', smtpPort: '587', password: '', }); Alert.alert('Success', 'Account added successfully'); }; const handleDeleteAccount = async (id: string) => { Alert.alert('Delete Account', 'Are you sure you want to remove this email account?', [ { text: 'Cancel', style: 'cancel' }, { text: 'Delete', style: 'destructive', onPress: async () => { const token = await getToken(); if (!token) return; const result = await accountsApi.delete(id, token); if (result.error) { Alert.alert('Error', result.error.message); return; } await fetchAccounts(token); }, }, ]); }; const handleSetDefault = async (id: string) => { const token = await getToken(); if (!token) return; const result = await accountsApi.setDefault(id, token); if (result.error) { Alert.alert('Error', result.error.message); return; } selectAccount(id); await fetchAccounts(token); }; return ( {/* Existing Accounts */} YOUR ACCOUNTS {accounts.map((account) => ( {account.name} {account.email} {account.provider.toUpperCase()} • {account.syncEnabled ? 'Syncing' : 'Paused'} {!account.isDefault && ( handleSetDefault(account.id)} > )} {account.isDefault && } handleDeleteAccount(account.id)} > ))} {/* Add Account Form */} {showAddForm ? ( ADD IMAP ACCOUNT Display Name setFormData({ ...formData, name: text })} /> Email Address * setFormData({ ...formData, email: text })} keyboardType="email-address" autoCapitalize="none" /> IMAP Host * setFormData({ ...formData, imapHost: text })} autoCapitalize="none" /> Password * setFormData({ ...formData, password: text })} secureTextEntry /> setShowAddForm(false)} > Cancel {loading ? ( ) : ( Add Account )} ) : ( setShowAddForm(true)} > Add IMAP Account )} ); } const styles = StyleSheet.create({ container: { flex: 1, }, section: { marginTop: 24, }, sectionTitle: { fontSize: 12, fontWeight: '600', paddingHorizontal: 16, marginBottom: 8, }, accountItem: { flexDirection: 'row', alignItems: 'center', padding: 16, borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: 'rgba(128, 128, 128, 0.2)', }, iconContainer: { width: 48, height: 48, borderRadius: 12, justifyContent: 'center', alignItems: 'center', marginRight: 12, }, accountInfo: { flex: 1, }, accountName: { fontSize: 16, fontWeight: '600', }, accountEmail: { fontSize: 14, marginTop: 2, }, accountProvider: { fontSize: 12, marginTop: 4, }, accountActions: { flexDirection: 'row', }, actionButton: { padding: 8, }, addButton: { flexDirection: 'row', alignItems: 'center', padding: 16, marginHorizontal: 16, borderRadius: 12, }, addButtonText: { fontSize: 16, fontWeight: '500', }, formContainer: { margin: 16, padding: 16, borderRadius: 12, }, inputGroup: { marginBottom: 16, }, inputLabel: { fontSize: 14, fontWeight: '500', marginBottom: 8, }, input: { borderWidth: 1, borderRadius: 8, paddingHorizontal: 12, paddingVertical: 10, fontSize: 16, }, formActions: { flexDirection: 'row', marginTop: 8, }, cancelButton: { flex: 1, paddingVertical: 12, borderWidth: 1, borderRadius: 8, alignItems: 'center', marginRight: 8, }, cancelButtonText: { fontSize: 16, fontWeight: '500', }, submitButton: { flex: 1, paddingVertical: 12, borderRadius: 8, alignItems: 'center', marginLeft: 8, }, submitButtonText: { color: '#fff', fontSize: 16, fontWeight: '500', }, });