|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useEffect, useState } from "react"; |
| 4 | +import { format } from "date-fns"; |
| 5 | +import { |
| 6 | + Calendar, |
| 7 | + Headphones, |
| 8 | + CloudRain, |
| 9 | + CloudSnow, |
| 10 | + CloudLightning, |
| 11 | + CloudDrizzle, |
| 12 | + Cloud, |
| 13 | + Sun, |
| 14 | +} from "lucide-react"; |
| 15 | + |
| 16 | +interface DeploymentInfo { |
| 17 | + createdAt: string; |
| 18 | + url: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface SongInfo { |
| 22 | + name: string; |
| 23 | + artist: string; |
| 24 | + url: string; |
| 25 | +} |
| 26 | + |
| 27 | +interface WeatherInfo { |
| 28 | + description: string; |
| 29 | + temperature: number; |
| 30 | + city: string; |
| 31 | +} |
| 32 | + |
| 33 | +export default function DeploymentStatus() { |
| 34 | + const [deploymentInfo, setDeploymentInfo] = useState<DeploymentInfo | null>( |
| 35 | + null |
| 36 | + ); |
| 37 | + const [songInfo, setSongInfo] = useState<SongInfo | null>(null); |
| 38 | + const [weatherInfo, setWeatherInfo] = useState<WeatherInfo | null>(null); |
| 39 | + const [isLoading, setIsLoading] = useState(true); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + const fetchData = async () => { |
| 43 | + try { |
| 44 | + setIsLoading(true); |
| 45 | + |
| 46 | + // Fetch deployment status |
| 47 | + const deploymentResponse = await fetch("/api/deployment-status"); |
| 48 | + if (deploymentResponse.ok) { |
| 49 | + const data = await deploymentResponse.json(); |
| 50 | + setDeploymentInfo(data.deploymentInfo); |
| 51 | + setWeatherInfo(data.weatherInfo); |
| 52 | + } |
| 53 | + |
| 54 | + // Fetch Spotify separately |
| 55 | + const spotifyResponse = await fetch("/api/spotify/now-playing"); |
| 56 | + if (spotifyResponse.ok) { |
| 57 | + const data = await spotifyResponse.json(); |
| 58 | + if (data.isPlaying && data.songInfo) { |
| 59 | + setSongInfo(data.songInfo); |
| 60 | + } |
| 61 | + } |
| 62 | + } catch (err) { |
| 63 | + console.error("Error fetching status data:", err); |
| 64 | + } finally { |
| 65 | + setIsLoading(false); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + fetchData(); |
| 70 | + }, []); |
| 71 | + |
| 72 | + if (isLoading) { |
| 73 | + return ( |
| 74 | + <div className="text-sm text-gray-500 dark:text-gray-400 mt-4 animate-pulse"> |
| 75 | + Loading latest site info... |
| 76 | + </div> |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + if (!deploymentInfo) { |
| 81 | + return null; // Don't show anything if there's no deployment info |
| 82 | + } |
| 83 | + |
| 84 | + // Format the deployment date |
| 85 | + const formattedDate = format( |
| 86 | + new Date(deploymentInfo.createdAt), |
| 87 | + "MMMM d, yyyy" |
| 88 | + ); |
| 89 | + const formattedTime = format(new Date(deploymentInfo.createdAt), "h:mm a"); |
| 90 | + |
| 91 | + // Get the appropriate weather icon based on the description |
| 92 | + const getWeatherIcon = (description: string) => { |
| 93 | + const desc = description.toLowerCase(); |
| 94 | + if (desc.includes("rain") || desc.includes("shower")) |
| 95 | + return ( |
| 96 | + <CloudRain size={16} className="inline-block ml-1 text-blue-500" /> |
| 97 | + ); |
| 98 | + if (desc.includes("snow")) |
| 99 | + return ( |
| 100 | + <CloudSnow size={16} className="inline-block ml-1 text-blue-200" /> |
| 101 | + ); |
| 102 | + if (desc.includes("thunder") || desc.includes("lightning")) |
| 103 | + return ( |
| 104 | + <CloudLightning |
| 105 | + size={16} |
| 106 | + className="inline-block ml-1 text-yellow-500" |
| 107 | + /> |
| 108 | + ); |
| 109 | + if (desc.includes("drizzl")) |
| 110 | + return ( |
| 111 | + <CloudDrizzle size={16} className="inline-block ml-1 text-blue-400" /> |
| 112 | + ); |
| 113 | + if (desc.includes("cloud") || desc.includes("overcast")) |
| 114 | + return <Cloud size={16} className="inline-block ml-1 text-gray-400" />; |
| 115 | + if (desc.includes("mist") || desc.includes("fog")) |
| 116 | + return <Cloud size={16} className="inline-block ml-1 text-gray-300" />; |
| 117 | + if (desc.includes("clear") || desc.includes("sunny")) |
| 118 | + return <Sun size={16} className="inline-block ml-1 text-yellow-500" />; |
| 119 | + return <Cloud size={16} className="inline-block ml-1 text-gray-400" />; |
| 120 | + }; |
| 121 | + |
| 122 | + // Get a more relatable weather description |
| 123 | + const getWeatherDescription = (description: string) => { |
| 124 | + const desc = description.toLowerCase(); |
| 125 | + if (desc.includes("mist") || desc.includes("fog")) return "foggy"; |
| 126 | + if (desc.includes("rain") && desc.includes("heavy")) return "pouring"; |
| 127 | + if (desc.includes("rain")) return "rainy"; |
| 128 | + if (desc.includes("drizzl")) return "drizzling"; |
| 129 | + if (desc.includes("snow")) return "snowing"; |
| 130 | + if (desc.includes("thunder")) return "stormy"; |
| 131 | + if (desc.includes("cloud") || desc.includes("overcast")) return "cloudy"; |
| 132 | + if (desc.includes("clear") || desc.includes("sunny")) return "sunny"; |
| 133 | + return description.toLowerCase(); |
| 134 | + }; |
| 135 | + |
| 136 | + return ( |
| 137 | + <div className="text-sm text-gray-600 dark:text-gray-300 mt-4 leading-relaxed italic border-t border-gray-200 dark:border-gray-700 pt-4"> |
| 138 | + <div className="flex items-start"> |
| 139 | + <Calendar className="w-4 h-4 mt-0.5 text-green-600 dark:text-green-400 flex-shrink-0 mr-2" /> |
| 140 | + <span> |
| 141 | + This site was last deployed on {formattedDate} at {formattedTime} |
| 142 | + {songInfo && ( |
| 143 | + <span> |
| 144 | + {" while listening to "} |
| 145 | + <a |
| 146 | + href={songInfo.url} |
| 147 | + target="_blank" |
| 148 | + rel="noopener noreferrer" |
| 149 | + className="text-green-600 dark:text-green-400 hover:underline font-medium" |
| 150 | + > |
| 151 | + {songInfo.name} |
| 152 | + </a>{" "} |
| 153 | + <span className="whitespace-nowrap"> |
| 154 | + by {songInfo.artist}{" "} |
| 155 | + <Headphones size={14} className="inline-block -mt-0.5" /> |
| 156 | + </span> |
| 157 | + </span> |
| 158 | + )} |
| 159 | + {weatherInfo && ( |
| 160 | + <span> |
| 161 | + {songInfo ? "." : ","} The weather was{" "} |
| 162 | + {getWeatherDescription(weatherInfo.description)} |
| 163 | + {getWeatherIcon(weatherInfo.description)} |
| 164 | + {" at "} |
| 165 | + <span className="font-medium"> |
| 166 | + {Math.round(weatherInfo.temperature)}°C |
| 167 | + </span> |
| 168 | + {" in "} |
| 169 | + <span className="font-medium">{weatherInfo.city}</span> |
| 170 | + {"."} |
| 171 | + </span> |
| 172 | + )} |
| 173 | + </span> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + ); |
| 177 | +} |
0 commit comments