Skip to content

Commit 04e65be

Browse files
committed
refactor code simplify logic and fix issue on production builds
1 parent a955594 commit 04e65be

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/components/StatsBoxGrid/GridItem.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Line } from "react-chartjs-2"
1111
import { MdInfoOutline } from "react-icons/md"
1212
import { Box, Flex, Icon, Text } from "@chakra-ui/react"
1313

14-
import type { StatsBoxMetric } from "@/lib/types"
14+
import type { StatsBoxMetric, TimestampedData } from "@/lib/types"
1515

1616
import { RANGES } from "@/lib/constants"
1717

@@ -69,8 +69,7 @@ export const GridItem = ({ metric }: GridItemProps) => {
6969
)
7070

7171
// Returns either 90 or 30-day data range depending on `range` selection
72-
const filteredData = (data: Array<{ timestamp: number }>) => {
73-
if (!data) return
72+
const filteredData = (data: TimestampedData<number>[]) => {
7473
if (range === RANGES[1]) return [...data]
7574

7675
return data.filter(({ timestamp }) => {
@@ -138,16 +137,13 @@ export const GridItem = ({ metric }: GridItemProps) => {
138137
},
139138
}
140139

141-
const filteredRange = hasData ? filteredData(state.data) : [] // timestamp values
142-
const dataValues = hasData ? state.data.map((item) => item.value) : [] // data values
140+
const filteredRange = filteredData(hasData ? state.data : [])
143141

144142
const chartData = {
145143
labels: filteredRange,
146144
datasets: [
147145
{
148-
data: hasData
149-
? dataValues.slice(dataValues.length - filteredRange!.length)
150-
: [],
146+
data: filteredRange.map((item) => item.value),
151147
},
152148
],
153149
}

0 commit comments

Comments
 (0)