2024-01-18 10:19:54 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import Link from "next/link";
|
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { Tab } from "@headlessui/react";
|
|
|
|
// hooks
|
|
|
|
import { useDashboard } from "hooks/store";
|
|
|
|
// components
|
|
|
|
import {
|
|
|
|
DurationFilterDropdown,
|
|
|
|
TabsList,
|
|
|
|
WidgetIssuesList,
|
|
|
|
WidgetLoader,
|
|
|
|
WidgetProps,
|
|
|
|
} from "components/dashboard/widgets";
|
|
|
|
// helpers
|
|
|
|
import { getCustomDates, getRedirectionFilters } from "helpers/dashboard.helper";
|
|
|
|
// types
|
|
|
|
import { TCreatedIssuesWidgetFilters, TCreatedIssuesWidgetResponse } from "@plane/types";
|
|
|
|
// constants
|
2024-02-05 13:42:33 +00:00
|
|
|
import { FILTERED_ISSUES_TABS_LIST, UNFILTERED_ISSUES_TABS_LIST } from "constants/dashboard";
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
const WIDGET_KEY = "created_issues";
|
|
|
|
|
|
|
|
export const CreatedIssuesWidget: React.FC<WidgetProps> = observer((props) => {
|
|
|
|
const { dashboardId, workspaceSlug } = props;
|
|
|
|
// states
|
|
|
|
const [fetching, setFetching] = useState(false);
|
|
|
|
// store hooks
|
2024-01-22 15:20:30 +00:00
|
|
|
const { fetchWidgetStats, getWidgetDetails, getWidgetStats, 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<TCreatedIssuesWidgetResponse>(workspaceSlug, dashboardId, WIDGET_KEY);
|
2024-02-05 13:42:33 +00:00
|
|
|
const selectedTab = widgetDetails?.widget_filters.tab ?? "pending";
|
|
|
|
const selectedDurationFilter = widgetDetails?.widget_filters.target_date ?? "none";
|
2024-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
const handleUpdateFilters = async (filters: Partial<TCreatedIssuesWidgetFilters>) => {
|
|
|
|
if (!widgetDetails) return;
|
|
|
|
|
|
|
|
setFetching(true);
|
|
|
|
|
|
|
|
await updateDashboardWidgetFilters(workspaceSlug, dashboardId, widgetDetails.id, {
|
|
|
|
widgetKey: WIDGET_KEY,
|
|
|
|
filters,
|
|
|
|
});
|
|
|
|
|
2024-02-05 13:42:33 +00:00
|
|
|
const filterDates = getCustomDates(filters.target_date ?? selectedDurationFilter);
|
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
|
|
|
}).finally(() => setFetching(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-02-05 13:42:33 +00:00
|
|
|
const filterDates = getCustomDates(selectedDurationFilter);
|
|
|
|
|
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
|
|
|
});
|
|
|
|
// 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-01-18 10:19:54 +00:00
|
|
|
|
|
|
|
if (!widgetDetails || !widgetStats) return <WidgetLoader widgetKey={WIDGET_KEY} />;
|
|
|
|
|
|
|
|
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-02-05 13:42:33 +00:00
|
|
|
<div className="flex items-center justify-between gap-2 p-6 pl-7">
|
|
|
|
<Link
|
|
|
|
href={`/${workspaceSlug}/workspace-views/created/${filterParams}`}
|
|
|
|
className="text-lg font-semibold text-custom-text-300 hover:underline"
|
|
|
|
>
|
|
|
|
Created by you
|
|
|
|
</Link>
|
2024-01-18 10:19:54 +00:00
|
|
|
<DurationFilterDropdown
|
2024-02-05 13:42:33 +00:00
|
|
|
value={selectedDurationFilter}
|
|
|
|
onChange={(val) => {
|
|
|
|
if (val === selectedDurationFilter) return;
|
|
|
|
|
|
|
|
// switch to pending tab if target date is changed to none
|
|
|
|
if (val === "none" && selectedTab !== "completed") {
|
|
|
|
handleUpdateFilters({ target_date: val, tab: "pending" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// switch to upcoming tab if target date is changed to other than none
|
|
|
|
if (val !== "none" && selectedDurationFilter === "none" && selectedTab !== "completed") {
|
|
|
|
handleUpdateFilters({
|
|
|
|
target_date: val,
|
|
|
|
tab: "upcoming",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateFilters({ target_date: val });
|
|
|
|
}}
|
2024-01-18 10:19:54 +00:00
|
|
|
/>
|
2024-01-22 15:20:30 +00:00
|
|
|
</div>
|
2024-01-18 10:19:54 +00:00
|
|
|
<Tab.Group
|
|
|
|
as="div"
|
2024-02-05 13:42:33 +00:00
|
|
|
selectedIndex={tabsList.findIndex((tab) => tab.key === selectedTab)}
|
2024-01-18 10:19:54 +00:00
|
|
|
onChange={(i) => {
|
2024-02-05 13:42:33 +00:00
|
|
|
const selectedTab = tabsList[i];
|
|
|
|
handleUpdateFilters({ tab: selectedTab.key ?? "pending" });
|
2024-01-18 10:19:54 +00:00
|
|
|
}}
|
|
|
|
className="h-full flex flex-col"
|
|
|
|
>
|
|
|
|
<div className="px-6">
|
2024-02-05 13:42:33 +00:00
|
|
|
<TabsList durationFilter={selectedDurationFilter} selectedTab={selectedTab} />
|
2024-01-18 10:19:54 +00:00
|
|
|
</div>
|
2024-01-24 14:11:02 +00:00
|
|
|
<Tab.Panels as="div" className="h-full">
|
2024-02-05 13:42:33 +00:00
|
|
|
{tabsList.map((tab) => (
|
2024-01-29 15:05:23 +00:00
|
|
|
<Tab.Panel key={tab.key} as="div" className="h-full flex flex-col">
|
2024-01-18 10:19:54 +00:00
|
|
|
<WidgetIssuesList
|
|
|
|
issues={widgetStats.issues}
|
|
|
|
tab={tab.key}
|
|
|
|
totalIssues={widgetStats.count}
|
|
|
|
type="created"
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
isLoading={fetching}
|
|
|
|
/>
|
|
|
|
</Tab.Panel>
|
|
|
|
))}
|
|
|
|
</Tab.Panels>
|
|
|
|
</Tab.Group>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|