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-07-26 12:21:26 +00:00
|
|
|
const { filters, groupBy, orderBy } = useMyIssuesFilters(workspaceSlug);
|
|
|
|
|
|
|
|
const params: any = {
|
|
|
|
assignees: filters?.assignees ? filters?.assignees.join(",") : undefined,
|
|
|
|
created_by: filters?.created_by ? filters?.created_by.join(",") : undefined,
|
|
|
|
group_by: groupBy,
|
|
|
|
labels: filters?.labels ? filters?.labels.join(",") : undefined,
|
|
|
|
order_by: orderBy,
|
|
|
|
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,
|
|
|
|
type: filters?.type ? filters?.type : undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
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-08-11 13:47:02 +00:00
|
|
|
if (groupBy === "state_detail.group") {
|
|
|
|
return myIssues
|
|
|
|
? Object.assign(
|
|
|
|
{
|
|
|
|
backlog: [],
|
|
|
|
unstarted: [],
|
|
|
|
started: [],
|
|
|
|
completed: [],
|
|
|
|
cancelled: [],
|
|
|
|
},
|
|
|
|
myIssues
|
|
|
|
)
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:21:26 +00:00
|
|
|
return myIssues;
|
2023-08-11 13:47:02 +00:00
|
|
|
}, [groupBy, 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;
|