import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { Control, Controller, UseFormSetValue } from "react-hook-form"; // services import analyticsService from "services/analytics.service"; // hooks import useProjects from "hooks/use-projects"; import useToast from "hooks/use-toast"; // ui import { CustomMenu, CustomSelect, PrimaryButton } from "components/ui"; // icons import { ArrowPathIcon, ArrowUpTrayIcon } from "@heroicons/react/24/outline"; // types import { IAnalyticsParams, IAnalyticsResponse, IExportAnalyticsFormData } from "types"; // fetch-keys import { ANALYTICS } from "constants/fetch-keys"; // constants import { ANALYTICS_X_AXIS_VALUES, ANALYTICS_Y_AXIS_VALUES } from "constants/analytics"; type Props = { analytics: IAnalyticsResponse | undefined; params: IAnalyticsParams; control: Control; setValue: UseFormSetValue; setSaveAnalyticsModal: React.Dispatch>; fullScreen: boolean; isProjectLevel?: boolean; }; export const AnalyticsSidebar: React.FC = ({ analytics, params, control, setValue, setSaveAnalyticsModal, fullScreen, isProjectLevel = false, }) => { const router = useRouter(); const { workspaceSlug } = router.query; const { projects } = useProjects(); const { setToastAlert } = useToast(); const exportAnalytics = () => { if (!workspaceSlug) return; const data: IExportAnalyticsFormData = { x_axis: params.x_axis, y_axis: params.y_axis, }; if (params.segment) data.segment = params.segment; if (params.project) data.project = [params.project]; analyticsService .exportAnalytics(workspaceSlug.toString(), data) .then((res) => setToastAlert({ type: "success", title: "Success!", message: res.message, }) ) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "There was some error in exporting the analytics. Please try again.", }) ); }; return (
{analytics?.total ?? 0}{" "} issues
{ if (!workspaceSlug) return; mutate(ANALYTICS(workspaceSlug.toString(), params)); }} >
Refresh
Export analytics as CSV
{isProjectLevel === false && (
Project
( p.id === value)?.name ?? "All projects"} onChange={onChange} width="w-full" maxHeight="lg" > All projects {projects.map((project) => ( {project.name} ))} )} />
)}
Measure (y-axis)
( {ANALYTICS_Y_AXIS_VALUES.find((v) => v.value === value)?.label ?? "None"} } onChange={onChange} width="w-full" > {ANALYTICS_Y_AXIS_VALUES.map((item) => ( {item.label} ))} )} />
Dimension (x-axis)
( {ANALYTICS_X_AXIS_VALUES.find((v) => v.value === value)?.label} } onChange={(val: string) => { if (params.segment === val) setValue("segment", null); onChange(val); }} width="w-full" maxHeight="lg" > {ANALYTICS_X_AXIS_VALUES.map((item) => ( {item.label} ))} )} />
Segment
( {ANALYTICS_X_AXIS_VALUES.find((v) => v.value === value)?.label ?? ( No value )} } onChange={onChange} width="w-full" maxHeight="lg" > No value {ANALYTICS_X_AXIS_VALUES.map((item) => { if (params.x_axis === item.value) return null; return ( {item.label} ); })} )} />
{/*
setSaveAnalyticsModal(true)}> Save analytics
*/}
); };