bb-plane-fork/web/core/components/analytics-v2/total-insights.tsx
Bavisetti Narayan 9812129ad3
[WEB-4133] chore: optimised the analytics endpoints (#7105)
* chore: optimised the analytics endpoints

* chore: segregated peek view endpoints

* chore: added analytics values validation

* chore: added project validation

* chore: reverted the changes

---------

Co-authored-by: JayashTripathy <jayashtripathy371@gmail.com>
2025-05-23 15:05:57 +05:30

66 lines
2.4 KiB
TypeScript

// plane package imports
import { observer } from "mobx-react-lite";
import { useParams } from "next/navigation";
import useSWR from "swr";
import { insightsFields } from "@plane/constants";
import { useTranslation } from "@plane/i18n";
import { IAnalyticsResponseV2, TAnalyticsTabsV2Base } from "@plane/types";
//hooks
import { cn } from "@/helpers/common.helper";
import { useAnalyticsV2 } from "@/hooks/store/use-analytics-v2";
//services
import { AnalyticsV2Service } from "@/services/analytics-v2.service";
// plane web components
import InsightCard from "./insight-card";
const analyticsV2Service = new AnalyticsV2Service();
const TotalInsights: React.FC<{ analyticsType: TAnalyticsTabsV2Base; peekView?: boolean }> = observer(
({ analyticsType, peekView }) => {
const params = useParams();
const workspaceSlug = params.workspaceSlug.toString();
const { t } = useTranslation();
const { selectedDuration, selectedProjects, selectedDurationLabel, selectedCycle, selectedModule, isPeekView } =
useAnalyticsV2();
const { data: totalInsightsData, isLoading } = useSWR(
`total-insights-${analyticsType}-${selectedDuration}-${selectedProjects}-${selectedCycle}-${selectedModule}-${isPeekView}`,
() =>
analyticsV2Service.getAdvanceAnalytics<IAnalyticsResponseV2>(
workspaceSlug,
analyticsType,
{
// date_filter: selectedDuration,
...(selectedProjects?.length > 0 ? { project_ids: selectedProjects.join(",") } : {}),
...(selectedCycle ? { cycle_id: selectedCycle } : {}),
...(selectedModule ? { module_id: selectedModule } : {}),
},
isPeekView
)
);
return (
<div
className={cn(
"grid grid-cols-1 gap-8 sm:grid-cols-2 md:gap-10",
!peekView
? insightsFields[analyticsType].length % 5 === 0
? "gap-10 lg:grid-cols-5"
: "gap-8 lg:grid-cols-4"
: "grid-cols-2"
)}
>
{insightsFields[analyticsType]?.map((item: string) => (
<InsightCard
key={`${analyticsType}-${item}`}
isLoading={isLoading}
data={totalInsightsData?.[item]}
label={t(`workspace_analytics.${item}`)}
versus={selectedDurationLabel}
/>
))}
</div>
);
}
);
export default TotalInsights;