-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathNotInterested.tsx
110 lines (99 loc) · 2.95 KB
/
NotInterested.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { MenuItem } from "@headlessui/react";
import errorToast from "@helpers/errorToast";
import { Leafwatch } from "@helpers/leafwatch";
import { EyeIcon, EyeSlashIcon } from "@heroicons/react/24/outline";
import { PUBLICATION } from "@hey/data/tracking";
import stopEventPropagation from "@hey/helpers/stopEventPropagation";
import type {
MirrorablePublication,
PublicationNotInterestedRequest
} from "@hey/lens";
import {
useAddPublicationNotInterestedMutation,
useUndoPublicationNotInterestedMutation
} from "@hey/lens";
import type { ApolloCache } from "@hey/lens/apollo";
import cn from "@hey/ui/cn";
import type { FC } from "react";
import { toast } from "react-hot-toast";
interface NotInterestedProps {
publication: MirrorablePublication;
}
const NotInterested: FC<NotInterestedProps> = ({ publication }) => {
const notInterested = publication.operations.isNotInterested;
const request: PublicationNotInterestedRequest = {
on: publication.id
};
const updateCache = (cache: ApolloCache<any>, notInterested: boolean) => {
cache.modify({
fields: {
operations: (existingValue) => {
return { ...existingValue, isNotInterested: notInterested };
}
},
id: cache.identify(publication)
});
};
const onError = (error: any) => {
errorToast(error);
};
const [addPublicationNotInterested] = useAddPublicationNotInterestedMutation({
onCompleted: () => {
toast.success("Marked as not Interested");
Leafwatch.track(PUBLICATION.NOT_INTERESTED, {
publication_id: publication.id
});
},
onError,
update: (cache) => updateCache(cache, true),
variables: { request }
});
const [undoPublicationNotInterested] =
useUndoPublicationNotInterestedMutation({
onCompleted: () => {
toast.success("Undo Not interested");
Leafwatch.track(PUBLICATION.UNDO_NOT_INTERESTED, {
publication_id: publication.id
});
},
onError,
update: (cache) => updateCache(cache, false),
variables: { request }
});
const togglePublicationProfileNotInterested = async () => {
if (notInterested) {
return await undoPublicationNotInterested();
}
return await addPublicationNotInterested();
};
return (
<MenuItem
as="div"
className={({ focus }) =>
cn(
{ "dropdown-active": focus },
"m-2 block cursor-pointer rounded-lg px-2 py-1.5 text-sm"
)
}
onClick={(event) => {
stopEventPropagation(event);
togglePublicationProfileNotInterested();
}}
>
<div className="flex items-center space-x-2">
{notInterested ? (
<>
<EyeIcon className="size-4" />
<div>Undo Not interested</div>
</>
) : (
<>
<EyeSlashIcon className="size-4" />
<div>Not interested</div>
</>
)}
</div>
</MenuItem>
);
};
export default NotInterested;