Skip to content

Commit 40db954

Browse files
committed
fix: update epoch card
1 parent 3b7ec0d commit 40db954

File tree

5 files changed

+143
-15
lines changed

5 files changed

+143
-15
lines changed

apps/namadillo/src/App/Setup/IndexerLoader.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ export const IndexerLoader = ({
1111
const indexerUrl = useAtomValue(indexerUrlAtom);
1212
const cycleTomlIndexer = useAtomValue(cycleTomlIndexerAtom);
1313
const cycleTomlRpc = useAtomValue(cycleTomlRpcAtom);
14-
console.log("cycleTomlIndexer", cycleTomlIndexer);
15-
console.log("indexerUrl", indexerUrl);
16-
console.log("cycleTomlRpc", cycleTomlRpc);
1714

1815
// if (!indexerUrl) {
1916
// return <Setup />;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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;

apps/namadillo/src/App/SplashPage/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { BigNumber } from "bignumber.js";
88
import { useAtomValue } from "jotai";
99
import { useMemo } from "react";
1010
import validityOpsLogo from "./assets/validitylogo.png";
11+
import EpochCard from "./EpochCard";
1112

1213
const ValidatorSplashPage = (): JSX.Element => {
1314
const validators = useAtomValue(allValidatorsAtom);
@@ -128,14 +129,7 @@ const ValidatorSplashPage = (): JSX.Element => {
128129
{Number(commission.multipliedBy(100).toFixed(2))}%
129130
</p>
130131
</div>
131-
<div className="p-6 bg-[#261b51] border-4 border-[#3f65a3] rounded-lg">
132-
<h2 className="text-xl font-semibold text-[#3f65a3]">
133-
Current Epoch
134-
</h2>
135-
<p className="text-3xl font-bold text-[#48b9d2] mt-4">
136-
{chainStatus?.epoch || "-"}
137-
</p>
138-
</div>
132+
<EpochCard />
139133
</div>
140134
<div className="mb-2">
141135
<StakingSummary />

apps/namadillo/src/atoms/settings/atoms.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ export const cycleTomlRpcAtom = atomWithQuery((get) => {
157157
retry: false,
158158
queryFn: async () => {
159159
for (const url of tomlRpcs) {
160-
console.log("Checking URL:", url);
161160
try {
162161
const response = await axios.get(url);
163-
console.log("HTTP status:", response.status);
164162
if (response.status === 200) return url; // Return the first healthy URL
165163
} catch (error) {
166164
console.log("Error or non-200 status for", url, error);
@@ -179,10 +177,8 @@ export const cycleTomlIndexerAtom = atomWithQuery((get) => {
179177
retry: false,
180178
queryFn: async () => {
181179
for (const url of tomlIndexerUrls) {
182-
console.log("Checking URL:", url);
183180
try {
184181
const response = await axios.get(`${url}/api/v1/pos/validator/all`);
185-
console.log("HTTP status:", response.status);
186182
if (response.status === 200) {
187183
return url; // First healthy URL
188184
}

apps/namadillo/vite.config.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import tsconfigPaths from "vite-tsconfig-paths";
88
export default defineConfig(() => {
99
return {
1010
server: {
11+
proxy: {
12+
"/namada": {
13+
target: "https://explorer75.org",
14+
changeOrigin: true,
15+
secure: false,
16+
},
17+
},
1118
headers: {
1219
"Cross-Origin-Opener-Policy": "same-origin",
1320
"Cross-Origin-Embedder-Policy": "require-corp",

0 commit comments

Comments
 (0)