2023-03-15 06:14:44 +00:00
|
|
|
import { useContext, useMemo } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// contexts
|
|
|
|
import { issueViewContext } from "contexts/issue-view.context";
|
|
|
|
// services
|
|
|
|
import issuesService from "services/issues.service";
|
|
|
|
import cyclesService from "services/cycles.service";
|
|
|
|
import modulesService from "services/modules.service";
|
2023-03-27 20:22:13 +00:00
|
|
|
import stateService from "services/state.service";
|
|
|
|
// helpers
|
|
|
|
import { getStatesList } from "helpers/state.helper";
|
|
|
|
// types
|
|
|
|
import type { IIssue } from "types";
|
2023-03-15 06:14:44 +00:00
|
|
|
// fetch-keys
|
|
|
|
import {
|
|
|
|
CYCLE_ISSUES_WITH_PARAMS,
|
|
|
|
MODULE_ISSUES_WITH_PARAMS,
|
|
|
|
PROJECT_ISSUES_LIST_WITH_PARAMS,
|
2023-03-22 19:30:50 +00:00
|
|
|
STATE_LIST,
|
2023-03-15 06:14:44 +00:00
|
|
|
} from "constants/fetch-keys";
|
|
|
|
|
|
|
|
const useIssuesView = () => {
|
|
|
|
const {
|
|
|
|
issueView,
|
|
|
|
groupByProperty,
|
|
|
|
setGroupByProperty,
|
|
|
|
orderBy,
|
|
|
|
setOrderBy,
|
2023-03-22 20:43:52 +00:00
|
|
|
showEmptyGroups,
|
|
|
|
setShowEmptyGroups,
|
2023-03-15 06:14:44 +00:00
|
|
|
filters,
|
|
|
|
setFilters,
|
|
|
|
resetFilterToDefault,
|
|
|
|
setNewFilterDefaultView,
|
|
|
|
setIssueViewToKanban,
|
|
|
|
setIssueViewToList,
|
|
|
|
} = useContext(issueViewContext);
|
|
|
|
|
|
|
|
const router = useRouter();
|
2023-03-22 12:48:19 +00:00
|
|
|
const { workspaceSlug, projectId, cycleId, moduleId } = router.query;
|
2023-03-15 06:14:44 +00:00
|
|
|
|
|
|
|
const params: any = {
|
|
|
|
order_by: orderBy,
|
|
|
|
group_by: groupByProperty,
|
2023-03-16 08:37:19 +00:00
|
|
|
assignees: filters?.assignees ? filters?.assignees.join(",") : undefined,
|
|
|
|
state: filters?.state ? filters?.state.join(",") : undefined,
|
|
|
|
priority: filters?.priority ? filters?.priority.join(",") : undefined,
|
|
|
|
type: filters?.type ? filters?.type : undefined,
|
|
|
|
labels: filters?.labels ? filters?.labels.join(",") : undefined,
|
|
|
|
issue__assignees__id: filters?.issue__assignees__id
|
|
|
|
? filters?.issue__assignees__id.join(",")
|
|
|
|
: undefined,
|
|
|
|
issue__labels__id: filters?.issue__labels__id
|
|
|
|
? filters?.issue__labels__id.join(",")
|
2023-03-15 06:14:44 +00:00
|
|
|
: undefined,
|
2023-03-24 18:01:56 +00:00
|
|
|
created_by: filters?.created_by ? filters?.created_by.join(",") : undefined,
|
2023-03-15 06:14:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const { data: projectIssues } = useSWR(
|
|
|
|
workspaceSlug && projectId && params
|
2023-03-16 12:45:08 +00:00
|
|
|
? PROJECT_ISSUES_LIST_WITH_PARAMS(projectId as string, params)
|
2023-03-15 06:14:44 +00:00
|
|
|
: null,
|
|
|
|
workspaceSlug && projectId && params
|
|
|
|
? () =>
|
|
|
|
issuesService.getIssuesWithParams(workspaceSlug as string, projectId as string, params)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const { data: cycleIssues } = useSWR(
|
|
|
|
workspaceSlug && projectId && cycleId && params
|
2023-03-16 12:45:08 +00:00
|
|
|
? CYCLE_ISSUES_WITH_PARAMS(cycleId as string, params)
|
2023-03-15 06:14:44 +00:00
|
|
|
: null,
|
|
|
|
workspaceSlug && projectId && cycleId && params
|
|
|
|
? () =>
|
|
|
|
cyclesService.getCycleIssuesWithParams(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
cycleId as string,
|
|
|
|
params
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const { data: moduleIssues } = useSWR(
|
|
|
|
workspaceSlug && projectId && moduleId && params
|
2023-03-16 12:45:08 +00:00
|
|
|
? MODULE_ISSUES_WITH_PARAMS(moduleId as string, params)
|
2023-03-15 06:14:44 +00:00
|
|
|
: null,
|
|
|
|
workspaceSlug && projectId && moduleId && params
|
|
|
|
? () =>
|
|
|
|
modulesService.getModuleIssuesWithParams(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
moduleId as string,
|
|
|
|
params
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-03-22 19:30:50 +00:00
|
|
|
const { data: states } = useSWR(
|
|
|
|
workspaceSlug && projectId ? STATE_LIST(projectId as string) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => stateService.getStates(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
const statesList = getStatesList(states ?? {});
|
|
|
|
const stateIds = statesList.map((state) => state.id);
|
2023-03-22 20:43:52 +00:00
|
|
|
|
2023-03-23 18:06:52 +00:00
|
|
|
const emptyStatesObject: { [key: string]: [] } = {};
|
2023-03-22 19:30:50 +00:00
|
|
|
for (let i = 0; i < stateIds.length; i++) {
|
|
|
|
emptyStatesObject[stateIds[i]] = [];
|
|
|
|
}
|
|
|
|
|
2023-03-15 06:14:44 +00:00
|
|
|
const groupedByIssues:
|
|
|
|
| {
|
|
|
|
[key: string]: IIssue[];
|
|
|
|
}
|
|
|
|
| undefined = useMemo(() => {
|
2023-03-22 12:48:19 +00:00
|
|
|
const issuesToGroup = cycleIssues ?? moduleIssues ?? projectIssues;
|
2023-03-15 06:14:44 +00:00
|
|
|
|
|
|
|
if (Array.isArray(issuesToGroup)) return { allIssues: issuesToGroup };
|
2023-03-23 18:06:52 +00:00
|
|
|
if (groupByProperty === "state")
|
|
|
|
return issuesToGroup ? Object.assign(emptyStatesObject, issuesToGroup) : undefined;
|
2023-03-22 19:30:50 +00:00
|
|
|
|
|
|
|
return issuesToGroup;
|
2023-03-27 20:22:13 +00:00
|
|
|
}, [projectIssues, cycleIssues, moduleIssues, groupByProperty]);
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-03-23 18:06:52 +00:00
|
|
|
const isEmpty =
|
|
|
|
Object.values(groupedByIssues ?? {}).every((group) => group.length === 0) ||
|
|
|
|
Object.keys(groupedByIssues ?? {}).length === 0;
|
|
|
|
|
2023-03-15 06:14:44 +00:00
|
|
|
return {
|
|
|
|
groupedByIssues,
|
|
|
|
issueView,
|
|
|
|
groupByProperty,
|
|
|
|
setGroupByProperty,
|
|
|
|
orderBy,
|
|
|
|
setOrderBy,
|
2023-03-22 20:43:52 +00:00
|
|
|
showEmptyGroups,
|
|
|
|
setShowEmptyGroups,
|
2023-03-15 06:14:44 +00:00
|
|
|
filters,
|
|
|
|
setFilters,
|
|
|
|
params,
|
2023-03-23 18:06:52 +00:00
|
|
|
isNotEmpty: !isEmpty,
|
2023-03-15 06:14:44 +00:00
|
|
|
resetFilterToDefault,
|
|
|
|
setNewFilterDefaultView,
|
|
|
|
setIssueViewToKanban,
|
|
|
|
setIssueViewToList,
|
|
|
|
} as const;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useIssuesView;
|