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

Function useVotePost

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

Source from the content-addressed store, hash-verified

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

Callers 14

PostActionsV1Function · 0.90
PostActionsFunction · 0.90
MobilePostFloatingBarFunction · 0.90
MobilePostFloatingBarV1Function · 0.90
ReaderRailActionBarFunction · 0.90
ReaderRailActionBarV1Function · 0.90
ReaderFloatingActionBarFunction · 0.90
FocusCardActionBarFunction · 0.90
useChangelogFunction · 0.90
usePostMenuActionsFunction · 0.90
useReadHistoryVotePostFunction · 0.90

Calls 7

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

Tested by

no test coverage detected