import { useEffect } from "react"; import { observer } from "mobx-react-lite"; import { useRouter } from "next/router"; import { mutate } from "swr"; // services // hooks // components // ui import { CalendarDays, Download, RefreshCw } from "lucide-react"; import { IAnalyticsParams, IAnalyticsResponse, IExportAnalyticsFormData, IWorkspace } from "@plane/types"; import { Button, LayersIcon, TOAST_TYPE, setToast } from "@plane/ui"; // icons import { CustomAnalyticsSidebarHeader, CustomAnalyticsSidebarProjectsList } from "@/components/analytics"; // helpers // types // fetch-keys import { ANALYTICS } from "@/constants/fetch-keys"; import { cn } from "@/helpers/common.helper"; import { renderFormattedDate } from "@/helpers/date-time.helper"; import { useCycle, useModule, useProject, useUser, useWorkspace } from "@/hooks/store"; import { AnalyticsService } from "@/services/analytics.service"; type Props = { analytics: IAnalyticsResponse | undefined; params: IAnalyticsParams; isProjectLevel: boolean; }; const analyticsService = new AnalyticsService(); export const CustomAnalyticsSidebar: React.FC = observer((props) => { const { analytics, params, isProjectLevel = false } = props; // router const router = useRouter(); const { workspaceSlug, projectId, cycleId, moduleId } = router.query; // store hooks const { currentUser } = useUser(); const { workspaceProjectIds, getProjectById } = useProject(); const { getWorkspaceById } = useWorkspace(); const { fetchCycleDetails, getCycleById } = useCycle(); const { fetchModuleDetails, getModuleById } = useModule(); const projectDetails = projectId ? getProjectById(projectId.toString()) ?? undefined : undefined; const trackExportAnalytics = () => { if (!currentUser) return; 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; const currentProjectDetails = getProjectById(details?.project_id || ""); const currentWorkspaceDetails = getWorkspaceById(details?.workspace_id || ""); eventPayload.workspaceId = details?.workspace_id; eventPayload.workspaceName = currentWorkspaceDetails?.name; eventPayload.projectId = details?.project_id; eventPayload.projectIdentifier = currentProjectDetails?.identifier; eventPayload.projectName = currentProjectDetails?.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) => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: res.message, }); trackExportAnalytics(); }) .catch(() => setToast({ type: TOAST_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; fetchCycleDetails(workspaceSlug.toString(), projectId.toString(), cycleId.toString()); }, [cycleId, cycleDetails, fetchCycleDetails, 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 : workspaceProjectIds; return (
{analytics ? analytics.total : "..."}
Issues
{isProjectLevel && (
{renderFormattedDate( (cycleId ? cycleDetails?.created_at : moduleId ? moduleDetails?.created_at : projectDetails?.created_at) ?? "" )}
)}
<> {!isProjectLevel && selectedProjects && selectedProjects.length > 0 && ( )}
); });