import React, { useState, useEffect } from 'react'; import './App.css'; import Scene3D from './components/Scene3D'; import LocationSelector from './components/LocationSelector'; import { weatherService } from './services/weatherService'; function App() { const [weatherData, setWeatherData] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [currentLocationName, setCurrentLocationName] = useState(''); const [isPortalMode, setIsPortalMode] = useState(false); const [exitPortalFunction, setExitPortalFunction] = useState(null); const [portalWeatherData, setPortalWeatherData] = useState(null); const [errorSearchQuery, setErrorSearchQuery] = useState(''); useEffect(() => { loadCurrentLocationWeather(); }, []); const loadCurrentLocationWeather = async () => { setIsLoading(true); setError(null); try { const location = await weatherService.getCurrentLocation(); const data = await weatherService.getCurrentWeather(location); setWeatherData(data); setCurrentLocationName(`${data.location.name}, ${data.location.region}`); } catch (error) { console.error('Error loading weather:', error); setError('Unable to load weather data. Please try entering a city manually.'); } finally { setIsLoading(false); } }; const handleLocationChange = async (location) => { setIsLoading(true); setError(null); try { const data = await weatherService.getCurrentWeather(location); setWeatherData(data); setCurrentLocationName(`${data.location.name}, ${data.location.region}`); } catch (error) { console.error('Error loading weather for location:', error); setError('Unable to load weather data for this location. Please try a different city.'); } finally { setIsLoading(false); } }; const isNightTime = () => { if (!weatherData?.location?.localtime) return false; const localTime = weatherData.location.localtime; const currentHour = new Date(localTime).getHours(); return currentHour >= 19 || currentHour <= 6; }; const handlePortalWeatherDataChange = (data) => { setPortalWeatherData(data); }; const handleErrorSearch = async (e) => { e.preventDefault(); if (errorSearchQuery.trim()) { await handleLocationChange(errorSearchQuery.trim()); setErrorSearchQuery(''); } }; // Use portal weather data when in portal mode, otherwise use main weather data const displayWeatherData = isPortalMode && portalWeatherData ? portalWeatherData : weatherData; const isNight = isNightTime(); const textColor = (isPortalMode || !isNight) ? 'text-black' : 'text-white'; return (
Loading weather data...
{error}
{/* Search form for manual city entry */} {/* Buttons */}