import React, { useState, useEffect } from 'react'; import { View, Text, TouchableOpacity, ActivityIndicator } from 'react-native'; import { FontAwesome5 } from '@expo/vector-icons'; import { useRouter } from 'expo-router'; import { supabase } from '../utils/supabase'; import { Session } from '@supabase/supabase-js'; import { useTheme, lightColors, darkColors } from '../utils/themeContext'; export default function DashboardStats() { const router = useRouter(); const { isDarkMode } = useTheme(); const [session, setSession] = useState(null); const [loading, setLoading] = useState(true); const [teamCount, setTeamCount] = useState(0); const [orgCount, setOrgCount] = useState(0); const [availableMana, setAvailableMana] = useState(0); const [totalMana, setTotalMana] = useState(0); useEffect(() => { // Prüfe den aktuellen Authentifizierungsstatus supabase.auth.getSession().then(({ data: { session } }) => { setSession(session); if (session) { fetchUserStats(session.user.id); } else { setLoading(false); } }); // Abonniere Authentifizierungsänderungen const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => { setSession(session); if (session) { fetchUserStats(session.user.id); } else { setLoading(false); } }); return () => subscription.unsubscribe(); }, []); async function fetchUserStats(userId: string) { try { setLoading(true); // Hole alle Teams, in denen der Benutzer Mitglied ist const { data: teamMembers, error: teamMembersError } = await supabase .from('team_members') .select('team_id') .eq('user_id', userId); if (teamMembersError) throw teamMembersError; setTeamCount(teamMembers?.length || 0); // Hole alle Organisationen, in denen der Benutzer eine Rolle hat const { data: userRoles, error: userRolesError } = await supabase .from('user_roles') .select('organization_id') .eq('user_id', userId) .not('organization_id', 'is', null); if (userRolesError) throw userRolesError; // Entferne Duplikate (falls der Benutzer mehrere Rollen in einer Organisation hat) const uniqueOrgIds = [...new Set(userRoles?.map(role => role.organization_id) || [])]; setOrgCount(uniqueOrgIds.length); // Hole die Mana-Informationen aus dem Profil des Benutzers const { data: profileData, error: profileError } = await supabase .from('profiles') .select('individual_quota, individual_usage') .eq('id', userId) .single(); if (profileError) throw profileError; if (profileData) { const quota = profileData.individual_quota || 0; const usage = profileData.individual_usage || 0; const available = Math.max(0, quota - usage); setTotalMana(quota); setAvailableMana(available); } } catch (error) { console.error('Fehler beim Abrufen der Benutzerstatistiken:', error); } finally { setLoading(false); } } if (!session || loading) { return ( ); } return ( {/* Mana-Anzeige */} router.push('/get-mana')} > Verfügbares Mana 0 ? `${Math.min(100, (availableMana / totalMana) * 100)}%` : '0%', backgroundColor: isDarkMode ? darkColors.primary : lightColors.primary }} /> Verfügbar: {availableMana} Gesamt: {totalMana} {/* Teams und Organisationen */} router.push('/teams')} > Teams {teamCount} router.push('/organizations')} > Organisationen {orgCount} ); }