2024-01-10 14:39:45 +00:00
|
|
|
import { FC, useState } from "react";
|
2024-02-12 11:31:24 +00:00
|
|
|
import { Bell, BellOff } from "lucide-react";
|
2024-01-10 14:39:45 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
// UI
|
|
|
|
import { Button } from "@plane/ui";
|
|
|
|
// hooks
|
|
|
|
import { useIssueDetail } from "hooks/store";
|
2024-01-11 12:56:58 +00:00
|
|
|
import useToast from "hooks/use-toast";
|
2024-01-10 14:39:45 +00:00
|
|
|
|
|
|
|
export type TIssueSubscription = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueSubscription: FC<TIssueSubscription> = observer((props) => {
|
2024-01-23 12:19:22 +00:00
|
|
|
const { workspaceSlug, projectId, issueId } = props;
|
2024-01-10 14:39:45 +00:00
|
|
|
// hooks
|
|
|
|
const {
|
|
|
|
subscription: { getSubscriptionByIssueId },
|
|
|
|
createSubscription,
|
|
|
|
removeSubscription,
|
|
|
|
} = useIssueDetail();
|
2024-01-11 12:56:58 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2024-01-10 14:39:45 +00:00
|
|
|
// state
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
|
|
|
const subscription = getSubscriptionByIssueId(issueId);
|
|
|
|
|
2024-01-11 12:56:58 +00:00
|
|
|
const handleSubscription = async () => {
|
2024-01-10 14:39:45 +00:00
|
|
|
setLoading(true);
|
2024-01-11 12:56:58 +00:00
|
|
|
try {
|
|
|
|
if (subscription?.subscribed) await removeSubscription(workspaceSlug, projectId, issueId);
|
|
|
|
else await createSubscription(workspaceSlug, projectId, issueId);
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: `Issue ${subscription?.subscribed ? `unsubscribed` : `subscribed`} successfully.!`,
|
|
|
|
message: `Issue ${subscription?.subscribed ? `unsubscribed` : `subscribed`} successfully.!`,
|
|
|
|
});
|
|
|
|
setLoading(false);
|
|
|
|
} catch (error) {
|
|
|
|
setLoading(false);
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error",
|
|
|
|
message: "Something went wrong. Please try again later.",
|
|
|
|
});
|
|
|
|
}
|
2024-01-10 14:39:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
size="sm"
|
2024-02-12 11:31:24 +00:00
|
|
|
prependIcon={subscription?.subscribed ? <BellOff /> : <Bell className="h-3 w-3" />}
|
2024-01-10 14:39:45 +00:00
|
|
|
variant="outline-primary"
|
|
|
|
className="hover:!bg-custom-primary-100/20"
|
|
|
|
onClick={handleSubscription}
|
|
|
|
>
|
2024-02-12 11:31:24 +00:00
|
|
|
{loading ? (
|
|
|
|
<span>
|
|
|
|
<span className="hidden sm:block">Loading</span>...
|
|
|
|
</span>
|
|
|
|
) : subscription?.subscribed ? (
|
|
|
|
<div className="hidden sm:block">Unsubscribe</div>
|
|
|
|
) : (
|
|
|
|
<div className="hidden sm:block">Subscribe</div>
|
|
|
|
)}
|
2024-01-10 14:39:45 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|