2023-04-05 18:14:46 +00:00
|
|
|
import { Title } from "@tremor/react";
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc";
|
|
|
|
|
|
|
|
import { useFilterContext } from "../context/provider";
|
|
|
|
import { valueFormatter } from "../lib/valueFormatter";
|
2023-03-28 23:24:57 +00:00
|
|
|
import { CardInsights } from "./Card";
|
2023-04-05 18:14:46 +00:00
|
|
|
import { LineChart } from "./LineChart";
|
2023-04-04 11:58:19 +00:00
|
|
|
import { LoadingInsight } from "./LoadingInsights";
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
export const AverageEventDurationChart = () => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const { filter } = useFilterContext();
|
2023-06-29 17:03:44 +00:00
|
|
|
const { dateRange, selectedMemberUserId, isAll } = filter;
|
2023-03-23 22:10:01 +00:00
|
|
|
const [startDate, endDate] = dateRange;
|
2023-04-04 11:58:19 +00:00
|
|
|
const { selectedTeamId: teamId, selectedUserId } = filter;
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
const { data, isSuccess, isLoading } = trpc.viewer.insights.averageEventDuration.useQuery(
|
|
|
|
{
|
|
|
|
startDate: startDate.toISOString(),
|
|
|
|
endDate: endDate.toISOString(),
|
|
|
|
teamId: teamId ?? undefined,
|
|
|
|
memberUserId: selectedMemberUserId ?? undefined,
|
|
|
|
userId: selectedUserId ?? undefined,
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll,
|
2023-04-19 20:14:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
staleTime: 30000,
|
|
|
|
trpc: {
|
|
|
|
context: { skipBatch: true },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (isLoading) return <LoadingInsight />;
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (!isSuccess || !startDate || !endDate || (!teamId && !selectedUserId)) return null;
|
|
|
|
const isNoData = (data && data.length === 0) || data.every((item) => item["Average"] === 0);
|
2023-03-23 22:10:01 +00:00
|
|
|
return (
|
2023-03-28 23:24:57 +00:00
|
|
|
<CardInsights>
|
2023-04-05 18:14:46 +00:00
|
|
|
<Title className="text-emphasis">{t("average_event_duration")}</Title>
|
2023-04-04 11:58:19 +00:00
|
|
|
{isNoData && (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-default flex h-60 text-center">
|
2023-04-04 11:58:19 +00:00
|
|
|
<p className="m-auto text-sm font-light">{t("insights_no_data_found_for_filter")}</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{data && data.length > 0 && !isNoData && (
|
|
|
|
<LineChart
|
|
|
|
className="mt-4 h-80"
|
|
|
|
data={data}
|
|
|
|
index="Date"
|
|
|
|
categories={["Average"]}
|
|
|
|
colors={["blue"]}
|
|
|
|
valueFormatter={valueFormatter}
|
|
|
|
/>
|
|
|
|
)}
|
2023-03-28 23:24:57 +00:00
|
|
|
</CardInsights>
|
2023-03-23 22:10:01 +00:00
|
|
|
);
|
|
|
|
};
|