import { useState } from 'react'; import { Redirect, router } from 'expo-router'; import { ActivityIndicator, Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'; import { useAuth } from '@/src/providers/auth-provider'; export default function TenantSelectScreen() { const { token, tenants, activeTenantId, requiresTenantSelection, switchTenant } = useAuth(); const [switchingTenantId, setSwitchingTenantId] = useState(null); const [error, setError] = useState(null); 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); } } return ( Tenant auswaehlen Bitte waehle den Tenant fuer deine Session. {error ? {error} : null} {tenants.map((tenant) => { const tenantId = Number(tenant.id); const isBusy = switchingTenantId === tenantId; return ( onSelectTenant(tenantId)} disabled={switchingTenantId !== null}> {tenant.name} ID: {tenantId} {isBusy ? : Auswaehlen} ); })} ); } const styles = StyleSheet.create({ container: { flexGrow: 1, padding: 16, gap: 10, backgroundColor: '#f9fafb', }, title: { fontSize: 24, fontWeight: '700', color: '#111827', }, subtitle: { color: '#6b7280', marginBottom: 8, }, error: { color: '#dc2626', marginBottom: 8, }, tenantButton: { borderRadius: 12, padding: 14, borderWidth: 1, borderColor: '#d1d5db', backgroundColor: '#ffffff', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, tenantButtonDisabled: { opacity: 0.6, }, tenantName: { fontSize: 16, fontWeight: '600', color: '#111827', }, tenantMeta: { color: '#6b7280', marginTop: 4, }, tenantAction: { color: '#2563eb', fontWeight: '600', }, });