import { useMemo, useState } from 'react'; import { Redirect, router } from 'expo-router'; import { ActivityIndicator, Pressable, ScrollView, StyleSheet, Text, TextInput, View } from 'react-native'; import { useAuth, useTokenStorageInfo } from '@/src/providers/auth-provider'; const PRIMARY = '#69c350'; export default function TenantSelectScreen() { const { token, tenants, activeTenantId, requiresTenantSelection, switchTenant, user, logout } = useAuth(); const storageInfo = useTokenStorageInfo(); const [switchingTenantId, setSwitchingTenantId] = useState(null); const [search, setSearch] = useState(''); const [error, setError] = useState(null); const filteredTenants = useMemo(() => { const terms = search.trim().toLowerCase().split(/\s+/).filter(Boolean); if (terms.length === 0) return tenants; return tenants.filter((tenant) => { const haystack = `${String(tenant.name || '').toLowerCase()} ${String(tenant.id || '').toLowerCase()} ${ String(tenant.short || '').toLowerCase() }`; return terms.every((term) => haystack.includes(term)); }); }, [search, tenants]); if (!token) { return ; } if (!requiresTenantSelection && activeTenantId) { return ; } async function onSelectTenant(tenantId: number) { setSwitchingTenantId(tenantId); setError(null); try { await switchTenant(tenantId); router.replace('/(tabs)'); } catch (err) { setError(err instanceof Error ? err.message : 'Tenant konnte nicht gewechselt werden.'); } finally { setSwitchingTenantId(null); } } async function onLogout() { setSwitchingTenantId(null); await logout(); router.replace('/login'); } return ( Tenant auswählen Wähle den Mandanten für diese Session. User: {String(user?.email || user?.id || 'unbekannt')} Storage: {storageInfo.mode} {error ? {error} : null} {filteredTenants.length === 0 ? Keine passenden Tenants gefunden. : null} {filteredTenants.map((tenant) => { const tenantId = Number(tenant.id); const isBusy = switchingTenantId === tenantId; return ( onSelectTenant(tenantId)} disabled={switchingTenantId !== null}> {tenant.name} ID: {tenantId} {isBusy ? : Auswählen} ); })} Anderen Nutzer anmelden ); } const styles = StyleSheet.create({ container: { flexGrow: 1, padding: 16, gap: 12, backgroundColor: '#f9fafb', }, headerCard: { borderRadius: 12, borderWidth: 1, borderColor: '#e5e7eb', backgroundColor: '#ffffff', padding: 14, gap: 4, }, title: { fontSize: 22, fontWeight: '700', color: '#111827', }, subtitle: { color: '#6b7280', marginBottom: 2, }, meta: { color: '#6b7280', fontSize: 12, }, error: { color: '#dc2626', fontSize: 13, }, searchInput: { borderWidth: 1, borderColor: '#d1d5db', borderRadius: 10, paddingHorizontal: 12, paddingVertical: 10, fontSize: 15, color: '#111827', backgroundColor: '#ffffff', }, tenantButton: { borderRadius: 12, padding: 14, borderWidth: 1, borderColor: '#d1d5db', backgroundColor: '#ffffff', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, tenantButtonDisabled: { opacity: 0.6, }, tenantInfo: { flex: 1, minWidth: 0, paddingRight: 10, }, tenantName: { fontSize: 16, fontWeight: '600', color: '#111827', }, tenantMeta: { color: '#6b7280', marginTop: 4, }, tenantAction: { color: PRIMARY, fontWeight: '600', }, empty: { color: '#6b7280', fontSize: 13, textAlign: 'center', paddingVertical: 8, }, logoutButton: { marginTop: 4, minHeight: 42, borderRadius: 10, borderWidth: 1, borderColor: '#d1d5db', backgroundColor: '#ffffff', alignItems: 'center', justifyContent: 'center', }, logoutText: { color: '#374151', fontWeight: '600', }, });