-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Like.tsx
163 lines (145 loc) · 4.22 KB
/
Like.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import type { ApolloCache } from '@apollo/client';
import type { MirrorablePublication, ReactionRequest } from '@hey/lens';
import type { FC } from 'react';
import { HeartIcon } from '@heroicons/react/24/outline';
import { HeartIcon as HeartIconSolid } from '@heroicons/react/24/solid';
import { Errors } from '@hey/data/errors';
import { PUBLICATION } from '@hey/data/tracking';
import {
PublicationReactionType,
useAddReactionMutation,
useRemoveReactionMutation
} from '@hey/lens';
import nFormatter from '@hey/lib/nFormatter';
import { Tooltip } from '@hey/ui';
import cn from '@hey/ui/cn';
import errorToast from '@lib/errorToast';
import { Leafwatch } from '@lib/leafwatch';
import { useCounter, useToggle } from '@uidotdev/usehooks';
import { motion } from 'framer-motion';
import toast from 'react-hot-toast';
import { useProfileRestriction } from 'src/store/non-persisted/useProfileRestriction';
import { useProfileStore } from 'src/store/persisted/useProfileStore';
interface LikeProps {
publication: MirrorablePublication;
showCount: boolean;
}
const Like: FC<LikeProps> = ({ publication, showCount }) => {
const { currentProfile } = useProfileStore();
const { isSuspended } = useProfileRestriction();
const [hasReacted, toggleReact] = useToggle(
publication.operations.hasReacted
);
const [reactions, { decrement, increment }] = useCounter(
publication.stats.reactions
);
const updateCache = (cache: ApolloCache<any>) => {
cache.modify({
fields: {
operations: (existingValue) => {
return {
...existingValue,
// TODO: This is a hack to make the cache update
'hasReacted({"request":{"type":"UPVOTE"}})': !hasReacted
};
}
},
id: cache.identify(publication)
});
cache.modify({
fields: {
reactions: () => (hasReacted ? reactions - 1 : reactions + 1)
},
id: cache.identify(publication.stats)
});
};
const onError = (error: any) => {
errorToast(error);
};
const eventProperties = { publication_id: publication?.id };
const [addReaction] = useAddReactionMutation({
onCompleted: () =>
Leafwatch.track(
PUBLICATION.LIKE,
eventProperties,
publication.by.ownedBy.address
),
onError: (error) => {
toggleReact();
decrement();
onError(error);
},
update: updateCache
});
const [removeReaction] = useRemoveReactionMutation({
onCompleted: () =>
Leafwatch.track(
PUBLICATION.UNLIKE,
eventProperties,
publication.by.ownedBy.address
),
onError: (error) => {
toggleReact();
increment();
onError(error);
},
update: updateCache
});
const createLike = async () => {
if (!currentProfile) {
return toast.error(Errors.SignWallet);
}
if (isSuspended) {
return toast.error(Errors.Suspended);
}
// Variables
const request: ReactionRequest = {
for: publication.id,
reaction: PublicationReactionType.Upvote
};
toggleReact();
if (hasReacted) {
decrement();
return await removeReaction({ variables: { request } });
}
increment();
return await addReaction({ variables: { request } });
};
const iconClassName = showCount
? 'w-[17px] sm:w-[20px]'
: 'w-[15px] sm:w-[18px]';
return (
<div
className={cn(
hasReacted ? 'text-brand-500' : 'ld-text-gray-500',
'flex items-center space-x-1'
)}
>
<motion.button
aria-label="Like"
className={cn(
hasReacted ? 'hover:bg-brand-300/20' : 'hover:bg-gray-300/20',
'rounded-full p-1.5 outline-offset-2'
)}
onClick={createLike}
whileTap={{ scale: 0.9 }}
>
<Tooltip
content={hasReacted ? 'Unlike' : 'Like'}
placement="top"
withDelay
>
{hasReacted ? (
<HeartIconSolid className={iconClassName} />
) : (
<HeartIcon className={iconClassName} />
)}
</Tooltip>
</motion.button>
{reactions > 0 && !showCount ? (
<span className="text-[11px] sm:text-xs">{nFormatter(reactions)}</span>
) : null}
</div>
);
};
export default Like;