From b0d10df8778c56163b21f600bf8b0de76d671a4d Mon Sep 17 00:00:00 2001 From: Mathieu Anderson Date: Wed, 10 Jan 2024 17:53:38 +0100 Subject: [PATCH] feat(coral): Add `useDashboardData` hook (#2184) * feat(coral): Add useDashboardData * feat(coral): Use useDashboardData and add toast for error --------- Signed-off-by: Mathieu Anderson --- .../app/features/dashboard/Dasboard.test.tsx | 1 + .../src/app/features/dashboard/Dashboard.tsx | 108 ++++++------------ .../__snapshots__/Dasboard.test.tsx.snap | 23 +++- .../dashboard/hooks/useDashboardData.ts | 88 ++++++++++++++ 4 files changed, 148 insertions(+), 72 deletions(-) create mode 100644 coral/src/app/features/dashboard/hooks/useDashboardData.ts diff --git a/coral/src/app/features/dashboard/Dasboard.test.tsx b/coral/src/app/features/dashboard/Dasboard.test.tsx index 87a7ec986b..f604f27efe 100644 --- a/coral/src/app/features/dashboard/Dasboard.test.tsx +++ b/coral/src/app/features/dashboard/Dasboard.test.tsx @@ -195,6 +195,7 @@ describe("Dashboard.tsx", () => { component = customRender(, { memoryRouter: true, queryClient: true, + aquariumContext: true, }); }); diff --git a/coral/src/app/features/dashboard/Dashboard.tsx b/coral/src/app/features/dashboard/Dashboard.tsx index 6bdb2d4728..dae999a666 100644 --- a/coral/src/app/features/dashboard/Dashboard.tsx +++ b/coral/src/app/features/dashboard/Dashboard.tsx @@ -1,43 +1,29 @@ -import { Alert, Box, Card, Grid, Icon, Template } from "@aivenio/aquarium"; +import { Box, Card, Grid, Icon, Template, useToast } from "@aivenio/aquarium"; import { AreaChart, Axis, BarChart, Tooltip } from "@aivenio/aquarium/charts"; import loading from "@aivenio/aquarium/icons/loading"; -import { useQuery } from "@tanstack/react-query"; import { useEffect, useState } from "react"; import StatsDisplay from "src/app/components/StatsDisplay"; -import { useAuthContext } from "src/app/context-provider/AuthProvider"; -import { usePendingRequests } from "src/app/hooks/usePendingRequests"; -import { - DashboardsAnalyticsData, - getActivityLogForTeamOverview, - getDashboardStats, -} from "src/domain/analytics"; -import { parseErrorMsg } from "src/services/mutation-utils"; +import { useDashboardData } from "src/app/features/dashboard/hooks/useDashboardData"; const Dashboard = () => { const { - data: chartData, - isLoading: isLoadingChartData, - isError: isErrorChartData, - error: errorChartData, - } = useQuery( - ["getActivityLogForTeamOverview"], - { - queryFn: () => getActivityLogForTeamOverview(), - } - ); + data: { chartsData, statsData }, + isLoading, + isError, + error, + } = useDashboardData(); - const { - data: statsData, - isLoading: isLoadingStatsData, - isError: isErrorStatsData, - error: errorStatsData, - } = useQuery(["getDashboardStats"], { - queryFn: getDashboardStats, - }); + const toast = useToast(); - const { TOPIC, ACL, SCHEMA, CONNECTOR } = usePendingRequests(); - - const { totalTeamTopics, totalOrgTopics } = useAuthContext(); + useEffect(() => { + if (isError) { + toast({ + message: error, + position: "bottom-left", + variant: "default", + }); + } + }, [isError]); // The following contraption is to allow charts to be responsive despite being rendered in a Card // Without it, the size of the charts calculated by the ResponsiveContainer they are crapped in under the hood @@ -45,6 +31,7 @@ const Dashboard = () => { // This will remove the Chart components and show the loading state while resizing, and then render them again with the proper size // Source: https://github.com/recharts/recharts/issues/1423#issuecomment-538670179 const [forceLoading, setForceLoading] = useState(false); + useEffect(() => { let resizeThrottleTimeout: NodeJS.Timeout; let skip = false; @@ -70,32 +57,25 @@ const Dashboard = () => { return (