2023-07-26 12:21:26 +00:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
2023-08-01 13:10:04 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-07-26 12:21:26 +00:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import userService from "services/user.service";
|
|
|
|
// hooks
|
|
|
|
import useMyIssuesFilters from "hooks/my-issues/use-my-issues-filter";
|
|
|
|
// types
|
|
|
|
import { IIssue } from "types";
|
|
|
|
// fetch-keys
|
|
|
|
import { USER_ISSUES } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
const useMyIssues = (workspaceSlug: string | undefined) => {
|
2023-08-01 13:10:04 +00:00
|
|
|
const router = useRouter();
|
|
|
|
|
2023-09-12 16:57:15 +00:00
|
|
|
const { filters, displayFilters } = useMyIssuesFilters(workspaceSlug);
|
2023-07-26 12:21:26 +00:00
|
|
|
|
|
|
|
const params: any = {
|
|
|
|
assignees: filters?.assignees ? filters?.assignees.join(",") : undefined,
|
|
|
|
created_by: filters?.created_by ? filters?.created_by.join(",") : undefined,
|
2023-09-12 16:57:15 +00:00
|
|
|
group_by: displayFilters?.group_by,
|
2023-07-26 12:21:26 +00:00
|
|
|
labels: filters?.labels ? filters?.labels.join(",") : undefined,
|
2023-09-12 16:57:15 +00:00
|
|
|
order_by: displayFilters?.order_by,
|
2023-07-26 12:21:26 +00:00
|
|
|
priority: filters?.priority ? filters?.priority.join(",") : undefined,
|
|
|
|
state_group: filters?.state_group ? filters?.state_group.join(",") : undefined,
|
2023-08-08 07:45:25 +00:00
|
|
|
subscriber: filters?.subscriber ? filters?.subscriber.join(",") : undefined,
|
2023-08-24 14:15:23 +00:00
|
|
|
start_date: filters?.start_date ? filters?.start_date.join(",") : undefined,
|
2023-07-26 12:21:26 +00:00
|
|
|
target_date: filters?.target_date ? filters?.target_date.join(",") : undefined,
|
2023-09-12 16:57:15 +00:00
|
|
|
type: displayFilters?.type,
|
2023-07-26 12:21:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const { data: myIssues, mutate: mutateMyIssues } = useSWR(
|
2023-08-01 13:10:04 +00:00
|
|
|
workspaceSlug && router.pathname.includes("my-issues")
|
|
|
|
? USER_ISSUES(workspaceSlug.toString(), params)
|
|
|
|
: null,
|
|
|
|
workspaceSlug && router.pathname.includes("my-issues")
|
|
|
|
? () => userService.userIssues(workspaceSlug.toString(), params)
|
|
|
|
: null
|
2023-07-26 12:21:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const groupedIssues:
|
|
|
|
| {
|
|
|
|
[key: string]: IIssue[];
|
|
|
|
}
|
|
|
|
| undefined = useMemo(() => {
|
|
|
|
if (!myIssues) return undefined;
|
|
|
|
|
|
|
|
if (Array.isArray(myIssues))
|
|
|
|
return {
|
|
|
|
allIssues: myIssues,
|
|
|
|
};
|
|
|
|
|
2023-09-12 16:57:15 +00:00
|
|
|
if (displayFilters?.group_by === "state_detail.group") {
|
2023-08-11 13:47:02 +00:00
|
|
|
return myIssues
|
|
|
|
? Object.assign(
|
|
|
|
{
|
|
|
|
backlog: [],
|
|
|
|
unstarted: [],
|
|
|
|
started: [],
|
|
|
|
completed: [],
|
|
|
|
cancelled: [],
|
|
|
|
},
|
|
|
|
myIssues
|
|
|
|
)
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:21:26 +00:00
|
|
|
return myIssues;
|
2023-09-12 16:57:15 +00:00
|
|
|
}, [displayFilters, myIssues]);
|
2023-07-26 12:21:26 +00:00
|
|
|
|
|
|
|
const isEmpty =
|
|
|
|
Object.values(groupedIssues ?? {}).every((group) => group.length === 0) ||
|
|
|
|
Object.keys(groupedIssues ?? {}).length === 0;
|
|
|
|
|
|
|
|
return {
|
|
|
|
groupedIssues,
|
|
|
|
isEmpty,
|
|
|
|
mutateMyIssues,
|
|
|
|
params,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useMyIssues;
|