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

Function useBookmarkPost

packages/shared/src/hooks/useBookmarkPost.ts:88–256  ·  view source on GitHub ↗
({
  onMutate,
  mutationKey,
}: UseBookmarkPostProps = {})

Source from the content-addressed store, hash-verified

86};
87
88const useBookmarkPost = ({
89 onMutate,
90 mutationKey,
91}: UseBookmarkPostProps = {}): UseBookmarkPost => {
92 const { requestMethod } = useRequestProtocol();
93 const client = useQueryClient();
94 const { displayToast } = useToastNotification();
95 const { user, showLogin } = useContext(AuthContext);
96 const { logEvent } = useLogContext();
97 const { completeAction } = useActions();
98 const { openModal } = useLazyModal();
99 const postLogEvent = usePostLogEvent();
100 const { logOpts } = useActiveFeedContext();
101
102 const defaultOnMutate: NonNullable<UseBookmarkPostProps['onMutate']> = ({
103 id,
104 }) => {
105 if (!id) {
106 return undefined;
107 }
108
109 const previousPost = client.getQueryData<PostData>(
110 getPostByIdKey(id),
111 )?.post;
112
113 updatePostCache(client, id, (post) => ({
114 bookmarked: !post.bookmarked,
115 analytics: post.analytics
116 ? {
117 ...post.analytics,
118 bookmarks: getOptimisticBookmarkCount(
119 post.analytics.bookmarks,
120 post.bookmarked,
121 ),
122 }
123 : post.analytics,
124 }));
125
126 return () => {
127 if (!previousPost) {
128 return;
129 }
130
131 updatePostCache(client, id, () => ({
132 bookmarked: previousPost.bookmarked,
133 bookmark: previousPost.bookmark,
134 analytics: previousPost.analytics,
135 }));
136 };
137 };
138
139 const { mutateAsync: bookmarkPost } = useMutation({
140 mutationKey: mutationKey
141 ? [...bookmarkMutationKey, ...mutationKey]
142 : bookmarkMutationKey,
143 mutationFn: ({ mutation, payload }: UseBookmarkPostMutationProps) =>
144 requestMethod(mutation, {
145 ...payload,

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
CoverTopicsFunction · 0.90
CoverGridFunction · 0.90

Calls 12

useRequestProtocolFunction · 0.90
useToastNotificationFunction · 0.90
useLogContextFunction · 0.90
useActionsFunction · 0.90
useLazyModalFunction · 0.90
usePostLogEventFunction · 0.90
useActiveFeedContextFunction · 0.90
rollbackFunction · 0.85
logEventFunction · 0.85
postLogEventFunction · 0.85
displayToastFunction · 0.85

Tested by

no test coverage detected