Skip to content

Commit 239c8c7

Browse files
committed
marktes list: prefetch next page
1 parent f49b087 commit 239c8c7

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

web/src/pages/index/+Page.tsx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import MarketsPagination from "@/components/Market/MarketsPagination";
44
import { PreviewCard } from "@/components/Market/PreviewCard";
55
import { getUsePoolHourDataSetsKey } from "@/hooks/chart/useChartData";
66
import { PoolHourDatasSets } from "@/hooks/chart/utils";
7-
import { UseMarketsProps, useMarkets } from "@/hooks/useMarkets";
7+
import {
8+
UseGraphMarketsParams,
9+
UseMarketsProps,
10+
getUseGraphMarketsKey,
11+
useGraphMarketsQueryFn,
12+
useMarkets,
13+
} from "@/hooks/useMarkets";
814
import useMarketsSearchParams from "@/hooks/useMarketsSearchParams";
915
import { useSortAndFilterResults } from "@/hooks/useSortAndFilterResults";
1016
import { Market } from "@/lib/market";
@@ -52,6 +58,47 @@ async function preLoadMarkets(markets: Market[], queryClient: QueryClient) {
5258
}
5359
}
5460

61+
function convertToGraphMarketsParams(params: UseMarketsProps): UseGraphMarketsParams {
62+
return {
63+
chainsList: params.chainsList || [],
64+
type: params.type || "",
65+
marketName: params.marketName || "",
66+
categoryList: params.categoryList || [],
67+
marketStatusList: params.marketStatusList || [],
68+
verificationStatusList: params.verificationStatusList || [],
69+
showConditionalMarkets: params.showConditionalMarkets,
70+
showMarketsWithRewards: params.showMarketsWithRewards,
71+
minLiquidity: params.minLiquidity,
72+
creator: params.creator || "",
73+
participant: params.participant || "",
74+
orderBy: params.orderBy,
75+
orderDirection: params.orderDirection,
76+
marketIds: params.marketIds,
77+
disabled: params.disabled,
78+
limit: params.limit,
79+
page: params.page,
80+
};
81+
}
82+
83+
async function preLoadNextPage(params: UseMarketsProps, queryClient: QueryClient) {
84+
try {
85+
const nextPageParams = convertToGraphMarketsParams({ ...params, page: (params.page || 1) + 1 });
86+
const queryKey = getUseGraphMarketsKey(nextPageParams);
87+
88+
// Check if next page data is already cached
89+
const cachedData = queryClient.getQueryData(queryKey);
90+
if (!cachedData) {
91+
// Prefetch next page data
92+
await queryClient.prefetchQuery({
93+
queryKey,
94+
queryFn: () => useGraphMarketsQueryFn(nextPageParams),
95+
});
96+
}
97+
} catch (error) {
98+
console.error("Failed to prefetch next page:", error);
99+
}
100+
}
101+
55102
function PageContent({ params }: { params: UseMarketsProps }) {
56103
const results = useMarkets(params);
57104
const {
@@ -66,6 +113,10 @@ function PageContent({ params }: { params: UseMarketsProps }) {
66113
preLoadMarkets(data.markets, queryClient);
67114
}, [data.markets, queryClient]);
68115

116+
useEffect(() => {
117+
preLoadNextPage(params, queryClient);
118+
}, [params.page, queryClient]);
119+
69120
return (
70121
<div>
71122
<div className="px-[24px] lg:px-[64px] py-[16px]">

0 commit comments

Comments
 (0)