Skip to content

Commit

Permalink
Merge pull request #2188 from zeitgeistpm/fix-cms-data-glitch
Browse files Browse the repository at this point in the history
Improve market metadata overides from glitching on load
  • Loading branch information
yornaath authored Jan 18, 2024
2 parents 3256d03 + 61180a2 commit f4ee5da
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
34 changes: 24 additions & 10 deletions lib/hooks/queries/useInfiniteMarkets.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { useInfiniteQuery, useQueryClient } from "@tanstack/react-query";
import { IndexerContext, isIndexedSdk, Market } from "@zeitgeistpm/sdk";
import { MarketOrderByInput, MarketWhereInput } from "@zeitgeistpm/indexer";
import { getOutcomesForMarkets } from "lib/gql/markets-list/outcomes-for-markets";
import { getCurrentPrediction } from "lib/util/assets";
import {
MarketOrderByInput,
MarketStatus,
MarketWhereInput,
ScoringRule,
} from "@zeitgeistpm/indexer";
import { IndexerContext, Market, isIndexedSdk } from "@zeitgeistpm/sdk";
import { hiddenMarketIds } from "lib/constants/markets";
import {
MarketsListFiltersQuery,
MarketsOrderBy,
} from "lib/types/market-filter";
import { marketsRootQuery } from "./useMarket";
import { useSdkv2 } from "../useSdkv2";
import { MarketOutcomes } from "lib/types/markets";
import { MarketStatus } from "@zeitgeistpm/indexer";
import { hiddenMarketIds } from "lib/constants/markets";
import { getCurrentPrediction } from "lib/util/assets";
import { useSdkv2 } from "../useSdkv2";

import { marketMetaFilter } from "./constants";
import { ScoringRule } from "@zeitgeistpm/indexer";
import { marketsRootQuery } from "./useMarket";
import { marketCmsDatakeyForMarket } from "./cms/useMarketCmsMetadata";
import { CmsMarketMetadata } from "lib/cms/markets";

export const rootKey = "markets-filtered";

Expand All @@ -40,6 +45,7 @@ export const useInfiniteMarkets = (
filters?: MarketsListFiltersQuery,
) => {
const [sdk, id] = useSdkv2();
const queryClient = useQueryClient();

const limit = 12;
const fetcher = async ({
Expand Down Expand Up @@ -101,6 +107,14 @@ export const useInfiniteMarkets = (
order: orderByMap[orderBy] as MarketOrderByInput, //todo: fix this type once sdk updated,
});

for (const market of markets) {
const cmsData: CmsMarketMetadata | undefined = queryClient.getQueryData(
marketCmsDatakeyForMarket(market.marketId),
);
if (cmsData?.question) market.question = cmsData.question;
if (cmsData?.imageUrl) market.img = cmsData.imageUrl;
}

const resMarkets: Array<QueryMarketData> = markets.map((market) => {
const outcomes: MarketOutcomes = market.assets.map((asset, index) => {
return {
Expand All @@ -126,7 +140,6 @@ export const useInfiniteMarkets = (
};
};

const queryClient = useQueryClient();
const query = useInfiniteQuery({
queryKey: [id, rootKey, filters, orderBy, withLiquidityOnly],
queryFn: fetcher,
Expand All @@ -149,5 +162,6 @@ export const useInfiniteMarkets = (
},
staleTime: 10_000,
});

return query;
};
5 changes: 2 additions & 3 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ export async function getStaticProps() {

for (const market of [...featuredMarkets, ...trendingMarkets]) {
const cmsData = marketsCmsData.find((m) => m.marketId === market.marketId);
if (cmsData?.imageUrl) {
market.img = cmsData.imageUrl;
}
if (cmsData?.question) market.question = cmsData.question;
if (cmsData?.imageUrl) market.img = cmsData.imageUrl;
}

return {
Expand Down
15 changes: 3 additions & 12 deletions pages/markets/[marketid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,11 @@ export async function getStaticProps({ params }) {
let resolutionTimestamp: string | undefined;
if (market) {
const { timestamp } = await getResolutionTimestamp(client, market.marketId);

resolutionTimestamp = timestamp ?? undefined;
}

if (cmsMetadata?.imageUrl) {
market.img = cmsMetadata?.imageUrl;
}

if (cmsMetadata?.question) {
market.question = cmsMetadata?.question;
}

if (cmsMetadata?.description) {
market.description = cmsMetadata?.description;
if (cmsMetadata?.imageUrl) market.img = cmsMetadata?.imageUrl;
if (cmsMetadata?.question) market.question = cmsMetadata?.question;
if (cmsMetadata?.description) market.description = cmsMetadata?.description;
}

return {
Expand Down
22 changes: 18 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
Expand All @@ -18,16 +22,26 @@
"useDefineForClassFields": true,
"baseUrl": "./",
"incremental": true,
"downlevelIteration": true
"downlevelIteration": true,
"plugins": [
{
"name": "next"
}
]
},
"include": [
"next-env.d.ts",
"declarations.d.ts",
"**/*.ts",
"**/*.tsx",
"**/*.jsx",
"**/*.js"
"**/*.js",
".next/types/**/*.ts"
],
"filesGlobs": [],
"exclude": ["node_modules", "cypress", "scripts"]
"exclude": [
"node_modules",
"cypress",
"scripts"
]
}

0 comments on commit f4ee5da

Please sign in to comment.