({
post,
}: UsePostFeedbackProps = {})
| 25 | } |
| 26 | |
| 27 | export const usePostFeedback = ({ |
| 28 | post, |
| 29 | }: UsePostFeedbackProps = {}): UsePostFeedback => { |
| 30 | const client = useQueryClient(); |
| 31 | const { queryKey: feedQueryKey, items } = useContext(ActiveFeedContext); |
| 32 | const { logEvent } = useLogContext(); |
| 33 | |
| 34 | const isMyFeed = useMemo(() => { |
| 35 | return feedQueryKey?.some((item) => item === SharedFeedPage.MyFeed); |
| 36 | }, [feedQueryKey]); |
| 37 | |
| 38 | const dismissFeedbackMutation = useMutation({ |
| 39 | mutationFn: () => dismissPostFeedback(post.id), |
| 40 | |
| 41 | onMutate: () => { |
| 42 | if (!post) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | logEvent({ |
| 47 | event_name: LogEvent.ClickDismissFeedback, |
| 48 | }); |
| 49 | |
| 50 | const mutationHandler = (postItem: Post) => { |
| 51 | return { |
| 52 | userState: { |
| 53 | ...postItem.userState, |
| 54 | flags: { |
| 55 | ...postItem.userState?.flags, |
| 56 | feedbackDismiss: true, |
| 57 | }, |
| 58 | }, |
| 59 | }; |
| 60 | }; |
| 61 | |
| 62 | if (feedQueryKey) { |
| 63 | const postIndex = items.findIndex( |
| 64 | (item) => |
| 65 | (item.type === 'post' && item.post.id === post.id) || |
| 66 | (item.type === 'ad' && item.ad.data?.post?.id === post.id), |
| 67 | ); |
| 68 | |
| 69 | if (postIndex === -1) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | const item = items[postIndex]; |
| 74 | if (item.type === 'post') { |
| 75 | const updateFeedPost = updateCachedPagePost(feedQueryKey, client); |
| 76 | const updateFeedPostCache = optimisticPostUpdateInFeed( |
| 77 | items, |
| 78 | updateFeedPost, |
| 79 | mutationHandler, |
| 80 | ); |
| 81 | updateFeedPostCache({ index: postIndex }); |
| 82 | } else if (isBoostedPostAd(item)) { |
| 83 | updateFeedAndAdsCache( |
| 84 | post.id, |
no test coverage detected