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

Function useVotePost

packages/shared/src/hooks/vote/useVotePost.ts:40–192  ·  view source on GitHub ↗
({
  onMutate,
  variables,
}: UseVotePostProps = {})

Source from the content-addressed store, hash-verified

38};
39
40const useVotePost = ({
41 onMutate,
42 variables,
43}: UseVotePostProps = {}): UseVotePost => {
44 const client = useQueryClient();
45 const { user, showLogin } = useContext(AuthContext);
46 const { logEvent } = useLogContext();
47 const postLogEvent = usePostLogEvent();
48 const { logOpts } = useActiveFeedContext();
49 const defaultOnMutate: NonNullable<UseVoteProps['onMutate']> = ({
50 id,
51 vote,
52 }: UseVoteMutationProps) => {
53 const mutationHandler = voteMutationHandlers[vote];
54
55 if (!mutationHandler) {
56 return undefined;
57 }
58
59 const previousVote = client.getQueryData<PostData>(getPostByIdKey(id))?.post
60 ?.userState?.vote;
61 updateSinglePostCache(client, id, mutationHandler);
62
63 return () => {
64 if (typeof previousVote === 'undefined') {
65 return;
66 }
67
68 const rollbackMutationHandler = voteMutationHandlers[previousVote];
69
70 if (!rollbackMutationHandler) {
71 return;
72 }
73
74 updateSinglePostCache(client, id, rollbackMutationHandler);
75 };
76 };
77
78 const {
79 upvote: upvotePost,
80 downvote: downvotePost,
81 cancelVote: cancelPostVote,
82 } = useVote({
83 onMutate: onMutate || defaultOnMutate,
84 entity: UserVoteEntity.Post,
85 variables,
86 });
87
88 const toggleUpvote: UseVotePost['toggleUpvote'] = useCallback(
89 async ({ payload: post, origin, opts }) => {
90 if (!post) {
91 return;
92 }
93
94 if (!user) {
95 showLogin({ trigger: AuthTriggers.Upvote });
96
97 return;

Callers 15

ActionBarFunction · 0.90
useFeedHeroPostActionsFunction · 0.90
PostActionsV1Function · 0.90
PostActionsFunction · 0.90
PostCapsuleFunction · 0.90
ReaderRailActionBarFunction · 0.90
ReaderRailActionBarV1Function · 0.90
ReaderFloatingActionBarFunction · 0.90
usePatchyAssistantFunction · 0.90
useChangelogFunction · 0.90
usePostMenuActionsFunction · 0.90

Calls 9

useLogContextFunction · 0.90
usePostLogEventFunction · 0.90
useActiveFeedContextFunction · 0.90
useVoteFunction · 0.90
playHapticFunction · 0.90
emitPatchyEventFunction · 0.90
logEventFunction · 0.85
postLogEventFunction · 0.85

Tested by

no test coverage detected