2024-01-18 10:19:54 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { observer } from "mobx-react-lite";
|
2024-03-06 13:09:14 +00:00
|
|
|
import Link from "next/link";
|
2024-01-18 10:19:54 +00:00
|
|
|
import { Tab } from "@headlessui/react";
|
|
|
|
// hooks
|
2024-03-06 13:16:36 +00:00
|
|
|
import { useDashboard } from "hooks/store";
|
|
|
|
// components
|
2024-01-18 10:19:54 +00:00
|
|
|
import {
|
|
|
|
DurationFilterDropdown,
|
2024-03-11 15:42:28 +00:00
|
|
|
IssuesErrorState,
|
2024-01-18 10:19:54 +00:00
|
|
|
TabsList,
|
|
|
|
WidgetIssuesList,
|
|
|
|
WidgetLoader,
|
|
|
|
WidgetProps,
|
|
|
|
} from "components/dashboard/widgets";
|
2024-03-06 13:09:14 +00:00
|
|
|
// helpers
|
2024-03-06 13:16:36 +00:00
|
|
|
import { getCustomDates, getRedirectionFilters, getTabKey } from "helpers/dashboard.helper";
|
2024-01-18 10:19:54 +00:00
|
|
|
// types
|
2024-03-06 13:16:36 +00:00
|
|
|
import { TAssignedIssuesWidgetFilters, TAssignedIssuesWidgetResponse } from "@plane/types";
|
2024-01-18 10:19:54 +00:00
|
|
|
// constants
|
2024-03-06 13:16:36 +00:00
|
|
|
import { EDurationFilters, FILTERED_ISSUES_TABS_LIST, UNFILTERED_ISSUES_TABS_LIST } from "constants/dashboard";
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
const WIDGET_KEY = "assigned_issues";
|
|
|
|
|
|
|
|
export const AssignedIssuesWidget: React.FC<WidgetProps> = observer((props) => {
|
|
|
|
const { dashboardId, workspaceSlug } = props;
|
|
|
|
// states
|
|
|
|
const [fetching, setFetching] = useState(false);
|
|
|
|
// store hooks
|
2024-03-11 15:42:28 +00:00
|
|
|
const { fetchWidgetStats, getWidgetDetails, getWidgetStats, getWidgetStatsError, updateDashboardWidgetFilters } =
|
|
|
|
useDashboard();
|
2024-01-18 10:19:54 +00:00
|
|
|
// derived values
|
2024-01-22 15:20:30 +00:00
|
|
|
const widgetDetails = getWidgetDetails(workspaceSlug, dashboardId, WIDGET_KEY);
|
|
|
|
const widgetStats = getWidgetStats<TAssignedIssuesWidgetResponse>(workspaceSlug, dashboardId, WIDGET_KEY);
|
2024-03-11 15:42:28 +00:00
|
|
|
const widgetStatsError = getWidgetStatsError(workspaceSlug, dashboardId, WIDGET_KEY);
|
2024-03-06 08:54:36 +00:00
|
|
|
const selectedDurationFilter = widgetDetails?.widget_filters.duration ?? EDurationFilters.NONE;
|
2024-02-08 12:27:22 +00:00
|
|
|
const selectedTab = getTabKey(selectedDurationFilter, widgetDetails?.widget_filters.tab);
|
2024-03-06 08:54:36 +00:00
|
|
|
const selectedCustomDates = widgetDetails?.widget_filters.custom_dates ?? [];
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
const handleUpdateFilters = async (filters: Partial<TAssignedIssuesWidgetFilters>) => {
|
|
|
|
if (!widgetDetails) return;
|
|
|
|
|
|
|
|
setFetching(true);
|
|
|
|
|
|
|
|
await updateDashboardWidgetFilters(workspaceSlug, dashboardId, widgetDetails.id, {
|
|
|
|
widgetKey: WIDGET_KEY,
|
|
|
|
filters,
|
|
|
|
});
|
|
|
|
|
2024-03-06 08:54:36 +00:00
|
|
|
const filterDates = getCustomDates(
|
|
|
|
filters.duration ?? selectedDurationFilter,
|
|
|
|
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
|
|
|
issue_type: filters.tab ?? selectedTab,
|
|
|
|
...(filterDates.trim() !== "" ? { target_date: filterDates } : {}),
|
2024-01-18 10:19:54 +00:00
|
|
|
expand: "issue_relation",
|
|
|
|
}).finally(() => setFetching(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-03-06 08:54:36 +00:00
|
|
|
const filterDates = getCustomDates(selectedDurationFilter, selectedCustomDates);
|
2024-01-18 10:19:54 +00:00
|
|
|
|
2024-01-24 14:11:02 +00:00
|
|
|
fetchWidgetStats(workspaceSlug, dashboardId, {
|
|
|
|
widget_key: WIDGET_KEY,
|
2024-02-05 13:42:33 +00:00
|
|
|
issue_type: selectedTab,
|
|
|
|
...(filterDates.trim() !== "" ? { target_date: filterDates } : {}),
|
2024-01-24 14:11:02 +00:00
|
|
|
expand: "issue_relation",
|
|
|
|
});
|
2024-02-05 13:42:33 +00:00
|
|
|
|
2024-01-24 14:11:02 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
2024-01-18 10:19:54 +00:00
|
|
|
|
2024-02-05 13:42:33 +00:00
|
|
|
const filterParams = getRedirectionFilters(selectedTab);
|
|
|
|
const tabsList = selectedDurationFilter === "none" ? UNFILTERED_ISSUES_TABS_LIST : FILTERED_ISSUES_TABS_LIST;
|
2024-02-26 08:09:29 +00:00
|
|
|
const selectedTabIndex = tabsList.findIndex((tab) => tab.key === selectedTab);
|
2024-01-18 10:19:54 +00:00
|
|
|
|
2024-03-11 15:42:28 +00:00
|
|
|
if ((!widgetDetails || !widgetStats) && !widgetStatsError) return <WidgetLoader widgetKey={WIDGET_KEY} />;
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
return (
|
2024-01-24 14:11:02 +00:00
|
|
|
<div className="bg-custom-background-100 rounded-xl border-[0.5px] border-custom-border-200 w-full hover:shadow-custom-shadow-4xl duration-300 flex flex-col min-h-96">
|
2024-03-11 15:42:28 +00:00
|
|
|
{widgetStatsError ? (
|
|
|
|
<IssuesErrorState
|
|
|
|
isRefreshing={fetching}
|
|
|
|
onClick={() =>
|
2024-02-26 08:09:29 +00:00
|
|
|
handleUpdateFilters({
|
2024-03-11 15:42:28 +00:00
|
|
|
duration: EDurationFilters.NONE,
|
|
|
|
tab: "pending",
|
|
|
|
})
|
|
|
|
}
|
2024-01-18 10:19:54 +00:00
|
|
|
/>
|
2024-03-11 15:42:28 +00:00
|
|
|
) : (
|
|
|
|
widgetStats && (
|
|
|
|
<>
|
|
|
|
<div className="flex items-center justify-between gap-2 p-6 pl-7">
|
|
|
|
<Link
|
|
|
|
href={`/${workspaceSlug}/workspace-views/assigned/${filterParams}`}
|
|
|
|
className="text-lg font-semibold text-custom-text-300 hover:underline"
|
|
|
|
>
|
|
|
|
Assigned to you
|
|
|
|
</Link>
|
|
|
|
<DurationFilterDropdown
|
|
|
|
customDates={selectedCustomDates}
|
|
|
|
value={selectedDurationFilter}
|
|
|
|
onChange={(val, customDates) => {
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Tab.Group
|
|
|
|
as="div"
|
|
|
|
selectedIndex={selectedTabIndex}
|
|
|
|
onChange={(i) => {
|
|
|
|
const newSelectedTab = tabsList[i];
|
|
|
|
handleUpdateFilters({ tab: newSelectedTab?.key ?? "completed" });
|
|
|
|
}}
|
|
|
|
className="h-full flex flex-col"
|
|
|
|
>
|
|
|
|
<div className="px-6">
|
|
|
|
<TabsList durationFilter={selectedDurationFilter} selectedTab={selectedTab} />
|
|
|
|
</div>
|
|
|
|
<Tab.Panels as="div" className="h-full">
|
|
|
|
{tabsList.map((tab) => {
|
|
|
|
if (tab.key !== selectedTab) return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tab.Panel key={tab.key} as="div" className="h-full flex flex-col" static>
|
|
|
|
<WidgetIssuesList
|
|
|
|
tab={tab.key}
|
|
|
|
type="assigned"
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
widgetStats={widgetStats}
|
|
|
|
isLoading={fetching}
|
|
|
|
/>
|
|
|
|
</Tab.Panel>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Tab.Panels>
|
|
|
|
</Tab.Group>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
)}
|
2024-01-18 10:19:54 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|