MCPcopy Create free account
hub / github.com/dailydotdev/apps / useVotePlugin

Function useVotePlugin

packages/shared/src/hooks/vote/useVotePlugin.ts:57–109  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55}
56
57export const useVotePlugin = (): UseVotePlugin => {
58 const client = useQueryClient();
59 const { user, showLogin } = useContext(AuthContext);
60 const { logEvent } = useLogContext();
61
62 const onMutate: NonNullable<UseVoteProps['onMutate']> = ({
63 id,
64 vote,
65 }: UseVoteMutationProps) => {
66 const previousVote = updateCachedPlugin(client, id, vote);
67
68 return () => {
69 if (typeof previousVote === 'undefined') {
70 return;
71 }
72
73 updateCachedPlugin(client, id, previousVote);
74 };
75 };
76
77 const { upvote, cancelVote } = useVote({
78 onMutate,
79 entity: UserVoteEntity.Plugin,
80 });
81
82 const toggleUpvote: UseVotePlugin['toggleUpvote'] = useCallback(
83 async (plugin) => {
84 if (!user) {
85 showLogin({ trigger: AuthTriggers.Upvote });
86 return;
87 }
88
89 const isUpvoted = plugin.userVote === UserVote.Up;
90
91 if (isUpvoted) {
92 await cancelVote({ id: plugin.id });
93 } else {
94 await upvote({ id: plugin.id });
95 }
96
97 logEvent({
98 event_name: isUpvoted
99 ? LogEvent.RemovePluginUpvote
100 : LogEvent.UpvotePlugin,
101 target_type: TargetType.Plugin,
102 target_id: plugin.id,
103 });
104 },
105 [cancelVote, logEvent, showLogin, upvote, user],
106 );
107
108 return { toggleUpvote };
109};

Callers 1

PluginUpvoteButtonFunction · 0.90

Calls 4

useLogContextFunction · 0.90
useVoteFunction · 0.90
upvoteFunction · 0.85
logEventFunction · 0.85

Tested by

no test coverage detected