2024-06-10 09:43:10 +00:00
|
|
|
"use client";
|
|
|
|
|
2024-02-22 15:28:34 +00:00
|
|
|
import { FC, useState } from "react";
|
2024-03-06 13:09:14 +00:00
|
|
|
import isNil from "lodash/isNil";
|
2024-05-08 17:31:20 +00:00
|
|
|
import { observer } from "mobx-react";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { Bell, BellOff } from "lucide-react";
|
2024-01-10 14:39:45 +00:00
|
|
|
// UI
|
2024-03-06 08:48:41 +00:00
|
|
|
import { Button, Loader, TOAST_TYPE, setToast } from "@plane/ui";
|
2024-01-10 14:39:45 +00:00
|
|
|
// hooks
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useIssueDetail } from "@/hooks/store";
|
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();
|
|
|
|
// state
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
2024-02-22 15:28:34 +00:00
|
|
|
const isSubscribed = getSubscriptionByIssueId(issueId);
|
2024-01-10 14:39:45 +00:00
|
|
|
|
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 {
|
2024-02-22 15:28:34 +00:00
|
|
|
if (isSubscribed) await removeSubscription(workspaceSlug, projectId, issueId);
|
2024-01-11 12:56:58 +00:00
|
|
|
else await createSubscription(workspaceSlug, projectId, issueId);
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2024-05-07 09:33:24 +00:00
|
|
|
title: "Success!",
|
2024-02-22 15:28:34 +00:00
|
|
|
message: `Issue ${isSubscribed ? `unsubscribed` : `subscribed`} successfully.!`,
|
2024-01-11 12:56:58 +00:00
|
|
|
});
|
|
|
|
setLoading(false);
|
|
|
|
} catch (error) {
|
|
|
|
setLoading(false);
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-05-07 09:33:24 +00:00
|
|
|
title: "Error!",
|
2024-01-11 12:56:58 +00:00
|
|
|
message: "Something went wrong. Please try again later.",
|
|
|
|
});
|
|
|
|
}
|
2024-01-10 14:39:45 +00:00
|
|
|
};
|
|
|
|
|
2024-02-22 15:28:34 +00:00
|
|
|
if (isNil(isSubscribed))
|
2024-02-21 13:53:54 +00:00
|
|
|
return (
|
|
|
|
<Loader>
|
2024-02-22 15:28:34 +00:00
|
|
|
<Loader.Item width="106px" height="28px" />
|
2024-02-21 13:53:54 +00:00
|
|
|
</Loader>
|
|
|
|
);
|
|
|
|
|
2024-01-10 14:39:45 +00:00
|
|
|
return (
|
2024-02-22 15:28:34 +00:00
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
size="sm"
|
|
|
|
prependIcon={isSubscribed ? <BellOff /> : <Bell className="h-3 w-3" />}
|
|
|
|
variant="outline-primary"
|
|
|
|
className="hover:!bg-custom-primary-100/20"
|
|
|
|
onClick={handleSubscription}
|
|
|
|
>
|
|
|
|
{loading ? (
|
|
|
|
<span>
|
2024-02-26 14:14:01 +00:00
|
|
|
<span className="hidden sm:block">Loading...</span>
|
2024-02-22 15:28:34 +00:00
|
|
|
</span>
|
|
|
|
) : isSubscribed ? (
|
|
|
|
<div className="hidden sm:block">Unsubscribe</div>
|
|
|
|
) : (
|
|
|
|
<div className="hidden sm:block">Subscribe</div>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2024-01-10 14:39:45 +00:00
|
|
|
);
|
|
|
|
});
|