2024-03-06 08:54:36 +00:00
|
|
|
import { useEffect } from "react";
|
2024-04-29 06:42:33 +00:00
|
|
|
import { observer } from "mobx-react";
|
2024-03-06 13:09:14 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { TIssuesByPriorityWidgetFilters, TIssuesByPriorityWidgetResponse } from "@plane/types";
|
2024-01-18 10:19:54 +00:00
|
|
|
// hooks
|
|
|
|
// components
|
|
|
|
import {
|
|
|
|
DurationFilterDropdown,
|
|
|
|
IssuesByPriorityEmptyState,
|
|
|
|
WidgetLoader,
|
|
|
|
WidgetProps,
|
2024-03-19 14:38:35 +00:00
|
|
|
} from "@/components/dashboard/widgets";
|
2024-01-18 10:19:54 +00:00
|
|
|
// helpers
|
|
|
|
// types
|
|
|
|
// constants
|
2024-03-19 14:38:35 +00:00
|
|
|
import { IssuesByPriorityGraph } from "@/components/graphs";
|
|
|
|
import { EDurationFilters } from "@/constants/dashboard";
|
|
|
|
import { getCustomDates } from "@/helpers/dashboard.helper";
|
|
|
|
import { useDashboard } from "@/hooks/store";
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
const WIDGET_KEY = "issues_by_priority";
|
|
|
|
|
|
|
|
export const IssuesByPriorityWidget: React.FC<WidgetProps> = observer((props) => {
|
|
|
|
const { dashboardId, workspaceSlug } = props;
|
2024-03-06 08:54:36 +00:00
|
|
|
// router
|
|
|
|
const router = useRouter();
|
2024-01-18 10:19:54 +00:00
|
|
|
// store hooks
|
2024-01-22 15:20:30 +00:00
|
|
|
const { fetchWidgetStats, getWidgetDetails, getWidgetStats, updateDashboardWidgetFilters } = useDashboard();
|
2024-02-08 12:27:22 +00:00
|
|
|
// derived values
|
2024-01-22 15:20:30 +00:00
|
|
|
const widgetDetails = getWidgetDetails(workspaceSlug, dashboardId, WIDGET_KEY);
|
|
|
|
const widgetStats = getWidgetStats<TIssuesByPriorityWidgetResponse[]>(workspaceSlug, dashboardId, WIDGET_KEY);
|
2024-03-06 08:54:36 +00:00
|
|
|
const selectedDuration = widgetDetails?.widget_filters.duration ?? EDurationFilters.NONE;
|
|
|
|
const selectedCustomDates = widgetDetails?.widget_filters.custom_dates ?? [];
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
const handleUpdateFilters = async (filters: Partial<TIssuesByPriorityWidgetFilters>) => {
|
|
|
|
if (!widgetDetails) return;
|
|
|
|
|
|
|
|
await updateDashboardWidgetFilters(workspaceSlug, dashboardId, widgetDetails.id, {
|
|
|
|
widgetKey: WIDGET_KEY,
|
|
|
|
filters,
|
|
|
|
});
|
|
|
|
|
2024-03-06 08:54:36 +00:00
|
|
|
const filterDates = getCustomDates(
|
|
|
|
filters.duration ?? selectedDuration,
|
|
|
|
filters.custom_dates ?? selectedCustomDates
|
|
|
|
);
|
2024-01-18 10:19:54 +00:00
|
|
|
fetchWidgetStats(workspaceSlug, dashboardId, {
|
|
|
|
widget_key: WIDGET_KEY,
|
2024-02-05 13:42:33 +00:00
|
|
|
...(filterDates.trim() !== "" ? { target_date: filterDates } : {}),
|
2024-01-18 10:19:54 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-03-06 08:54:36 +00:00
|
|
|
const filterDates = getCustomDates(selectedDuration, selectedCustomDates);
|
2024-01-24 14:11:02 +00:00
|
|
|
fetchWidgetStats(workspaceSlug, dashboardId, {
|
|
|
|
widget_key: WIDGET_KEY,
|
2024-02-05 13:42:33 +00:00
|
|
|
...(filterDates.trim() !== "" ? { target_date: filterDates } : {}),
|
2024-01-24 14:11:02 +00:00
|
|
|
});
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
if (!widgetDetails || !widgetStats) return <WidgetLoader widgetKey={WIDGET_KEY} />;
|
|
|
|
|
|
|
|
const totalCount = widgetStats.reduce((acc, item) => acc + item?.count, 0);
|
2024-03-06 08:54:36 +00:00
|
|
|
const chartData = widgetStats.map((item) => ({
|
|
|
|
priority: item?.priority,
|
|
|
|
priority_count: item?.count,
|
|
|
|
}));
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
return (
|
2024-03-06 13:09:14 +00:00
|
|
|
<div className="flex min-h-96 w-full flex-col overflow-hidden rounded-xl border-[0.5px] border-custom-border-200 bg-custom-background-100 py-6 duration-300 hover:shadow-custom-shadow-4xl">
|
2024-02-05 13:42:33 +00:00
|
|
|
<div className="flex items-center justify-between gap-2 pl-7 pr-6">
|
|
|
|
<Link
|
|
|
|
href={`/${workspaceSlug}/workspace-views/assigned`}
|
|
|
|
className="text-lg font-semibold text-custom-text-300 hover:underline"
|
|
|
|
>
|
|
|
|
Assigned by priority
|
|
|
|
</Link>
|
2024-01-18 10:19:54 +00:00
|
|
|
<DurationFilterDropdown
|
2024-03-06 08:54:36 +00:00
|
|
|
customDates={selectedCustomDates}
|
2024-02-08 12:27:22 +00:00
|
|
|
value={selectedDuration}
|
2024-03-06 08:54:36 +00:00
|
|
|
onChange={(val, customDates) =>
|
2024-01-18 10:19:54 +00:00
|
|
|
handleUpdateFilters({
|
2024-02-08 12:27:22 +00:00
|
|
|
duration: val,
|
2024-03-06 08:54:36 +00:00
|
|
|
...(val === "custom" ? { custom_dates: customDates } : {}),
|
2024-01-18 10:19:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{totalCount > 0 ? (
|
2024-03-06 13:09:14 +00:00
|
|
|
<div className="flex h-full items-center">
|
|
|
|
<div className="-mt-[11px] w-full">
|
2024-03-06 08:54:36 +00:00
|
|
|
<IssuesByPriorityGraph
|
2024-01-18 10:19:54 +00:00
|
|
|
data={chartData}
|
2024-03-06 08:54:36 +00:00
|
|
|
onBarClick={(datum) => {
|
|
|
|
router.push(
|
|
|
|
`/${workspaceSlug}/workspace-views/assigned?priority=${`${datum.data.priority}`.toLowerCase()}`
|
|
|
|
);
|
2024-01-18 10:19:54 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
2024-03-06 13:09:14 +00:00
|
|
|
<div className="grid h-full place-items-center">
|
2024-01-24 14:11:02 +00:00
|
|
|
<IssuesByPriorityEmptyState />
|
2024-01-18 10:19:54 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2024-01-22 15:20:30 +00:00
|
|
|
</div>
|
2024-01-18 10:19:54 +00:00
|
|
|
);
|
|
|
|
});
|