import React, { useState, useEffect } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { Organization, Project, Player } from '../types'; import { Icons } from '../components/IconSet'; import { dbService } from '../services/DatabaseService'; const CityProfile: React.FC = () => { const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); const [city, setCity] = useState(null); const backLabel = 'Zurück zu Städte'; const onSelectPlayer = (playerId: string) => navigate(`/players/${playerId}`); const onSelectProject = (projectId: string) => navigate(`/projects/${projectId}`); const [activeTab, setActiveTab] = useState<'overview' | 'residents' | 'ventures'>('overview'); const [residents, setResidents] = useState([]); const [ventures, setVentures] = useState([]); const [cityStats, setCityStats] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { if (!id) { navigate('/cities'); return; } // Fetch fresh data from database on mount/reload const loadCityData = async () => { try { await dbService.fetchAll(); const cityData = dbService.getOrg(id); if (cityData) { setCity(cityData); } else { navigate('/cities'); return; } } catch (error) { console.warn('Failed to fetch fresh city data:', error); // Fallback to cached data const cityData = dbService.getOrg(id); if (cityData) { setCity(cityData); } else { navigate('/cities'); return; } } setLoading(false); }; loadCityData(); }, [id, navigate]); useEffect(() => { if (!city) return; const loadCityData = () => { // Load residents (players in this city) const allPlayers = dbService.getPlayers(); const cityResidents = allPlayers.filter(p => p.organizationId === city.id); setResidents(cityResidents); // Load ventures (projects in this city) const allProjects = dbService.getProjects(); const cityVentures = allProjects.filter(p => p.associatedOrgId === city.id); setVentures(cityVentures); // Calculate dynamic city statistics const stats = { citizens: cityResidents.length, businesses: cityVentures.length, activeBusinesses: cityVentures.filter(p => p.status === 'active').length, recruitingBusinesses: cityVentures.filter(p => p.hiring).length, // Merge with existing static stats ...city.cityStats }; setCityStats(stats); }; // Initial load loadCityData(); // Subscribe to updates const unsub = dbService.subscribe(loadCityData); return unsub; }, [city]); if (loading || !city) { return (
); } return (
{/* Hero Header */}
{city.name}
{city.establishedYear && (
GEGR. {city.establishedYear}
)}

{city.name}

{city.memberCount} Mitglieder
{city.status}
{city.mayor && (
Aktueller Anführer
{city.mayor}
)}
{/* Navigation Tabs */}
{/* Content Area */}
{activeTab === 'overview' && (

Über {city.name}

{city.description}

{city.gallery && city.gallery.length > 0 && (

Bildarchiv

{city.gallery.map((url, idx) => (
{`Gallery
))}
)}
{city.cityStats && (

Stadt-Statistiken

{/* Tax Rate */}
Steuersatz
{city.cityStats.taxRate}%
Pro Transaktion
{/* Defense */}
Verteidigungswert
{city.cityStats.defenseRating}/10
{/* Gov Type */}
Regierungsform
{city.cityStats.government}
{/* Biome */}
Biom {city.cityStats.biome}
Spezialität {city.cityStats.specialty}
)}
)} {activeTab === 'residents' && (
{residents.map(player => (
onSelectPlayer(player.uuid)} className="bg-surface border border-border p-4 rounded-xl flex items-center gap-4 hover:border-accentInfo/50 transition-colors cursor-pointer group hover:shadow-card hover:bg-surfaceHighlight/30" >
{player.username.charAt(0)}
{player.username}
{player.stats.role}
))} {residents.length === 0 &&
Keine Bewohner öffentlich registriert.
}
)} {activeTab === 'ventures' && (
{ventures.map(project => (
onSelectProject(project.id)} className="bg-surface border border-border rounded-xl p-5 hover:border-accentInfo/50 transition-all cursor-pointer group hover:shadow-card hover:bg-surfaceHighlight/30" >
{project.category} {project.hiring && ( STELLEN )}

{project.title}

{project.description}

Geführt von {project.owner}
{project.progress}% Fortschr.
))} {ventures.length === 0 &&
Keine aktiven Unternehmen in dieser Organisation.
}
)}
); }; export default CityProfile;