import { useEffect, useState } from "react"; import { observer } from "mobx-react-lite"; import Link from "next/link"; import { Tab } from "@headlessui/react"; import { TCreatedIssuesWidgetFilters, TCreatedIssuesWidgetResponse } from "@plane/types"; // hooks import { DurationFilterDropdown, IssuesErrorState, TabsList, WidgetIssuesList, WidgetLoader, WidgetProps, } from "@/components/dashboard/widgets"; import { EDurationFilters, FILTERED_ISSUES_TABS_LIST, UNFILTERED_ISSUES_TABS_LIST } from "@/constants/dashboard"; import { getCustomDates, getRedirectionFilters, getTabKey } from "@/helpers/dashboard.helper"; import { useDashboard } from "@/hooks/store"; // components // helpers // types // constants const WIDGET_KEY = "created_issues"; export const CreatedIssuesWidget: React.FC = observer((props) => { const { dashboardId, workspaceSlug } = props; // states const [fetching, setFetching] = useState(false); // store hooks const { fetchWidgetStats, getWidgetDetails, getWidgetStats, getWidgetStatsError, updateDashboardWidgetFilters } = useDashboard(); // derived values const widgetDetails = getWidgetDetails(workspaceSlug, dashboardId, WIDGET_KEY); const widgetStats = getWidgetStats(workspaceSlug, dashboardId, WIDGET_KEY); const widgetStatsError = getWidgetStatsError(workspaceSlug, dashboardId, WIDGET_KEY); const selectedDurationFilter = widgetDetails?.widget_filters.duration ?? EDurationFilters.NONE; const selectedTab = getTabKey(selectedDurationFilter, widgetDetails?.widget_filters.tab); const selectedCustomDates = widgetDetails?.widget_filters.custom_dates ?? []; const handleUpdateFilters = async (filters: Partial) => { if (!widgetDetails) return; setFetching(true); await updateDashboardWidgetFilters(workspaceSlug, dashboardId, widgetDetails.id, { widgetKey: WIDGET_KEY, filters, }); const filterDates = getCustomDates( filters.duration ?? selectedDurationFilter, filters.custom_dates ?? selectedCustomDates ); fetchWidgetStats(workspaceSlug, dashboardId, { widget_key: WIDGET_KEY, issue_type: filters.tab ?? selectedTab, ...(filterDates.trim() !== "" ? { target_date: filterDates } : {}), }).finally(() => setFetching(false)); }; useEffect(() => { const filterDates = getCustomDates(selectedDurationFilter, selectedCustomDates); fetchWidgetStats(workspaceSlug, dashboardId, { widget_key: WIDGET_KEY, issue_type: selectedTab, ...(filterDates.trim() !== "" ? { target_date: filterDates } : {}), }); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const filterParams = getRedirectionFilters(selectedTab); const tabsList = selectedDurationFilter === "none" ? UNFILTERED_ISSUES_TABS_LIST : FILTERED_ISSUES_TABS_LIST; const selectedTabIndex = tabsList.findIndex((tab) => tab.key === selectedTab); if ((!widgetDetails || !widgetStats) && !widgetStatsError) return ; return (
{widgetStatsError ? ( handleUpdateFilters({ duration: EDurationFilters.NONE, tab: "pending", }) } /> ) : ( widgetStats && ( <>
Created by you { if (val === "custom" && customDates) { handleUpdateFilters({ duration: val, custom_dates: customDates, }); return; } if (val === selectedDurationFilter) return; let newTab = selectedTab; // switch to pending tab if target date is changed to none if (val === "none" && selectedTab !== "completed") newTab = "pending"; // switch to upcoming tab if target date is changed to other than none if (val !== "none" && selectedDurationFilter === "none" && selectedTab !== "completed") newTab = "upcoming"; handleUpdateFilters({ duration: val, tab: newTab, }); }} />
{ const newSelectedTab = tabsList[i]; handleUpdateFilters({ tab: newSelectedTab.key ?? "completed" }); }} className="h-full flex flex-col" >
{tabsList.map((tab) => { if (tab.key !== selectedTab) return null; return ( ); })}
) )}
); });