Skip to content

Commit 3920656

Browse files
committed
Improve caching mechanism for R1MintedLastEpoch, improve scrolling UX when changing a page, make the assigned space for the node alias shorter in Node component
1 parent 0fac644 commit 3920656

File tree

12 files changed

+128
-30
lines changed

12 files changed

+128
-30
lines changed

app/server-components/Accounts/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default async function List({
2626

2727
return (
2828
<div className="list-wrapper">
29-
<div className="list">
29+
<div id="list" className="list">
3030
<ListHeader useFixedWidthSmall>
3131
<div className="min-w-[260px]">Address</div>
3232
<div className="min-w-[188px]">Licenses Owned (ND / MND)</div>

app/server-components/Licenses/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default async function List({ licenses, currentPage }: { licenses: Licens
1616

1717
return (
1818
<div className="list-wrapper">
19-
<div className="list">
19+
<div id="list" className="list">
2020
<ListHeader>
2121
<div className="min-w-[244px]">License</div>
2222
<div className="min-w-[34px]">Type</div>

app/server-components/Nodes/Hero.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const CachedHeroContent = cache(async () => (
6464
widthClasses="min-[420px]:min-w-[330px] md:max-w-[350px]"
6565
/>
6666

67-
<Suspense fallback={<Skeleton className="min-h-[76px] w-full min-w-[300px] flex-1 rounded-xl md:max-w-[320px]" />}>
67+
<Suspense fallback={<Skeleton className="min-h-[76px] w-full min-w-[300px] flex-1 rounded-xl md:max-w-[340px]" />}>
6868
<CardHorizontal
6969
label={
7070
<div>
@@ -89,7 +89,7 @@ export default async function Hero({
8989
resourcesTotal: types.ResourcesTotal;
9090
}) {
9191
return (
92-
<div className="w-full">
92+
<div id="hero" className="w-full">
9393
<BorderedCard>
9494
<div className="card-title-big font-bold">Nodes</div>
9595

app/server-components/Nodes/List.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export default async function List({
1919
}) {
2020
return (
2121
<div className="list-wrapper">
22-
<div className="list">
22+
<div id="list" className="list">
2323
<ListHeader>
24-
<div className="min-w-[200px]">Alias</div>
24+
<div className="min-w-[130px]">Alias</div>
2525
<div className="min-w-[164px]">Addresses</div>
2626
<div className="min-w-[244px]">License</div>
2727
<div className="min-w-[112px]">Owner</div>

app/server-components/Nodes/NodeListNodeCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default async function NodeListNodeCard({
3434
return (
3535
<BorderedCard useCustomWrapper useFixedWidthLarge>
3636
<div className="row justify-between gap-3 py-2 md:py-3 lg:gap-6">
37-
<Link href={`${routePath.node}/${node.eth_addr}`} className="group min-w-[200px] py-3">
37+
<Link href={`${routePath.node}/${node.eth_addr}`} className="group min-w-[130px] py-3">
3838
<div className="row gap-1.5">
3939
<div className="overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium group-hover:text-primary lg:text-[15px]">
4040
{node.alias}

app/server-components/R1MintedLastEpoch.tsx

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,51 @@
11
import { getCurrentEpoch } from '@/config';
22
import { fetchR1MintedLastEpoch } from '@/lib/api/blockchain';
3-
import { cache } from 'react';
43
import { formatUnits } from 'viem';
4+
interface CacheEntry {
5+
value: string;
6+
epoch: number;
7+
timestamp: number;
8+
}
59

6-
let cachedValue: string | null = null;
7-
let cachedEpoch: number | null = null;
10+
let cacheEntry: CacheEntry | null = null;
11+
let fetchPromise: Promise<string> | null = null;
812

9-
const getCachedR1MintedLastEpoch = cache(async () => {
13+
const getCachedR1MintedLastEpoch = async (): Promise<string> => {
1014
const currentEpoch = getCurrentEpoch();
15+
const now = Date.now();
16+
17+
// Return cached value if still valid
18+
if (cacheEntry && cacheEntry.epoch === currentEpoch) {
19+
return cacheEntry.value;
20+
}
1121

12-
// Check if we have cached data for the current epoch
13-
if (cachedValue !== null && cachedEpoch === currentEpoch) {
14-
// console.log(`[R1MintedLastEpoch] using cached data for epoch ${currentEpoch}`);
15-
return cachedValue;
22+
// Prevent multiple concurrent fetches
23+
if (fetchPromise) {
24+
return fetchPromise;
1625
}
1726

18-
// console.log(`[R1MintedLastEpoch] fetching new data for epoch ${currentEpoch}`);
19-
const value = await fetchR1MintedLastEpoch();
20-
const valueString = value.toString();
27+
fetchPromise = (async () => {
28+
try {
29+
const value = await fetchR1MintedLastEpoch();
30+
const valueString = value.toString();
2131

22-
// console.log(`[R1MintedLastEpoch] fetched new data for epoch ${currentEpoch}, value: ${valueString}`);
32+
cacheEntry = {
33+
value: valueString,
34+
epoch: currentEpoch,
35+
timestamp: now,
36+
};
2337

24-
cachedValue = valueString;
25-
cachedEpoch = currentEpoch;
38+
return valueString;
39+
} catch (error) {
40+
console.error('[R1MintedLastEpoch] error', error);
41+
throw error;
42+
} finally {
43+
fetchPromise = null;
44+
}
45+
})();
2646

27-
return valueString;
28-
});
47+
return fetchPromise;
48+
};
2949

3050
export default async function R1MintedLastEpoch() {
3151
let value: bigint | undefined;

components/Hero/HeroEpochCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default function HeroEpochCard() {
4343
</div>
4444

4545
<div className="col gap-[5px]">
46-
<div className="text-[15px] font-medium leading-none text-slate-500">Time left</div>
46+
<div className="text-right text-[15px] font-medium leading-none text-slate-500 md:text-left">Time left</div>
4747
<div className="font-semibold leading-none">
4848
<EpochTimer />
4949
</div>

components/Nodes/ParamsPagination.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@ export default function ParamsPagination({ total }: { total: number }) {
1515
params.set('page', pageNumber.toString());
1616
replace(`${pathname}?${params.toString()}`);
1717

18-
window.scrollTo({
19-
top: 0,
20-
behavior: 'smooth',
21-
});
18+
const element = document.getElementById('list');
19+
20+
if (element) {
21+
element.scrollIntoView({
22+
behavior: 'auto',
23+
block: 'start',
24+
});
25+
} else {
26+
// Fallback to scrolling to top if element not found
27+
setTimeout(() => {
28+
window.scrollTo({
29+
top: 0,
30+
behavior: 'auto',
31+
});
32+
}, 0);
33+
}
2234
};
2335

2436
if (total === 1) {

lib/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export const search = cache(
136136

137137
try {
138138
response = await getActiveNodes(1, query);
139-
console.log('Response', response);
139+
console.log('getActiveNodes Response', response);
140140

141141
if (response.result.nodes) {
142142
Object.entries(response.result.nodes).forEach(([_ratio1Addr, node]) => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"build:testnet": "dotenv -e .env.production.testnet next build",
1212
"build:devnet": "dotenv -e .env.production.devnet next build",
1313
"build": "dotenv -e .env.production.mainnet next build",
14+
"build:all": "./scripts/build-all.sh",
1415
"start": "next start",
1516
"lint": "next lint"
1617
},

0 commit comments

Comments
 (0)