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-04-22 12:49:35 +00:00
|
|
|
STATES_LIST,
|
2023-05-10 20:45:39 +00:00
|
|
|
VIEW_ISSUES,
|
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,
|
2023-03-30 20:47:35 +00:00
|
|
|
setIssueView,
|
2023-03-15 06:14:44 +00:00
|
|
|
} = useContext(issueViewContext);
|
|
|
|
|
|
|
|
const router = useRouter();
|
2023-05-10 20:45:39 +00:00
|
|
|
const { workspaceSlug, projectId, cycleId, moduleId, viewId } = 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-05-10 20:45:39 +00:00
|
|
|
const { data: viewIssues } = useSWR(
|
|
|
|
workspaceSlug && projectId && viewId && params ? VIEW_ISSUES(viewId.toString(), params) : null,
|
|
|
|
workspaceSlug && projectId && viewId && params
|
|
|
|
? () =>
|
|
|
|
issuesService.getIssuesWithParams(workspaceSlug.toString(), projectId.toString(), params)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-03-22 19:30:50 +00:00
|
|
|
const { data: states } = useSWR(
|
2023-04-22 12:49:35 +00:00
|
|
|
workspaceSlug && projectId ? STATES_LIST(projectId as string) : null,
|
2023-03-22 19:30:50 +00:00
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => stateService.getStates(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
const statesList = getStatesList(states ?? {});
|
2023-05-10 20:45:39 +00:00
|
|
|
const activeStatesList = statesList.filter(
|
|
|
|
(state) => state.group === "started" || state.group === "unstarted"
|
|
|
|
);
|
|
|
|
const backlogStatesList = statesList.filter((state) => state.group === "backlog");
|
|
|
|
|
|
|
|
const stateIds =
|
|
|
|
filters && filters?.type === "active"
|
|
|
|
? activeStatesList.map((state) => state.id)
|
|
|
|
: filters?.type === "backlog"
|
|
|
|
? backlogStatesList.map((state) => state.id)
|
|
|
|
: statesList.map((state) => state.id);
|
2023-03-22 20:43:52 +00:00
|
|
|
|
2023-04-03 12:34:45 +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-05-10 20:45:39 +00:00
|
|
|
const issuesToGroup = cycleId
|
|
|
|
? cycleIssues
|
|
|
|
: moduleId
|
|
|
|
? moduleIssues
|
|
|
|
: viewId
|
|
|
|
? viewIssues
|
|
|
|
: 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-04-03 09:37:39 +00:00
|
|
|
}, [
|
|
|
|
projectIssues,
|
|
|
|
cycleIssues,
|
|
|
|
moduleIssues,
|
2023-05-10 20:45:39 +00:00
|
|
|
viewIssues,
|
2023-04-03 09:37:39 +00:00
|
|
|
groupByProperty,
|
|
|
|
cycleId,
|
|
|
|
moduleId,
|
2023-05-10 20:45:39 +00:00
|
|
|
viewId,
|
2023-04-03 09:37:39 +00:00
|
|
|
emptyStatesObject,
|
|
|
|
]);
|
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,
|
2023-03-29 20:31:53 +00:00
|
|
|
setIssueView,
|
2023-03-15 06:14:44 +00:00
|
|
|
} as const;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useIssuesView;
|