|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { baseUrl } from "../lib/utils"; |
| 3 | + |
| 4 | +interface PyPIReleaseProps { |
| 5 | + initialVersion: string | null; |
| 6 | + initialReleaseDate: string | null; |
| 7 | +} |
| 8 | + |
| 9 | +export default function PyPIRelease({ |
| 10 | + initialVersion, |
| 11 | + initialReleaseDate, |
| 12 | +}: PyPIReleaseProps) { |
| 13 | + const [version, setVersion] = useState<string | null>(null); |
| 14 | + const [releaseDate, setReleaseDate] = useState<string | null>(null); |
| 15 | + const [isLoading, setIsLoading] = useState(true); |
| 16 | + const [dataSource, setDataSource] = useState<"static" | "live">("static"); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + // Step 1: Initialize from static JSON file |
| 20 | + const initializeFromJSON = async () => { |
| 21 | + try { |
| 22 | + console.log( |
| 23 | + "[Build Data] 🔄 Initializing PyPI data from static JSON file..." |
| 24 | + ); |
| 25 | + const response = await fetch(baseUrl("build-data-pypi.json")); |
| 26 | + if (response.ok) { |
| 27 | + const data = await response.json(); |
| 28 | + if (data.version) { |
| 29 | + setVersion(data.version); |
| 30 | + setReleaseDate(data.releaseDate); |
| 31 | + setDataSource("static"); |
| 32 | + setIsLoading(false); |
| 33 | + console.log( |
| 34 | + `[Build Data] ✓ Initialized from static JSON: v${data.version} (${ |
| 35 | + data.releaseDate || "date unknown" |
| 36 | + })` |
| 37 | + ); |
| 38 | + return true; |
| 39 | + } |
| 40 | + } |
| 41 | + } catch (error) { |
| 42 | + console.error( |
| 43 | + "[Build Data] ✗ Failed to initialize from static JSON:", |
| 44 | + error |
| 45 | + ); |
| 46 | + } |
| 47 | + setIsLoading(false); |
| 48 | + return false; |
| 49 | + }; |
| 50 | + |
| 51 | + // Step 2: Try to update with direct PyPI API call |
| 52 | + const updateFromPyPI = async () => { |
| 53 | + setIsLoading(true); |
| 54 | + console.log( |
| 55 | + "[PyPI API] 🔄 Attempting to update PyPI data from PyPI API..." |
| 56 | + ); |
| 57 | + |
| 58 | + try { |
| 59 | + const response = await fetch("https://pypi.org/pypi/ipython/json"); |
| 60 | + |
| 61 | + if (response.ok) { |
| 62 | + const data = await response.json(); |
| 63 | + const newVersion = data.info.version; |
| 64 | + const releases = data.releases[newVersion]; |
| 65 | + let newReleaseDate: string | null = null; |
| 66 | + |
| 67 | + if (releases && releases.length > 0) { |
| 68 | + const latestRelease = releases[releases.length - 1]; |
| 69 | + if (latestRelease.upload_time) { |
| 70 | + const date = new Date(latestRelease.upload_time); |
| 71 | + newReleaseDate = date.toLocaleDateString("en-US", { |
| 72 | + year: "numeric", |
| 73 | + month: "long", |
| 74 | + day: "numeric", |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + setVersion(newVersion); |
| 80 | + setReleaseDate(newReleaseDate); |
| 81 | + setDataSource("live"); |
| 82 | + console.log( |
| 83 | + `[PyPI API] ✓ Updated from PyPI: v${newVersion} (${ |
| 84 | + newReleaseDate || "date unknown" |
| 85 | + })` |
| 86 | + ); |
| 87 | + } else { |
| 88 | + console.error( |
| 89 | + `[PyPI API] ✗ Could not reach PyPI API: ${response.status} ${response.statusText}` |
| 90 | + ); |
| 91 | + // Don't change dataSource - keep it as 'static' since update failed |
| 92 | + } |
| 93 | + } catch (error) { |
| 94 | + console.error("[PyPI API] ✗ Could not reach PyPI API:", error); |
| 95 | + // Don't change dataSource - keep it as 'static' since update failed |
| 96 | + } finally { |
| 97 | + setIsLoading(false); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + // Initialize from JSON, then try to update from PyPI |
| 102 | + initializeFromJSON().then((initialized) => { |
| 103 | + if (initialized) { |
| 104 | + // Only try to update if we successfully initialized |
| 105 | + setTimeout(updateFromPyPI, 100); |
| 106 | + } |
| 107 | + }); |
| 108 | + }, []); |
| 109 | + |
| 110 | + if (!version) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + return ( |
| 115 | + <div className="gradient-stats-green-cyan rounded-lg p-6 border border-theme-accent/20"> |
| 116 | + <div className="flex items-center justify-between"> |
| 117 | + <div> |
| 118 | + <p className="text-sm text-gray-600 dark:text-gray-400 mb-1"> |
| 119 | + Latest Release |
| 120 | + {dataSource === "static" && ( |
| 121 | + <span className="ml-2 text-xs opacity-75">(static)</span> |
| 122 | + )} |
| 123 | + {dataSource === "live" && ( |
| 124 | + <span className="ml-2 text-xs opacity-75">(live)</span> |
| 125 | + )} |
| 126 | + {isLoading && ( |
| 127 | + <span className="ml-2 text-xs opacity-75">(updating...)</span> |
| 128 | + )} |
| 129 | + </p> |
| 130 | + <p className="text-2xl font-bold text-gray-900 dark:text-white mb-1"> |
| 131 | + v{version} |
| 132 | + </p> |
| 133 | + {releaseDate && ( |
| 134 | + <p className="text-sm text-gray-600 dark:text-gray-400"> |
| 135 | + Released {releaseDate} |
| 136 | + </p> |
| 137 | + )} |
| 138 | + </div> |
| 139 | + <a |
| 140 | + href="https://pypi.org/project/ipython/" |
| 141 | + target="_blank" |
| 142 | + rel="noopener noreferrer" |
| 143 | + className="text-theme-secondary hover:text-theme-primary transition-colors" |
| 144 | + > |
| 145 | + <svg |
| 146 | + className="w-8 h-8" |
| 147 | + fill="none" |
| 148 | + stroke="currentColor" |
| 149 | + viewBox="0 0 24 24" |
| 150 | + > |
| 151 | + <path |
| 152 | + strokeLinecap="round" |
| 153 | + strokeLinejoin="round" |
| 154 | + strokeWidth="2" |
| 155 | + d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" |
| 156 | + ></path> |
| 157 | + </svg> |
| 158 | + </a> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +} |
0 commit comments