/
P2PRecommendation.tsx
130 lines (115 loc) 路 3.31 KB
/
P2PRecommendation.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
import type { ApolloCache } from '@apollo/client';
import type { PeerToPeerRecommendRequest, Profile } from '@hey/lens';
import type { FC } from 'react';
import {
ArrowDownCircleIcon,
ArrowUpCircleIcon
} from '@heroicons/react/24/outline';
import { Errors } from '@hey/data/errors';
import { GARDENER } from '@hey/data/tracking';
import {
usePeerToPeerRecommendMutation,
usePeerToPeerUnrecommendMutation
} from '@hey/lens';
import stopEventPropagation from '@hey/lib/stopEventPropagation';
import { Button } from '@hey/ui';
import errorToast from '@lib/errorToast';
import { Leafwatch } from '@lib/leafwatch';
import { useState } from 'react';
import toast from 'react-hot-toast';
import { useProfileRestriction } from 'src/store/non-persisted/useProfileRestriction';
import { useProfileStore } from 'src/store/persisted/useProfileStore';
interface P2PRecommendationProps {
profile: Profile;
recommendTitle?: string;
unrecommendTitle?: string;
}
const P2PRecommendation: FC<P2PRecommendationProps> = ({
profile,
recommendTitle = 'Recommend',
unrecommendTitle = 'Unrecommend'
}) => {
const { currentProfile } = useProfileStore();
const { isSuspended } = useProfileRestriction();
const [hasRecommended, setHasRecommended] = useState(
profile.peerToPeerRecommendedByMe
);
const updateCache = (cache: ApolloCache<any>) => {
cache.modify({
fields: {
operations: (existingValue) => {
return {
...existingValue,
peerToPeerRecommendedByMe: hasRecommended
};
}
},
id: cache.identify(profile)
});
};
const onError = (error: any) => {
errorToast(error);
};
const [p2pRecommend] = usePeerToPeerRecommendMutation({
onCompleted: () => {
Leafwatch.track(GARDENER.PROFILE.P2P_RECOMMEND, { profile: profile.id });
toast.success('Profile recommended');
},
onError: (error) => {
setHasRecommended(!hasRecommended);
onError(error);
},
update: updateCache
});
const [p2pUnRecommend] = usePeerToPeerUnrecommendMutation({
onCompleted: () => {
Leafwatch.track(GARDENER.PROFILE.P2P_UNRECOMMEND, {
profile: profile.id
});
toast.success('Profile unrecommended');
},
onError: (error) => {
setHasRecommended(!hasRecommended);
onError(error);
},
update: updateCache
});
const updateRecommendation = async () => {
if (!currentProfile) {
return toast.error(Errors.SignWallet);
}
if (isSuspended) {
return toast.error(Errors.Suspended);
}
// Variables
const request: PeerToPeerRecommendRequest = {
profileId: profile.id
};
if (hasRecommended) {
setHasRecommended(false);
return await p2pUnRecommend({ variables: { request } });
}
setHasRecommended(true);
return await p2pRecommend({ variables: { request } });
};
return (
<Button
icon={
hasRecommended ? (
<ArrowDownCircleIcon className="size-4" />
) : (
<ArrowUpCircleIcon className="size-4" />
)
}
onClick={(event) => {
stopEventPropagation(event);
updateRecommendation();
}}
outline={!hasRecommended}
size="sm"
>
{hasRecommended ? unrecommendTitle : recommendTitle}
</Button>
);
};
export default P2PRecommendation;