diff --git a/packages/features/insights/components/ChartCard.tsx b/packages/features/insights/components/ChartCard.tsx index 95bf592089..3e01ef4a16 100644 --- a/packages/features/insights/components/ChartCard.tsx +++ b/packages/features/insights/components/ChartCard.tsx @@ -1,6 +1,6 @@ "use client"; -import { Fragment, type ReactNode } from "react"; +import { Fragment, useMemo, type ReactNode } from "react"; import classNames from "@calcom/ui/classNames"; import { PanelCard } from "@calcom/ui/components/card"; @@ -19,6 +19,8 @@ export function ChartCard({ cta, legend, legendSize, + enabledLegend, + onSeriesToggle, children, className, titleTooltip, @@ -28,11 +30,16 @@ export function ChartCard({ cta?: { label: string; onClick: () => void }; legend?: Array; legendSize?: LegendSize; + enabledLegend?: Array; + onSeriesToggle?: (label: string) => void; className?: string; titleTooltip?: string; children: ReactNode; }) { - const legendComponent = legend && legend.length > 0 ? : null; + const legendComponent = + legend && legend.length > 0 ? ( + + ) : null; return ( void; +}) { + const enabledSet = useMemo(() => new Set((enabledItems ?? []).map((i) => i.label)), [enabledItems]); + const isClickable = Boolean(onItemToggle); + return (
- {items.map((item, index) => ( - -
-
- -

- {item.label} -

-
-
- {index < items.length - 1 &&
} - - ))} + {items.map((item, index) => { + const isEnabled = enabledItems ? enabledSet.has(item.label) : true; + + return ( + + + {index < items.length - 1 &&
} + + ); + })}
); } diff --git a/packages/features/insights/components/booking/EventTrendsChart.tsx b/packages/features/insights/components/booking/EventTrendsChart.tsx index 9d9f2f9507..4e192e5b45 100644 --- a/packages/features/insights/components/booking/EventTrendsChart.tsx +++ b/packages/features/insights/components/booking/EventTrendsChart.tsx @@ -7,6 +7,7 @@ import { trpc } from "@calcom/trpc"; import type { RouterOutputs } from "@calcom/trpc/react"; import { useInsightsBookingParameters } from "../../hooks/useInsightsBookingParameters"; +import { useToggleableLegend } from "../../hooks/useToggleableLegend"; import { valueFormatter } from "../../lib/valueFormatter"; import { ChartCard } from "../ChartCard"; import { LoadingInsight } from "../LoadingInsights"; @@ -66,6 +67,7 @@ const CustomTooltip = ({ export const EventTrendsChart = () => { const { t } = useLocale(); const insightsBookingParams = useInsightsBookingParameters(); + const { enabledLegend, toggleSeries } = useToggleableLegend(legend); const { data: eventTrends, @@ -84,7 +86,11 @@ export const EventTrendsChart = () => { if (!isSuccess) return null; return ( - +
@@ -98,7 +104,7 @@ export const EventTrendsChart = () => { tickFormatter={valueFormatter} /> } /> - {legend.map((item) => ( + {enabledLegend.map((item) => ( + ); } return ( - - + + ); } diff --git a/packages/features/insights/components/routing/RoutingFunnelContent.tsx b/packages/features/insights/components/routing/RoutingFunnelContent.tsx index 5ef9a8d98c..591ccf243e 100644 --- a/packages/features/insights/components/routing/RoutingFunnelContent.tsx +++ b/packages/features/insights/components/routing/RoutingFunnelContent.tsx @@ -14,6 +14,7 @@ interface RoutingFunnelData { interface RoutingFunnelContentProps { data: RoutingFunnelData[]; + enabledLegend?: Array<{ label: string; color: string }>; } const COLOR = { @@ -28,8 +29,9 @@ export const legend = [ { label: "Accepted Bookings", color: COLOR.ACCEPTED }, ]; -export function RoutingFunnelContent({ data }: RoutingFunnelContentProps) { +export function RoutingFunnelContent({ data, enabledLegend }: RoutingFunnelContentProps) { const { t } = useLocale(); + const activeAreas = enabledLegend || legend; return ( @@ -38,30 +40,36 @@ export function RoutingFunnelContent({ data }: RoutingFunnelContentProps) { } /> - - - + {activeAreas.some((area) => area.label === "Total Submissions") && ( + + )} + {activeAreas.some((area) => area.label === "Successful Routings") && ( + + )} + {activeAreas.some((area) => area.label === "Accepted Bookings") && ( + + )} ); diff --git a/packages/features/insights/hooks/useToggleableLegend.ts b/packages/features/insights/hooks/useToggleableLegend.ts new file mode 100644 index 0000000000..b9887e5f62 --- /dev/null +++ b/packages/features/insights/hooks/useToggleableLegend.ts @@ -0,0 +1,24 @@ +import { useState, useMemo, useCallback } from "react"; + +export const useToggleableLegend = (legend: T[], initialEnabled?: string[]) => { + const [enabledSeries, setEnabledSeries] = useState( + initialEnabled ?? legend.map((item) => item.label) + ); + + const enabledLegend = useMemo( + () => legend.filter((item) => enabledSeries.includes(item.label)), + [legend, enabledSeries] + ); + + const toggleSeries = useCallback( + (label: string) => { + setEnabledSeries((prev) => (prev.includes(label) ? prev.filter((s) => s !== label) : [...prev, label])); + }, + [setEnabledSeries] + ); + + return { + enabledLegend, + toggleSeries, + }; +};