2023-03-23 22:10:01 +00:00
|
|
|
import { Grid } from "@tremor/react";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc";
|
2023-04-04 11:58:19 +00:00
|
|
|
import { SkeletonContainer, SkeletonText } from "@calcom/ui";
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
import { useFilterContext } from "../context/provider";
|
2023-04-04 11:58:19 +00:00
|
|
|
import { CardInsights } from "./Card";
|
2023-03-23 22:10:01 +00:00
|
|
|
import { KPICard } from "./KPICard";
|
|
|
|
|
|
|
|
export const BookingKPICards = () => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const { filter } = useFilterContext();
|
2023-09-18 23:36:07 +00:00
|
|
|
const { dateRange, selectedEventTypeId, selectedUserId, selectedMemberUserId, isAll, initialConfig } =
|
|
|
|
filter;
|
|
|
|
const initialConfigIsReady = !!(initialConfig?.teamId || initialConfig?.userId || initialConfig?.isAll);
|
2023-03-23 22:10:01 +00:00
|
|
|
const [startDate, endDate] = dateRange;
|
|
|
|
|
|
|
|
const { selectedTeamId: teamId } = filter;
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
const { data, isSuccess, isLoading } = trpc.viewer.insights.eventsByStatus.useQuery(
|
|
|
|
{
|
|
|
|
startDate: startDate.toISOString(),
|
|
|
|
endDate: endDate.toISOString(),
|
|
|
|
teamId,
|
|
|
|
eventTypeId: selectedEventTypeId ?? 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-09-18 23:36:07 +00:00
|
|
|
enabled: initialConfigIsReady,
|
2023-04-19 20:14:09 +00:00
|
|
|
}
|
|
|
|
);
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
const categories: {
|
|
|
|
title: string;
|
|
|
|
index: "created" | "completed" | "rescheduled" | "cancelled";
|
|
|
|
}[] = [
|
|
|
|
{
|
|
|
|
title: t("events_created"),
|
|
|
|
index: "created",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("events_completed"),
|
|
|
|
index: "completed",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("events_rescheduled"),
|
|
|
|
index: "rescheduled",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("events_cancelled"),
|
|
|
|
index: "cancelled",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (isLoading) {
|
|
|
|
return <LoadingKPICards categories={categories} />;
|
|
|
|
}
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (!isSuccess || !startDate || !endDate || (!teamId && !selectedUserId)) return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Grid numColsSm={2} numColsLg={4} className="gap-x-6 gap-y-6">
|
|
|
|
{categories.map((item) => (
|
|
|
|
<KPICard
|
|
|
|
key={item.title}
|
|
|
|
title={item.title}
|
|
|
|
value={data[item.index].count}
|
|
|
|
previousMetricData={data[item.index]}
|
|
|
|
previousDateRange={data.previousRange}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Grid>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const LoadingKPICards = (props: { categories: { title: string; index: string }[] }) => {
|
|
|
|
const { categories } = props;
|
2023-03-23 22:10:01 +00:00
|
|
|
return (
|
|
|
|
<Grid numColsSm={2} numColsLg={4} className="gap-x-6 gap-y-6">
|
|
|
|
{categories.map((item) => (
|
2023-04-04 11:58:19 +00:00
|
|
|
<CardInsights key={item.title}>
|
|
|
|
<SkeletonContainer className="flex w-full flex-col">
|
|
|
|
<SkeletonText className="mt-2 h-4 w-32" />
|
|
|
|
<SkeletonText className="mt-2 h-6 w-16" />
|
|
|
|
<SkeletonText className="mt-4 h-6 w-44" />
|
|
|
|
</SkeletonContainer>
|
|
|
|
</CardInsights>
|
2023-03-23 22:10:01 +00:00
|
|
|
))}
|
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
};
|