fix: analytics page fixes

This commit is contained in:
sriram veeraghanta 2023-10-11 15:48:38 +05:30
parent 991a6858ec
commit 9f61d8bc06
5 changed files with 194 additions and 130 deletions

View File

@ -1,49 +1,51 @@
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import useSWR, { mutate } from "swr";
import { mutate } from "swr"; import { Control, UseFormSetValue, useForm } from "react-hook-form";
// react-hook-form
import { Control, UseFormSetValue } from "react-hook-form";
// hooks // hooks
import useProjects from "hooks/use-projects"; import useProjects from "hooks/use-projects";
// components // components
import { import { AnalyticsGraph, AnalyticsSelectBar, AnalyticsSidebar, AnalyticsTable } from "components/analytics";
AnalyticsGraph,
AnalyticsSelectBar,
AnalyticsSidebar,
AnalyticsTable,
} from "components/analytics";
// ui // ui
import { Loader, PrimaryButton } from "components/ui"; import { Loader, PrimaryButton } from "components/ui";
// helpers // helpers
import { convertResponseToBarGraphData } from "helpers/analytics.helper"; import { convertResponseToBarGraphData } from "helpers/analytics.helper";
// types // types
import { IAnalyticsParams, IAnalyticsResponse, ICurrentUserResponse } from "types"; import { IAnalyticsParams, IAnalyticsResponse, IUser } from "types";
// fetch-keys // fetch-keys
import { ANALYTICS } from "constants/fetch-keys"; import { ANALYTICS } from "constants/fetch-keys";
// services
import analyticsService from "services/analytics.service";
type Props = { type Props = {
analytics: IAnalyticsResponse | undefined;
analyticsError: any;
params: IAnalyticsParams;
control: Control<IAnalyticsParams, any>;
setValue: UseFormSetValue<IAnalyticsParams>;
fullScreen: boolean; fullScreen: boolean;
user: ICurrentUserResponse | undefined; user?: IUser | undefined;
}; };
export const CustomAnalytics: React.FC<Props> = ({ const defaultValues: IAnalyticsParams = {
analytics, x_axis: "priority",
analyticsError, y_axis: "issue_count",
params, segment: null,
control, project: null,
setValue, };
fullScreen,
user, export const CustomAnalytics: React.FC<Props> = ({ fullScreen, user }) => {
}) => {
const router = useRouter(); const router = useRouter();
const { workspaceSlug, projectId } = router.query; const { workspaceSlug, projectId } = router.query;
const { control, watch, setValue } = useForm<IAnalyticsParams>({ defaultValues });
const params: IAnalyticsParams = {
x_axis: watch("x_axis"),
y_axis: watch("y_axis"),
segment: watch("segment"),
project: watch("project"),
};
const { data: analytics, error: analyticsError } = useSWR(
workspaceSlug ? ANALYTICS(workspaceSlug.toString(), params) : null,
workspaceSlug ? () => analyticsService.getAnalytics(workspaceSlug.toString(), params) : null
);
const isProjectLevel = projectId ? true : false; const isProjectLevel = projectId ? true : false;
const yAxisKey = params.y_axis === "issue_count" ? "count" : "estimate"; const yAxisKey = params.y_axis === "issue_count" ? "count" : "estimate";
@ -52,11 +54,7 @@ export const CustomAnalytics: React.FC<Props> = ({
const { projects } = useProjects(); const { projects } = useProjects();
return ( return (
<div <div className={`overflow-hidden flex flex-col-reverse ${fullScreen ? "md:grid md:grid-cols-4 md:h-full" : ""}`}>
className={`overflow-hidden flex flex-col-reverse ${
fullScreen ? "md:grid md:grid-cols-4 md:h-full" : ""
}`}
>
<div className="col-span-3 flex flex-col h-full overflow-hidden"> <div className="col-span-3 flex flex-col h-full overflow-hidden">
<AnalyticsSelectBar <AnalyticsSelectBar
control={control} control={control}
@ -77,12 +75,7 @@ export const CustomAnalytics: React.FC<Props> = ({
yAxisKey={yAxisKey} yAxisKey={yAxisKey}
fullScreen={fullScreen} fullScreen={fullScreen}
/> />
<AnalyticsTable <AnalyticsTable analytics={analytics} barGraphData={barGraphData} params={params} yAxisKey={yAxisKey} />
analytics={analytics}
barGraphData={barGraphData}
params={params}
yAxisKey={yAxisKey}
/>
</div> </div>
) : ( ) : (
<div className="grid h-full place-items-center p-5"> <div className="grid h-full place-items-center p-5">

View File

@ -0,0 +1,33 @@
import { useRouter } from "next/router";
import { ArrowLeft } from "lucide-react";
// ui
import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs";
export const WorkspaceAnalyticsHeader = () => {
const router = useRouter();
return (
<>
<div
className={`relative flex w-full flex-shrink-0 flex-row z-10 items-center justify-between gap-x-2 gap-y-4 border-b border-custom-border-200 bg-custom-sidebar-background-100 p-4`}
>
<div className="flex items-center gap-2 flex-grow w-full whitespace-nowrap overflow-ellipsis">
<div className="block md:hidden">
<button
type="button"
className="grid h-8 w-8 place-items-center rounded border border-custom-border-200"
onClick={() => router.back()}
>
<ArrowLeft fontSize={14} strokeWidth={2} />
</button>
</div>
<div>
<Breadcrumbs>
<BreadcrumbItem title="Workspace Analytics" />
</Breadcrumbs>
</div>
</div>
</div>
</>
);
};

View File

@ -1,3 +1,2 @@
export * from "./layout"; export * from "./layout";
export * from "./sidebar"; export * from "./sidebar";
export * from "./header";

View File

@ -5,10 +5,7 @@ import { useRouter } from "next/router";
import useSWR from "swr"; import useSWR from "swr";
// react-hook-form // react-hook-form
import { useForm } from "react-hook-form";
// hooks
import useUserAuth from "hooks/use-user-auth";
import useProjects from "hooks/use-projects";
// headless ui // headless ui
import { Tab } from "@headlessui/react"; import { Tab } from "@headlessui/react";
// services // services
@ -29,36 +26,25 @@ import emptyAnalytics from "public/empty-state/analytics.svg";
import { IAnalyticsParams } from "types"; import { IAnalyticsParams } from "types";
// fetch-keys // fetch-keys
import { ANALYTICS } from "constants/fetch-keys"; import { ANALYTICS } from "constants/fetch-keys";
import { AppLayout } from "layouts/app-layout";
const defaultValues: IAnalyticsParams = { import { useMobxStore } from "lib/mobx/store-provider";
x_axis: "priority", import { observer } from "mobx-react-lite";
y_axis: "issue_count", import { WorkspaceAnalyticsHeader } from "components/headers/workspace-analytics";
segment: null,
project: null,
};
const tabsList = ["Scope and Demand", "Custom Analytics"]; const tabsList = ["Scope and Demand", "Custom Analytics"];
const Analytics = () => { const AnalyticsPage = observer(() => {
// router
const router = useRouter(); const router = useRouter();
const { workspaceSlug } = router.query; const { workspaceSlug } = router.query;
// store
const { workspace: workspaceStore, project: projectStore, user: userStore } = useMobxStore();
const { user } = useUserAuth(); const user = userStore.currentUser;
const { projects } = useProjects(); const projects = workspaceSlug ? projectStore.projects[workspaceSlug?.toString()] : null;
const { control, watch, setValue } = useForm<IAnalyticsParams>({ defaultValues }); // const { user } = useUserAuth();
// const { projects } = useProjects();
const params: IAnalyticsParams = {
x_axis: watch("x_axis"),
y_axis: watch("y_axis"),
segment: watch("segment"),
project: watch("project"),
};
const { data: analytics, error: analyticsError } = useSWR(
workspaceSlug ? ANALYTICS(workspaceSlug.toString(), params) : null,
workspaceSlug ? () => analyticsService.getAnalytics(workspaceSlug.toString(), params) : null
);
const trackAnalyticsEvent = (tab: string) => { const trackAnalyticsEvent = (tab: string) => {
const eventPayload = { const eventPayload = {
@ -83,70 +69,123 @@ const Analytics = () => {
}, [user, workspaceSlug]); }, [user, workspaceSlug]);
return ( return (
<WorkspaceAuthorizationLayout // <WorkspaceAuthorizationLayout
breadcrumbs={ // breadcrumbs={
<Breadcrumbs> // <Breadcrumbs>
<BreadcrumbItem title="Workspace Analytics" /> // <BreadcrumbItem title="Workspace Analytics" />
</Breadcrumbs> // </Breadcrumbs>
} // }
> // >
{projects ? ( // {projects ? (
projects.length > 0 ? ( // projects.length > 0 ? (
<div className="h-full flex flex-col overflow-hidden bg-custom-background-100"> // <div className="h-full flex flex-col overflow-hidden bg-custom-background-100">
<Tab.Group as={Fragment}> // <Tab.Group as={Fragment}>
<Tab.List as="div" className="space-x-2 border-b border-custom-border-200 px-5 py-3"> // <Tab.List as="div" className="space-x-2 border-b border-custom-border-200 px-5 py-3">
{tabsList.map((tab) => ( // {tabsList.map((tab) => (
<Tab // <Tab
key={tab} // key={tab}
className={({ selected }) => // className={({ selected }) =>
`rounded-3xl border border-custom-border-200 px-4 py-2 text-xs hover:bg-custom-background-80 ${ // `rounded-3xl border border-custom-border-200 px-4 py-2 text-xs hover:bg-custom-background-80 ${
selected ? "bg-custom-background-80" : "" // selected ? "bg-custom-background-80" : ""
}` // }`
} // }
onClick={() => trackAnalyticsEvent(tab)} // onClick={() => trackAnalyticsEvent(tab)}
> // >
{tab} // {tab}
</Tab> // </Tab>
))} // ))}
</Tab.List> // </Tab.List>
<Tab.Panels as={Fragment}> // <Tab.Panels as={Fragment}>
<Tab.Panel as={Fragment}> // <Tab.Panel as={Fragment}>
<ScopeAndDemand fullScreen /> // <ScopeAndDemand fullScreen />
</Tab.Panel> // </Tab.Panel>
<Tab.Panel as={Fragment}> // <Tab.Panel as={Fragment}>
<CustomAnalytics // <CustomAnalytics
analytics={analytics} // analytics={analytics}
analyticsError={analyticsError} // analyticsError={analyticsError}
params={params} // params={params}
control={control} // control={control}
setValue={setValue} // setValue={setValue}
user={user} // user={user}
fullScreen // fullScreen
/> // />
</Tab.Panel> // </Tab.Panel>
</Tab.Panels> // </Tab.Panels>
</Tab.Group> // </Tab.Group>
</div> // </div>
// ) : (
// <EmptyState
// title="You can see your all projects' analytics here"
// description="Let's create your first project and analyze the stats with various graphs."
// image={emptyAnalytics}
// primaryButton={{
// icon: <PlusIcon className="h-4 w-4" />,
// text: "New Project",
// onClick: () => {
// const e = new KeyboardEvent("keydown", {
// key: "p",
// });
// document.dispatchEvent(e);
// },
// }}
// />
// )
// ) : null}
// </WorkspaceAuthorizationLayout>
<AppLayout header={<WorkspaceAnalyticsHeader />}>
<>
{projects && projects.length > 0 ? (
<>
<div className="h-full flex flex-col overflow-hidden bg-custom-background-100">
<Tab.Group as={Fragment}>
<Tab.List as="div" className="space-x-2 border-b border-custom-border-200 px-5 py-3">
{tabsList.map((tab) => (
<Tab
key={tab}
className={({ selected }) =>
`rounded-3xl border border-custom-border-200 px-4 py-2 text-xs hover:bg-custom-background-80 ${
selected ? "bg-custom-background-80" : ""
}`
}
onClick={() => trackAnalyticsEvent(tab)}
>
{tab}
</Tab>
))}
</Tab.List>
<Tab.Panels as={Fragment}>
<Tab.Panel as={Fragment}>
<ScopeAndDemand fullScreen />
</Tab.Panel>
<Tab.Panel as={Fragment}>
<CustomAnalytics fullScreen />
</Tab.Panel>
</Tab.Panels>
</Tab.Group>
</div>
</>
) : ( ) : (
<EmptyState <>
title="You can see your all projects' analytics here" <EmptyState
description="Let's create your first project and analyse the stats with various graphs." title="You can see your all projects' analytics here"
image={emptyAnalytics} description="Let's create your first project and analyze the stats with various graphs."
primaryButton={{ image={emptyAnalytics}
icon: <PlusIcon className="h-4 w-4" />, primaryButton={{
text: "New Project", icon: <PlusIcon className="h-4 w-4" />,
onClick: () => { text: "New Project",
const e = new KeyboardEvent("keydown", { onClick: () => {
key: "p", const e = new KeyboardEvent("keydown", {
}); key: "p",
document.dispatchEvent(e); });
}, document.dispatchEvent(e);
}} },
/> }}
) />
) : null} </>
</WorkspaceAuthorizationLayout> )}
</>
</AppLayout>
); );
}; });
export default Analytics; export default AnalyticsPage;