-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ASC-25689 - add condition to show moderator badge (#652)
* fix: add condition to show moderator badge * fix: hooks moderator
- Loading branch information
1 parent
8d4c4a8
commit 90ba4f7
Showing
5 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { CommunityPostSettings } from '@amityco/ts-sdk'; | ||
import { useMemo } from 'react'; | ||
import usePostsCollection from '~/v4/social/hooks/collections/usePostsCollection'; | ||
import { useSDK } from '~/v4/core/hooks/useSDK'; | ||
import useCommunityModeratorsCollection from '~/v4/social/hooks/collections/useCommunityModeratorsCollection'; | ||
import { Permissions } from '~/v4/social/constants/permissions'; | ||
|
||
const useCommunityPostPermission = ({ | ||
post, | ||
childrenPosts = [], | ||
community, | ||
userId, | ||
}: { | ||
post?: Amity.Post | null; | ||
childrenPosts?: Amity.Post[]; | ||
community?: Amity.Community | null; | ||
userId?: string; | ||
}) => { | ||
const { moderators } = useCommunityModeratorsCollection({ communityId: community?.communityId }); | ||
const { client } = useSDK(); | ||
|
||
const { posts: reviewingPosts } = usePostsCollection({ | ||
targetType: 'community', | ||
targetId: community?.communityId, | ||
feedType: 'reviewing', | ||
}); | ||
|
||
const isEditable = useMemo(() => { | ||
if ( | ||
childrenPosts.find( | ||
(childPost) => childPost.dataType === 'liveStream' || childPost.dataType === 'poll', | ||
) | ||
) { | ||
return false; | ||
} | ||
return true; | ||
}, [childrenPosts]); | ||
|
||
const moderator = moderators.find((moderator) => moderator.userId === userId); | ||
const isMyPost = post?.postedUserId === userId; | ||
const isPostUnderReview = useMemo(() => { | ||
if (community?.postSetting != CommunityPostSettings.ANYONE_CAN_POST) { | ||
return reviewingPosts.find((reviewingPost) => reviewingPost.postId === post?.postId) != null; | ||
} | ||
return false; | ||
}, [community, reviewingPosts]); | ||
|
||
const isModerator = moderator != null; | ||
|
||
const permissions: { | ||
canEdit: boolean; | ||
canReport: boolean; | ||
canDelete: boolean; | ||
canReview: boolean; | ||
} = { | ||
canEdit: false, | ||
canReport: false, | ||
canDelete: false, | ||
canReview: false, | ||
}; | ||
|
||
if (isMyPost) { | ||
if (!isPostUnderReview && isEditable) { | ||
permissions.canEdit = true; | ||
} | ||
permissions.canDelete = true; | ||
} else { | ||
if (community != null) { | ||
const canEdit = | ||
client | ||
?.hasPermission(Permissions.EditCommunityFeedPostPermission) | ||
.community(community.communityId) ?? false; | ||
|
||
permissions.canEdit = canEdit && isEditable; | ||
|
||
const canDelete = | ||
client | ||
?.hasPermission(Permissions.DeleteCommunityFeedPostPermission) | ||
.community(community.communityId) ?? false; | ||
|
||
permissions.canDelete = canDelete; | ||
} else { | ||
const canDelete = | ||
client?.hasPermission(Permissions.EditUserFeedPostPermission).currentUser() ?? false; | ||
|
||
permissions.canDelete = canDelete; | ||
} | ||
|
||
if (!isPostUnderReview) { | ||
permissions.canReport = true; | ||
} | ||
} | ||
|
||
return { | ||
isPostUnderReview, | ||
isModerator, | ||
...permissions, | ||
}; | ||
}; | ||
|
||
export default useCommunityPostPermission; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters