|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + BarChart, |
| 5 | + Bar, |
| 6 | + XAxis, |
| 7 | + YAxis, |
| 8 | + CartesianGrid, |
| 9 | + Tooltip, |
| 10 | + ResponsiveContainer, |
| 11 | + Rectangle, |
| 12 | +} from "recharts"; |
| 13 | + |
| 14 | +import { useDataTable } from "@calcom/features/data-table"; |
| 15 | +import { useLocale } from "@calcom/lib/hooks/useLocale"; |
| 16 | +import { CURRENT_TIMEZONE } from "@calcom/lib/timezoneConstants"; |
| 17 | +import { trpc } from "@calcom/trpc"; |
| 18 | + |
| 19 | +import { useInsightsParameters } from "../hooks/useInsightsParameters"; |
| 20 | +import { ChartCard } from "./ChartCard"; |
| 21 | +import { LoadingInsight } from "./LoadingInsights"; |
| 22 | + |
| 23 | +type BookingsByHourData = { |
| 24 | + hour: number; |
| 25 | + count: number; |
| 26 | +}; |
| 27 | + |
| 28 | +export const BookingsByHourChartContent = ({ data }: { data: BookingsByHourData[] }) => { |
| 29 | + const { t } = useLocale(); |
| 30 | + |
| 31 | + const chartData = data.map((item) => ({ |
| 32 | + hour: `${item.hour.toString().padStart(2, "0")}:00`, |
| 33 | + count: item.count, |
| 34 | + })); |
| 35 | + |
| 36 | + const maxBookings = Math.max(...data.map((item) => item.count)); |
| 37 | + const isEmpty = maxBookings === 0; |
| 38 | + |
| 39 | + if (isEmpty) { |
| 40 | + return ( |
| 41 | + <div className="text-default flex h-60 text-center"> |
| 42 | + <p className="m-auto text-sm font-light">{t("insights_no_data_found_for_filter")}</p> |
| 43 | + </div> |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className="mt-4 h-80"> |
| 49 | + <ResponsiveContainer width="100%" height="100%"> |
| 50 | + <BarChart data={chartData} margin={{ top: 20, right: 0, left: -10, bottom: 5 }}> |
| 51 | + <CartesianGrid strokeDasharray="3 3" vertical={false} /> |
| 52 | + <XAxis dataKey="hour" className="text-xs" axisLine={false} tickLine={false} /> |
| 53 | + <YAxis allowDecimals={false} className="text-xs opacity-50" axisLine={false} tickLine={false} /> |
| 54 | + <Tooltip cursor={false} content={<CustomTooltip />} /> |
| 55 | + <Bar |
| 56 | + dataKey="count" |
| 57 | + fill="var(--cal-bg-subtle)" |
| 58 | + radius={[2, 2, 0, 0]} |
| 59 | + activeBar={<Rectangle fill="var(--cal-bg-info)" />} |
| 60 | + /> |
| 61 | + </BarChart> |
| 62 | + </ResponsiveContainer> |
| 63 | + </div> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +// Custom Tooltip component |
| 68 | +const CustomTooltip = ({ |
| 69 | + active, |
| 70 | + payload, |
| 71 | + label, |
| 72 | +}: { |
| 73 | + active?: boolean; |
| 74 | + payload?: Array<{ |
| 75 | + value: number; |
| 76 | + dataKey: string; |
| 77 | + name: string; |
| 78 | + color: string; |
| 79 | + payload: { hour: string; count: number }; |
| 80 | + }>; |
| 81 | + label?: string; |
| 82 | +}) => { |
| 83 | + const { t } = useLocale(); |
| 84 | + if (!active || !payload?.length) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <div className="bg-default border-subtle rounded-lg border p-3 shadow-lg"> |
| 90 | + <p className="text-default font-medium">{label}</p> |
| 91 | + {payload.map((entry, index: number) => ( |
| 92 | + <p key={index}> |
| 93 | + {t("bookings")}: {entry.value} |
| 94 | + </p> |
| 95 | + ))} |
| 96 | + </div> |
| 97 | + ); |
| 98 | +}; |
| 99 | + |
| 100 | +export const BookingsByHourChart = () => { |
| 101 | + const { t } = useLocale(); |
| 102 | + const { timeZone } = useDataTable(); |
| 103 | + const { scope, selectedTeamId, memberUserId, startDate, endDate, eventTypeId } = useInsightsParameters(); |
| 104 | + |
| 105 | + const { data, isSuccess, isPending } = trpc.viewer.insights.bookingsByHourStats.useQuery( |
| 106 | + { |
| 107 | + scope, |
| 108 | + selectedTeamId, |
| 109 | + startDate, |
| 110 | + endDate, |
| 111 | + eventTypeId, |
| 112 | + memberUserId, |
| 113 | + timeZone: timeZone || CURRENT_TIMEZONE, |
| 114 | + }, |
| 115 | + { |
| 116 | + staleTime: 30000, |
| 117 | + trpc: { |
| 118 | + context: { skipBatch: true }, |
| 119 | + }, |
| 120 | + } |
| 121 | + ); |
| 122 | + |
| 123 | + if (isPending) return <LoadingInsight />; |
| 124 | + |
| 125 | + if (!isSuccess || !data) return null; |
| 126 | + |
| 127 | + return ( |
| 128 | + <ChartCard title={t("bookings_by_hour")}> |
| 129 | + <BookingsByHourChartContent data={data} /> |
| 130 | + </ChartCard> |
| 131 | + ); |
| 132 | +}; |
0 commit comments