plane/web/hooks/use-issue-notification-subscription.tsx
sriram veeraghanta 1e152c666c
New Directory Setup (#2065)
* chore: moved app & space from apps to root

* chore: modified workspace configuration

* chore: modified dockerfiles for space and web

* chore: modified icons for space

* feat: updated files for new svg icons supported by next-images

* chore: added /spaces base path for next

* chore: added compose config for space

* chore: updated husky configuration

* chore: updated workflows for new configuration

* chore: changed app name to web

* fix: resolved build errors with web

* chore: reset file tracing root for both projects

* chore: added nginx config for deploy

* fix: eslint and tsconfig settings for space app

* husky setup fixes based on new dir

* eslint fixes

* prettier formatting

---------

Co-authored-by: Henit Chobisa <chobisa.henit@gmail.com>
2023-09-03 18:50:30 +05:30

86 lines
2.0 KiB
TypeScript

import { useCallback } from "react";
import useSWR from "swr";
// hooks
import useUserAuth from "hooks/use-user-auth";
// services
import userNotificationServices from "services/notifications.service";
const useUserIssueNotificationSubscription = (
workspaceSlug?: string | string[] | null,
projectId?: string | string[] | null,
issueId?: string | string[] | null
) => {
const { user } = useUserAuth();
const { data, error, mutate } = useSWR(
workspaceSlug && projectId && issueId
? `SUBSCRIPTION_STATUE_${workspaceSlug}_${projectId}_${issueId}`
: null,
workspaceSlug && projectId && issueId
? () =>
userNotificationServices.getIssueNotificationSubscriptionStatus(
workspaceSlug.toString(),
projectId.toString(),
issueId.toString()
)
: null
);
const handleUnsubscribe = useCallback(() => {
if (!workspaceSlug || !projectId || !issueId) return;
mutate(
{
subscribed: false,
},
false
);
userNotificationServices
.unsubscribeFromIssueNotifications(
workspaceSlug.toString(),
projectId.toString(),
issueId.toString()
)
.then(() => {
mutate({
subscribed: false,
});
});
}, [workspaceSlug, projectId, issueId, mutate]);
const handleSubscribe = useCallback(() => {
if (!workspaceSlug || !projectId || !issueId || !user) return;
mutate(
{
subscribed: true,
},
false
);
userNotificationServices
.subscribeToIssueNotifications(
workspaceSlug.toString(),
projectId.toString(),
issueId.toString()
)
.then(() => {
mutate({
subscribed: true,
});
});
}, [workspaceSlug, projectId, issueId, mutate, user]);
return {
loading: !data && !error,
subscribed: data?.subscribed,
handleSubscribe,
handleUnsubscribe,
} as const;
};
export default useUserIssueNotificationSubscription;