|
| 1 | +import { chainStatusAtom } from "atoms/chain"; |
| 2 | +import { useAtomValue } from "jotai"; |
| 3 | +import React, { useEffect, useState } from "react"; |
| 4 | + |
| 5 | +// Defines the structure of the time breakdown (days, hours, minutes, seconds). |
| 6 | +interface TimeParts { |
| 7 | + days: number; |
| 8 | + hours: number; |
| 9 | + minutes: number; |
| 10 | + seconds: number; |
| 11 | +} |
| 12 | + |
| 13 | +// Describes the portion of the JSON containing epoch-related info. |
| 14 | +interface HeightInfo { |
| 15 | + epoch?: number; |
| 16 | + epochProgress?: string; |
| 17 | + epochEndTime?: string; |
| 18 | +} |
| 19 | + |
| 20 | +// Main structure of the fetched JSON chunk. |
| 21 | +interface NamadaEventsData { |
| 22 | + heightInfo?: HeightInfo; |
| 23 | +} |
| 24 | + |
| 25 | +const SSE_URL = "https://explorer75.org/namada/events-root"; |
| 26 | +const DEFAULT_TIME_LEFT = "--:--:--"; |
| 27 | + |
| 28 | +const EpochCard: React.FC = () => { |
| 29 | + // If you still want to use chainStatus for some fallback: |
| 30 | + const chainStatus = useAtomValue(chainStatusAtom); |
| 31 | + |
| 32 | + const [epoch, setEpoch] = useState<string>( |
| 33 | + chainStatus?.epoch?.toString() || "-" |
| 34 | + ); |
| 35 | + const [timeLeft, setTimeLeft] = useState<string>(DEFAULT_TIME_LEFT); |
| 36 | + const [progress, setProgress] = useState<number>(0); |
| 37 | + |
| 38 | + // parse an ISO date string into a JS Date |
| 39 | + const parseISOTime = (isoString: string): Date => new Date(isoString); |
| 40 | + |
| 41 | + // Returns days/hours/mins/secs until futureDate |
| 42 | + const getTimeRemaining = (futureDate: Date): TimeParts => { |
| 43 | + const total = futureDate.getTime() - Date.now(); |
| 44 | + if (total <= 0) { |
| 45 | + return { days: 0, hours: 0, minutes: 0, seconds: 0 }; |
| 46 | + } |
| 47 | + return { |
| 48 | + days: Math.floor(total / (1000 * 60 * 60 * 24)), |
| 49 | + hours: Math.floor((total / (1000 * 60 * 60)) % 24), |
| 50 | + minutes: Math.floor((total / (1000 * 60)) % 60), |
| 51 | + seconds: Math.floor((total / 1000) % 60), |
| 52 | + }; |
| 53 | + }; |
| 54 | + |
| 55 | + // Formats days/hours/mins/secs into a string |
| 56 | + const formatTime = (timeParts: TimeParts): string => { |
| 57 | + const { days, hours, minutes, seconds } = timeParts; |
| 58 | + const dStr = days > 0 ? `${days}d ` : ""; |
| 59 | + const hStr = hours < 10 ? `0${hours}` : hours.toString(); |
| 60 | + const mStr = minutes < 10 ? `0${minutes}` : minutes.toString(); |
| 61 | + const sStr = seconds < 10 ? `0${seconds}` : seconds.toString(); |
| 62 | + return `${dStr}${hStr}:${mStr}:${sStr}`; |
| 63 | + }; |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + // Create the SSE connection |
| 67 | + const eventSource = new EventSource(SSE_URL); |
| 68 | + |
| 69 | + eventSource.onmessage = (event) => { |
| 70 | + // Each chunk looks like `data: {"heightInfo":{"epoch":...}}` |
| 71 | + // so event.data is the JSON after the "data: " prefix |
| 72 | + try { |
| 73 | + const data: NamadaEventsData = JSON.parse(event.data); |
| 74 | + const heightInfo = data.heightInfo; |
| 75 | + if (heightInfo) { |
| 76 | + if (heightInfo.epoch !== undefined) { |
| 77 | + setEpoch(heightInfo.epoch.toString()); |
| 78 | + } |
| 79 | + if (heightInfo.epochEndTime && heightInfo.epochProgress) { |
| 80 | + // Calculate time left |
| 81 | + const endDate = parseISOTime(heightInfo.epochEndTime); |
| 82 | + const remainingParts = getTimeRemaining(endDate); |
| 83 | + setTimeLeft(formatTime(remainingParts)); |
| 84 | + |
| 85 | + // epochProgress might be something like "83.2"—just store it as a number |
| 86 | + const progressVal = parseFloat(heightInfo.epochProgress); |
| 87 | + setProgress(isNaN(progressVal) ? 0 : progressVal); |
| 88 | + } |
| 89 | + } |
| 90 | + } catch (err) { |
| 91 | + console.error("Error parsing SSE chunk:", err); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + eventSource.onerror = (err) => { |
| 96 | + // This will fire if there's an error with the SSE stream |
| 97 | + console.error("SSE error:", err); |
| 98 | + }; |
| 99 | + |
| 100 | + // Cleanup: close stream on unmount |
| 101 | + return () => { |
| 102 | + eventSource.close(); |
| 103 | + }; |
| 104 | + }, []); |
| 105 | + |
| 106 | + return ( |
| 107 | + <div className="p-6 bg-[#261b51] border-4 border-[#3f65a3] rounded-lg max-w-sm"> |
| 108 | + <h2 className="text-xl font-semibold text-[#3f65a3]">Current Epoch</h2> |
| 109 | + |
| 110 | + {timeLeft !== DEFAULT_TIME_LEFT && ( |
| 111 | + <> |
| 112 | + <div className="flex justify-between"> |
| 113 | + <p className="text-3xl font-bold text-[#48b9d2] mt-4">{epoch}</p> |
| 114 | + <div className="mt-4 text-white"> |
| 115 | + <p className="text-lg font-semibold">Next Epoch:</p> |
| 116 | + <p className="text-2xl font-semibold">{timeLeft}</p> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + <div className="relative w-full h-3 bg-[#3f65a3] rounded-md mt-4"> |
| 120 | + <div |
| 121 | + className="absolute h-3 bg-[#48b9d2] rounded-md transition-all duration-500" |
| 122 | + style={{ width: `${progress}%` }} |
| 123 | + /> |
| 124 | + </div> |
| 125 | + <p className="text-xs text-white mt-2 text-right"> |
| 126 | + {progress.toFixed(1)}% Complete |
| 127 | + </p> |
| 128 | + </> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +export default EpochCard; |
0 commit comments