From 13d014ba400159940b1aa8642761dfd6fd04bdad Mon Sep 17 00:00:00 2001 From: robhyrk Date: Sat, 2 Mar 2024 10:59:05 -0500 Subject: [PATCH] fix: price undefined checks --- components/front-page/HeroBanner.tsx | 28 +++++++++++++++------------- lib/gql/historical-prices.ts | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/components/front-page/HeroBanner.tsx b/components/front-page/HeroBanner.tsx index 97280a5bc..652e77583 100644 --- a/components/front-page/HeroBanner.tsx +++ b/components/front-page/HeroBanner.tsx @@ -15,12 +15,12 @@ export const HeroBanner = ({ bannerPlaceholder: string; chainProperties: GenericChainProperties; }) => { - const chartData = ztgHistory.prices.map(([timestamp, price]) => { + const chartData = ztgHistory.prices?.map(([timestamp, price]) => { return { v: price, t: 1 }; }); - const firstPrice = ztgHistory.prices[0][1]; - const latestPrice = ztgHistory.prices[ztgHistory.prices.length - 1][1]; + const firstPrice = ztgHistory.prices?.[0]?.[1]; + const latestPrice = ztgHistory.prices?.[ztgHistory.prices.length - 1]?.[1]; const prctChange = ((latestPrice - firstPrice) / firstPrice) * 100; return ( @@ -76,24 +76,26 @@ export const HeroBanner = ({ dot={false} strokeWidth={2} stroke={getColour( - chartData[0].v, - chartData[chartData.length - 1].v, + chartData?.[0].v, + chartData?.[chartData.length - 1].v, )} /> -
-
-
- ${latestPrice.toFixed(3)} -
-
- {!isNaN(prctChange) ? prctChange.toFixed(1) : 0}% + {latestPrice && ( +
+
+
+ ${latestPrice.toFixed(3)} +
+
+ {!isNaN(prctChange) ? prctChange.toFixed(1) : 0}% +
-
+ )}
diff --git a/lib/gql/historical-prices.ts b/lib/gql/historical-prices.ts index 92c08c87c..2323e5902 100644 --- a/lib/gql/historical-prices.ts +++ b/lib/gql/historical-prices.ts @@ -58,7 +58,7 @@ export const getBaseAssetHistoricalPrices = async (): Promise => { const findPrice = (timestamp: number, prices: [number, number][]) => { const date = new Date(Number(timestamp)); - const price = prices.find((p) => { + const price = prices?.find((p) => { return datesAreOnSameDay(date, new Date(p[0])); });