import React from 'react'; import { jmapClient } from '../api/jmap-client'; import { View, Text, StyleSheet, SectionList, Pressable, TextInput, ActivityIndicator, RefreshControl, ScrollView, Alert, Share, } from 'react-native'; import * as FileSystem from 'expo-file-system/legacy'; import * as Sharing from 'expo-sharing'; import { SafeAreaView } from 'react-native-safe-area-context'; import { useNavigation } from '@react-navigation/native'; import { useBackWhileFocused } from '../lib/use-back-while-focused'; import { listBackStep } from '../lib/list-back'; import type { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { Search, Plus, UserCircle, X, Menu, Trash2, Tag, FolderInput, Upload, CheckSquare, Share2, } from '../components/icons'; import type { RootStackParamList } from '../navigation/types'; import type { ContactCard } from '../api/types'; import { useContactsStore, sortContactsByName, selectCreateTargetBookId, selectGroupMembers, selectUncategorized, type ContactCategory, } from '../stores/contacts-store'; import { getContactDisplayName, getContactSortName, isGroup, matchesContactSearch } from '../lib/contact-utils'; import { contactsToVCard } from '../lib/vcard'; import { ContactListRow, AddressBookPickerSheet, ContactImportSheet, TagAssignSheet, } from '../components/contacts'; import Dialog from '../components/Dialog'; import ComailServiceAccess from '../components/ComailServiceAccess'; import ContactsSidebarDrawer from '../components/contacts/ContactsSidebarDrawer'; import { useSettingsStore } from '../stores/settings-store'; import { useLocaleStore, type TranslateFn } from '../stores/locale-store'; import { spacing, radius, typography, componentSizes, type ThemePalette } from '../theme/tokens'; import { useColors } from '../theme/colors'; type Nav = NativeStackNavigationProp; interface Section { title: string; data: ContactCard[]; } /** Letter sections for contacts already in `sortContactsByName` order. */ function groupContacts(sorted: ContactCard[], byLastName: boolean): Section[] { const groups: Record = {}; for (const c of sorted) { // Sections follow the sort name, so with "sort by last name" Alice Smith // files under S. const name = getContactSortName(c, byLastName).trim(); // Any letter (Ä, É, Ł, 王…) gets its own section like the webmail; only // digits and symbols fall into "#". Hermes supports Unicode property escapes. const first = Array.from(name)[0] || '#'; const letter = first.toLocaleUpperCase(); const key = /\p{L}/u.test(letter) ? letter : '#'; if (!groups[key]) groups[key] = []; groups[key].push(c); } return Object.keys(groups) .sort((a, b) => { if (a === '#') return 1; if (b === '#') return -1; return a.localeCompare(b, undefined, { sensitivity: 'base' }); }) .map((title) => ({ title, data: groups[title] })); } function categoryTitle( category: ContactCategory, books: Array<{ id: string; name: string }>, groups: ContactCard[], t: TranslateFn, ): string { switch (category.type) { case 'all': return t('contacts.all_contacts', 'All Contacts'); case 'addressBook': return books.find((b) => b.id === category.addressBookId)?.name || t('contacts.address_books.address_book', 'Address Book'); case 'group': { const g = groups.find((c) => c.id === category.groupId); return (g && getContactDisplayName(g)) || t('contacts.group', 'Group'); } case 'keyword': return `#${category.keyword}`; case 'uncategorized': return t('contacts.no_category', 'No Category'); default: return t('contacts.title', 'Contacts'); } } interface ContactsScreenProps { /** Split view: open the contact in the detail pane instead of the stack. */ onOpenContact?: (contact: ContactCard) => void; /** The contact open in the detail pane. */ activeContactId?: string | null; } export default function ContactsScreen({ onOpenContact, activeContactId = null }: ContactsScreenProps = {}) { const c = useColors(); const styles = React.useMemo(() => makeStyles(c), [c]); const navigation = useNavigation