diff --git a/lib/hooks/useMarketImage.ts b/lib/hooks/useMarketImage.ts index e9daf9f85..b2f7a2ac3 100644 --- a/lib/hooks/useMarketImage.ts +++ b/lib/hooks/useMarketImage.ts @@ -8,8 +8,21 @@ export const useMarketImage = ( fallback?: string; }, ) => { - const tagIndex = market.tags ? market.marketId % market.tags.length : 0; - const pickedTag = market.tags?.[tagIndex]; + const cmsQuery = useMarketCmsMetadata(market.marketId); + const fallback = getFallbackImage(market.tags, market.marketId); + + return { + ...cmsQuery, + data: cmsQuery.data?.imageUrl ?? opts?.fallback ?? fallback, + }; +}; + +export const getFallbackImage = ( + marketTags: FullMarketFragment["tags"], + marketId: number, +) => { + const tagIndex = marketTags ? marketId % marketTags.length : 0; + const pickedTag = marketTags?.[tagIndex]; const tag = ( pickedTag && pickedTag in CATEGORY_IMAGES ? pickedTag : "untagged" @@ -17,12 +30,6 @@ export const useMarketImage = ( const category = CATEGORY_IMAGES[tag]; - const fallback = category[market.marketId % category.length]; - - const cmsQuery = useMarketCmsMetadata(market.marketId); - - return { - ...cmsQuery, - data: cmsQuery.data?.imageUrl ?? opts?.fallback ?? fallback, - }; + const fallback = category[marketId % category.length]; + return fallback; }; diff --git a/pages/api/og/generate.tsx b/pages/api/og/generate.tsx index 36ba8b406..0f685a206 100644 --- a/pages/api/og/generate.tsx +++ b/pages/api/og/generate.tsx @@ -1,5 +1,5 @@ import { ImageResponse } from "@vercel/og"; -import { isMarketImageBase64Encoded } from "lib/types/create-market"; +import { getFallbackImage } from "lib/hooks/useMarketImage"; import { formatNumberCompact } from "lib/util/format-compact"; import type { PageConfig } from "next"; import { NextRequest } from "next/server"; @@ -20,8 +20,14 @@ export default async function GenerateOgImage(request: NextRequest) { const marketId = searchParams.get("marketId"); - const url = request.nextUrl.clone(); - url.pathname = `/api/og/${marketId}`; + const marketUrl = request.nextUrl.clone(); + marketUrl.pathname = `/api/og/${marketId}`; + + const cmsUrl = `${ + marketUrl.origin + }/api/cms/market-metadata/batch?marketIds=${JSON.stringify([ + Number(marketId), + ])}`; const { market, @@ -29,32 +35,48 @@ export default async function GenerateOgImage(request: NextRequest) { prediction, ends, currencyMetadata, - }: MarketImageData = await fetch(url.href).then((r) => r.json()); + }: MarketImageData = await fetch(marketUrl.href).then((r) => r.json()); + + const cmsRes = await fetch(cmsUrl).then((r) => r.json()); + const cmsImageUrl = cmsRes[0]?.imageUrl; + const cmsQuestion = cmsRes[0]?.question; + + const fallbackImagePath = `../../../public${getFallbackImage( + market.tags, + Number(marketId), + )}`; - if (!market?.question) return; + const question = cmsQuestion ?? market?.question; - const questionClass = market.question.length > 90 ? "text-4xl" : "text-5xl"; + if (!question) return; - const [boldFont, regularFont, bg, zeitgeistBadge] = await Promise.all([ - fetch( - new URL( - "../../../public/fonts/inter/static/Inter-Bold.ttf", - import.meta.url, - ).href, - ).then((res) => res.arrayBuffer()), - fetch( - new URL( - "../../../public/fonts/inter/static/Inter-Regular.ttf", - import.meta.url, - ).href, - ).then((res) => res.arrayBuffer()), - fetch(new URL("../../../public/og/bg1.png", import.meta.url)).then((res) => - res.arrayBuffer(), - ), - fetch( - new URL("../../../public/og/zeitgeist_badge.png", import.meta.url), - ).then((res) => res.arrayBuffer()), - ]); + const questionClass = question.length > 90 ? "text-4xl" : "text-5xl"; + + const [boldFont, regularFont, bg, zeitgeistBadge, fallbackImage] = + await Promise.all([ + fetch( + new URL( + "../../../public/fonts/inter/static/Inter-Bold.ttf", + import.meta.url, + ).href, + ).then((res) => res.arrayBuffer()), + fetch( + new URL( + "../../../public/fonts/inter/static/Inter-Regular.ttf", + import.meta.url, + ).href, + ).then((res) => res.arrayBuffer()), + fetch(new URL("../../../public/og/bg1.png", import.meta.url)).then( + (res) => res.arrayBuffer(), + ), + fetch( + new URL("../../../public/og/zeitgeist_badge.png", import.meta.url), + ).then((res) => res.arrayBuffer()), + fetch( + new URL("../../../public/categories/sports/4.png", import.meta.url), + // new URL(fallbackImagePath, import.meta.url), + ).then((res) => res.arrayBuffer()), + ]); const image = (