2023-07-28 08:09:42 +00:00
|
|
|
import { useContext, useEffect, useMemo } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import userService from "services/user.service";
|
|
|
|
// contexts
|
|
|
|
import { profileIssuesContext } from "contexts/profile-issues-context";
|
|
|
|
// types
|
|
|
|
import { IIssue } from "types";
|
|
|
|
// fetch-keys
|
|
|
|
import { USER_PROFILE_ISSUES } from "constants/fetch-keys";
|
2023-07-31 12:41:25 +00:00
|
|
|
import { useWorkspaceMyMembership } from "contexts/workspace-member.context";
|
2023-07-28 08:09:42 +00:00
|
|
|
|
|
|
|
const useProfileIssues = (workspaceSlug: string | undefined, userId: string | undefined) => {
|
|
|
|
const {
|
2023-09-12 16:57:15 +00:00
|
|
|
display_filters: displayFilters,
|
|
|
|
setDisplayFilters,
|
2023-07-28 08:09:42 +00:00
|
|
|
filters,
|
|
|
|
setFilters,
|
2023-09-12 16:57:15 +00:00
|
|
|
display_properties: displayProperties,
|
2023-07-28 08:09:42 +00:00
|
|
|
setProperties,
|
|
|
|
} = useContext(profileIssuesContext);
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
2023-07-31 12:41:25 +00:00
|
|
|
const { memberRole } = useWorkspaceMyMembership();
|
|
|
|
|
2023-07-28 08:09:42 +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 17:21:13 +00:00
|
|
|
group_by: displayFilters?.group_by,
|
2023-07-28 08:09:42 +00:00
|
|
|
labels: filters?.labels ? filters?.labels.join(",") : undefined,
|
2023-09-12 17:21:13 +00:00
|
|
|
order_by: displayFilters?.order_by,
|
2023-07-28 08:09:42 +00:00
|
|
|
priority: filters?.priority ? filters?.priority.join(",") : undefined,
|
|
|
|
state_group: filters?.state_group ? filters?.state_group.join(",") : undefined,
|
2023-08-24 14:15:23 +00:00
|
|
|
start_date: filters?.start_date ? filters?.start_date.join(",") : undefined,
|
2023-07-28 08:09:42 +00:00
|
|
|
target_date: filters?.target_date ? filters?.target_date.join(",") : undefined,
|
2023-09-12 16:57:15 +00:00
|
|
|
type: displayFilters?.type ? displayFilters?.type : undefined,
|
2023-08-08 07:45:25 +00:00
|
|
|
subscriber: filters?.subscriber ? filters?.subscriber.join(",") : undefined,
|
2023-07-28 08:09:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const { data: userProfileIssues, mutate: mutateProfileIssues } = useSWR(
|
2023-07-31 12:41:25 +00:00
|
|
|
workspaceSlug && userId && (memberRole.isOwner || memberRole.isMember || memberRole.isViewer)
|
2023-07-28 08:09:42 +00:00
|
|
|
? USER_PROFILE_ISSUES(workspaceSlug.toString(), userId.toString(), params)
|
|
|
|
: null,
|
2023-07-31 12:41:25 +00:00
|
|
|
workspaceSlug && userId && (memberRole.isOwner || memberRole.isMember || memberRole.isViewer)
|
2023-07-28 08:09:42 +00:00
|
|
|
? () => userService.getUserProfileIssues(workspaceSlug.toString(), userId.toString(), params)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const groupedIssues:
|
|
|
|
| {
|
|
|
|
[key: string]: IIssue[];
|
|
|
|
}
|
|
|
|
| undefined = useMemo(() => {
|
|
|
|
if (!userProfileIssues) return undefined;
|
|
|
|
|
|
|
|
if (Array.isArray(userProfileIssues))
|
|
|
|
return {
|
|
|
|
allIssues: userProfileIssues,
|
|
|
|
};
|
|
|
|
|
2023-09-12 17:21:13 +00:00
|
|
|
if (displayFilters?.group_by === "state_detail.group") {
|
2023-08-30 11:58:17 +00:00
|
|
|
return userProfileIssues
|
|
|
|
? Object.assign(
|
|
|
|
{
|
|
|
|
backlog: [],
|
|
|
|
unstarted: [],
|
|
|
|
started: [],
|
|
|
|
completed: [],
|
|
|
|
cancelled: [],
|
|
|
|
},
|
|
|
|
userProfileIssues
|
|
|
|
)
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2023-07-28 08:09:42 +00:00
|
|
|
return userProfileIssues;
|
2023-09-12 17:21:13 +00:00
|
|
|
}, [displayFilters?.group_by, userProfileIssues]);
|
2023-07-28 08:09:42 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!userId || !filters) return;
|
|
|
|
|
|
|
|
if (
|
|
|
|
router.pathname.includes("assigned") &&
|
|
|
|
(!filters.assignees || !filters.assignees.includes(userId))
|
|
|
|
) {
|
|
|
|
setFilters({ assignees: [...(filters.assignees ?? []), userId] });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
router.pathname.includes("created") &&
|
|
|
|
(!filters.created_by || !filters.created_by.includes(userId))
|
|
|
|
) {
|
|
|
|
setFilters({ created_by: [...(filters.created_by ?? []), userId] });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (router.pathname.includes("subscribed") && filters.subscriber === null) {
|
2023-08-08 07:45:25 +00:00
|
|
|
setFilters({ subscriber: [userId] });
|
2023-07-28 08:09:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, [filters, router, setFilters, userId]);
|
|
|
|
|
|
|
|
const isEmpty =
|
|
|
|
Object.values(groupedIssues ?? {}).every((group) => group.length === 0) ||
|
|
|
|
Object.keys(groupedIssues ?? {}).length === 0;
|
|
|
|
|
|
|
|
return {
|
|
|
|
groupedIssues,
|
2023-09-12 16:57:15 +00:00
|
|
|
displayFilters,
|
|
|
|
setDisplayFilters,
|
2023-07-28 08:09:42 +00:00
|
|
|
filters,
|
|
|
|
setFilters,
|
2023-09-12 16:57:15 +00:00
|
|
|
displayProperties,
|
2023-07-28 08:09:42 +00:00
|
|
|
setProperties,
|
|
|
|
isEmpty,
|
|
|
|
mutateProfileIssues,
|
|
|
|
params,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useProfileIssues;
|