({
post,
postIndex,
onPostDeleted,
onPinSuccessful,
onSwapPostSuccessful,
origin,
}: UsePostMenuActionsProps)
| 53 | }; |
| 54 | |
| 55 | export const usePostMenuActions = ({ |
| 56 | post, |
| 57 | postIndex, |
| 58 | onPostDeleted, |
| 59 | onPinSuccessful, |
| 60 | onSwapPostSuccessful, |
| 61 | origin, |
| 62 | }: UsePostMenuActionsProps): UsePostMenuActions => { |
| 63 | const { user } = useAuthContext(); |
| 64 | const { showPrompt } = usePrompt(); |
| 65 | const { mutateAsync: onDeletePost } = useMutation({ |
| 66 | mutationFn: ({ id }: DeletePostProps) => deletePost(id), |
| 67 | onSuccess: (_, vars) => onPostDeleted(vars), |
| 68 | }); |
| 69 | const deletePostPrompt = useCallback(async () => { |
| 70 | const param = { id: post.id, index: postIndex, post }; |
| 71 | |
| 72 | if (await showPrompt(deletePromptOptions)) { |
| 73 | await onDeletePost(param); |
| 74 | } |
| 75 | }, [post, postIndex, onDeletePost, showPrompt]); |
| 76 | |
| 77 | const isSharedPostAuthor = |
| 78 | post?.source.type === SourceType.Squad && post?.author?.id === user?.id; |
| 79 | const isModerator = user?.roles?.includes(Roles.Moderator); |
| 80 | const canDelete = |
| 81 | isModerator || |
| 82 | isSharedPostAuthor || |
| 83 | post?.source.currentMember?.permissions?.includes( |
| 84 | SourcePermissions.PostDelete, |
| 85 | ); |
| 86 | |
| 87 | const canPin = post?.source.currentMember?.permissions?.includes( |
| 88 | SourcePermissions.PostPin, |
| 89 | ); |
| 90 | |
| 91 | const canSwap = canPin && post?.pinnedAt; |
| 92 | |
| 93 | const { mutateAsync: onPinPost } = useMutation({ |
| 94 | mutationFn: () => updatePinnedPost({ id: post.id, pinned: !post.pinnedAt }), |
| 95 | onSuccess: () => { |
| 96 | onPinSuccessful?.(); |
| 97 | }, |
| 98 | }); |
| 99 | |
| 100 | const { mutateAsync: onSwapPinnedPost } = useMutation({ |
| 101 | mutationFn: ({ swapWithId }: { swapWithId: Post['id'] }) => |
| 102 | swapPinnedPosts({ id: post.id, swapWithId }), |
| 103 | |
| 104 | onSuccess: () => { |
| 105 | onSwapPostSuccessful?.(); |
| 106 | }, |
| 107 | }); |
| 108 | |
| 109 | const { onClose, onShowPanel } = useBlockPostPanel(post); |
| 110 | const { toggleDownvote } = useVotePost(); |
| 111 | |
| 112 | return { |
no test coverage detected