2023-07-18 06:37:55 +00:00
|
|
|
import { useCallback } from "react";
|
|
|
|
import useSWR from "swr";
|
|
|
|
// hooks
|
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
|
|
|
// services
|
2023-12-04 06:41:36 +00:00
|
|
|
import { NotificationService } from "services/notification.service";
|
|
|
|
|
|
|
|
const userNotificationServices = new NotificationService();
|
2023-07-18 06:37:55 +00:00
|
|
|
|
|
|
|
const useUserIssueNotificationSubscription = (
|
|
|
|
workspaceSlug?: string | string[] | null,
|
|
|
|
projectId?: string | string[] | null,
|
|
|
|
issueId?: string | string[] | null
|
|
|
|
) => {
|
|
|
|
const { user } = useUserAuth();
|
|
|
|
|
|
|
|
const { data, error, mutate } = useSWR(
|
2023-12-04 06:41:36 +00:00
|
|
|
workspaceSlug && projectId && issueId ? `SUBSCRIPTION_STATUE_${workspaceSlug}_${projectId}_${issueId}` : null,
|
2023-07-18 06:37:55 +00:00
|
|
|
workspaceSlug && projectId && issueId
|
|
|
|
? () =>
|
|
|
|
userNotificationServices.getIssueNotificationSubscriptionStatus(
|
|
|
|
workspaceSlug.toString(),
|
|
|
|
projectId.toString(),
|
|
|
|
issueId.toString()
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleUnsubscribe = useCallback(() => {
|
|
|
|
if (!workspaceSlug || !projectId || !issueId) return;
|
|
|
|
|
2023-07-20 11:54:17 +00:00
|
|
|
mutate(
|
|
|
|
{
|
|
|
|
subscribed: false,
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-07-18 06:37:55 +00:00
|
|
|
userNotificationServices
|
2023-12-04 06:41:36 +00:00
|
|
|
.unsubscribeFromIssueNotifications(workspaceSlug.toString(), projectId.toString(), issueId.toString())
|
2023-07-18 06:37:55 +00:00
|
|
|
.then(() => {
|
|
|
|
mutate({
|
|
|
|
subscribed: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, [workspaceSlug, projectId, issueId, mutate]);
|
|
|
|
|
|
|
|
const handleSubscribe = useCallback(() => {
|
|
|
|
if (!workspaceSlug || !projectId || !issueId || !user) return;
|
|
|
|
|
2023-07-20 11:54:17 +00:00
|
|
|
mutate(
|
|
|
|
{
|
|
|
|
subscribed: true,
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-07-18 06:37:55 +00:00
|
|
|
userNotificationServices
|
2023-12-04 06:41:36 +00:00
|
|
|
.subscribeToIssueNotifications(workspaceSlug.toString(), projectId.toString(), issueId.toString())
|
2023-07-18 06:37:55 +00:00
|
|
|
.then(() => {
|
|
|
|
mutate({
|
|
|
|
subscribed: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, [workspaceSlug, projectId, issueId, mutate, user]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
loading: !data && !error,
|
|
|
|
subscribed: data?.subscribed,
|
|
|
|
handleSubscribe,
|
|
|
|
handleUnsubscribe,
|
|
|
|
} as const;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useUserIssueNotificationSubscription;
|