import { useEffect } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { mutate } from "swr"; // services import { AnalyticsService } from "services/analytics.service"; // hooks import useToast from "hooks/use-toast"; import { useMobxStore } from "lib/mobx/store-provider"; // components import { CustomAnalyticsSidebarHeader, CustomAnalyticsSidebarProjectsList } from "components/analytics"; // ui import { Button, LayersIcon } from "@plane/ui"; // icons import { CalendarDays, Download, RefreshCw } from "lucide-react"; // helpers import { renderShortDate } from "helpers/date-time.helper"; // types import { IAnalyticsParams, IAnalyticsResponse, IExportAnalyticsFormData, IWorkspace } from "types"; // fetch-keys import { ANALYTICS } from "constants/fetch-keys"; type Props = { analytics: IAnalyticsResponse | undefined; params: IAnalyticsParams; fullScreen: boolean; isProjectLevel: boolean; }; const analyticsService = new AnalyticsService(); export const CustomAnalyticsSidebar: React.FC = observer((props) => { const { analytics, params, fullScreen, isProjectLevel = false } = props; // router const router = useRouter(); const { workspaceSlug, projectId, cycleId, moduleId } = router.query; // toast const { setToastAlert } = useToast(); // mobx store const { project: { workspaceProjects, getProjectById }, cycle: { getCycleById, fetchCycleWithId }, module: { getModuleById, fetchModuleDetails }, } = useMobxStore(); const projectDetails = workspaceSlug && projectId ? getProjectById(workspaceSlug.toString(), projectId.toString()) ?? undefined : undefined; const trackExportAnalytics = () => { const eventPayload: any = { workspaceSlug: workspaceSlug?.toString(), params: { x_axis: params.x_axis, y_axis: params.y_axis, group: params.segment, project: params.project, }, }; if (projectDetails) { const workspaceDetails = projectDetails.workspace as IWorkspace; eventPayload.workspaceId = workspaceDetails.id; eventPayload.workspaceName = workspaceDetails.name; eventPayload.projectId = projectDetails.id; eventPayload.projectIdentifier = projectDetails.identifier; eventPayload.projectName = projectDetails.name; } if (cycleDetails || moduleDetails) { const details = cycleDetails || moduleDetails; eventPayload.workspaceId = details?.workspace_detail?.id; eventPayload.workspaceName = details?.workspace_detail?.name; eventPayload.projectId = details?.project_detail.id; eventPayload.projectIdentifier = details?.project_detail.identifier; eventPayload.projectName = details?.project_detail.name; } if (cycleDetails) { eventPayload.cycleId = cycleDetails.id; eventPayload.cycleName = cycleDetails.name; } if (moduleDetails) { eventPayload.moduleId = moduleDetails.id; eventPayload.moduleName = moduleDetails.name; } }; 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, }); trackExportAnalytics(); }) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "There was some error in exporting the analytics. Please try again.", }) ); }; const cycleDetails = cycleId ? getCycleById(cycleId.toString()) : undefined; const moduleDetails = moduleId ? getModuleById(moduleId.toString()) : undefined; // fetch cycle details useEffect(() => { if (!workspaceSlug || !projectId || !cycleId || cycleDetails) return; fetchCycleWithId(workspaceSlug.toString(), projectId.toString(), cycleId.toString()); }, [cycleId, cycleDetails, fetchCycleWithId, projectId, workspaceSlug]); // fetch module details useEffect(() => { if (!workspaceSlug || !projectId || !moduleId || moduleDetails) return; fetchModuleDetails(workspaceSlug.toString(), projectId.toString(), moduleId.toString()); }, [moduleId, moduleDetails, fetchModuleDetails, projectId, workspaceSlug]); const selectedProjects = params.project && params.project.length > 0 ? params.project : workspaceProjects?.map((p) => p.id); return (
{analytics ? analytics.total : "..."} Issues
{isProjectLevel && (
{renderShortDate( (cycleId ? cycleDetails?.created_at : moduleId ? moduleDetails?.created_at : projectDetails?.created_at) ?? "" )}
)}
{fullScreen ? ( <> {!isProjectLevel && selectedProjects && selectedProjects.length > 0 && ( selectedProjects.includes(p.id)) ?? []} /> )} ) : null}
); });