diff --git a/src/app/page.tsx b/src/app/page.tsx index 0d81a32..914ca04 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,20 +1,76 @@ +/* eslint-disable react-hooks/set-state-in-effect */ "use client"; -import GetTotalGroupsQuery from "@/graphql/queries/getTotalGroups"; -import { Heading, Skeleton, Text } from "@chakra-ui/react"; +import { useEffect, useState } from "react"; +import { Fragment } from "react/jsx-runtime"; +import { Box, Heading, Skeleton, Text } from "@chakra-ui/react"; import { Icon } from "@iconify/react"; import { useQuery } from "@urql/next"; -import { Fragment } from "react/jsx-runtime"; +import Get30DayStatsQuery from "@/graphql/queries/get30DayStats"; +import GetTotalGroupsQuery from "@/graphql/queries/getTotalGroups"; +import lineChartArr from "@/lib/lineChartArray"; +import LinksDeletedChart from "./LinksDeletedChart"; +import { LineChartArr } from "@/types/LineChartStats"; export default function Home() { - const [{ fetching, data, error }] = useQuery({ query: GetTotalGroupsQuery }); + // Total Groups // + + const [ + { + fetching: totalGroupsFetching, + data: totalGroups, + error: totalGroupsError + } + ] = useQuery({ + query: GetTotalGroupsQuery + }); + + // 30 Day Stats // + + // Dates + const [currDate, setCurrDate] = useState(); + const [date30DaysAgo, setDate30DaysAgo] = useState(); + + useEffect(() => { + setCurrDate(new Date()); + setDate30DaysAgo(new Date(new Date().setDate(new Date().getDay() - 30))); + }, []); + + const [ + { + fetching: thirtyDayStatsFetching, + data: thirtyDayStats, + error: thirtyDayStatsError + } + ] = useQuery({ + query: Get30DayStatsQuery, + variables: { + stateDate: currDate, + endDate: date30DaysAgo + } + }); + + // Line Chart Data + const [lineChartArrState, setLineChartArrState] = useState( + [] as LineChartArr + ); + + useEffect(() => { + if (!thirtyDayStatsFetching && thirtyDayStats.getStatsRange) { + setLineChartArrState(lineChartArr(thirtyDayStats.getStatsRange)); + } + }, [ + thirtyDayStats.getStatsRange, + thirtyDayStatsError, + thirtyDayStatsFetching + ]); return ( {`Total number of groups the bot has helped`} - + - {error || !data || !data.getTotalGroups ? ( + {totalGroupsError || !totalGroups || !totalGroups.getTotalGroups ? ( ) : ( - `${data.getTotalGroups}` + `${totalGroups.getTotalGroups}` )} + + + ); }