Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into release/v4.0.0-b…
Browse files Browse the repository at this point in the history
…eta.17
  • Loading branch information
ptchayap committed Oct 25, 2024
2 parents e11f4b0 + b387986 commit a22c067
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 47 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
"tsc": "tsc"
},
"peerDependencies": {
"@amityco/ts-sdk": "^6.30.0",
"@amityco/ts-sdk": "^6.32.0",
"react": ">=17.0.2",
"react-dom": ">=17.0.2"
},
"devDependencies": {
"@amityco/ts-sdk": "^6.30.4",
"@amityco/ts-sdk": "6.32.0",
"@eslint/js": "^9.4.0",
"@storybook/addon-a11y": "^7.6.7",
"@storybook/addon-actions": "^7.6.7",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/social/components/CommunityInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import { useConfirmContext } from '~/core/providers/ConfirmProvider';

interface CommunityInfoProps {
communityId: string;
stories: (Amity.Story | undefined)[];
}

const CommunityInfo = ({ communityId, stories }: CommunityInfoProps) => {
const CommunityInfo = ({ communityId }: CommunityInfoProps) => {
const {
community,
communityCategories,
Expand Down
1 change: 1 addition & 0 deletions src/social/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const Permissions = Object.freeze({
EditCommunityCategoryPermission: 'EDIT_COMMUNITY_CATEGORY',
DeleteCommunityCategoryPermission: 'DELETE_COMMUNITY_CATEGORY',
ManageStoryPermission: 'MANAGE_COMMUNITY_STORY',
CreatePivillegedPostPermission: 'CREATE_PRIVILEGED_POST',
});

export const MAXIMUM_MENTIONEES = 30;
Expand Down
11 changes: 10 additions & 1 deletion src/social/hooks/useCommunityPermission.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import useSDK from '~/core/hooks/useSDK';
import { Permissions } from '../constants';
import useCommunityModeratorsCollection from './collections/useCommunityModeratorsCollection';

const useCommunityPermission = ({ community }: { community?: Amity.Community | null }) => {
const { currentUserId, userRoles } = useSDK();
const { currentUserId, userRoles, client } = useSDK();
const { moderators } = useCommunityModeratorsCollection(community?.communityId);

const moderator = moderators.find((moderator) => moderator.userId === currentUserId);
Expand All @@ -11,10 +12,18 @@ const useCommunityPermission = ({ community }: { community?: Amity.Community | n

const isModerator = moderator != null;

const canCreatePost =
(community?.communityId &&
client
?.hasPermission(Permissions.CreatePivillegedPostPermission)
.community(community?.communityId)) ||
community?.postSetting !== 'ONLY_ADMIN_CAN_POST';

return {
isModerator,
canEdit: isGlobalAdmin || isModerator,
canReview: isGlobalAdmin || isModerator,
canCreatePost: !!canCreatePost,
};
};

Expand Down
22 changes: 5 additions & 17 deletions src/social/pages/CommunityFeed/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useEffect, useMemo, useState } from 'react';

import { SubscriptionLevels } from '@amityco/ts-sdk';
import { FormattedMessage } from 'react-intl';
import CommunityCreatedModal from '~/social/components/CommunityCreatedModal';

Expand All @@ -11,12 +10,12 @@ import MediaGallery from '~/social/components/MediaGallery';
import CommunityInfo from '~/social/components/CommunityInfo';
import CommunityMembers from '~/social/components/CommunityMembers';
import FeedHeaderTabs from '~/social/components/FeedHeaderTabs';

import { CommunityFeedTabs } from './constants';
import { getTabs } from './utils';
import { DeclineBanner, Wrapper } from './styles';
import useCommunityPermission from '~/social/hooks/useCommunityPermission';
import useCommunitySubscription from '~/social/hooks/useCommunitySubscription';

import useCommunityPermission from '~/social/hooks/useCommunityPermission';
import usePostsCollection from '~/social/hooks/collections/usePostsCollection';

import {
Expand All @@ -27,7 +26,6 @@ import {
StyledCommunitySideMenu,
} from '../NewsFeed/styles';

import { useNavigation } from '~/social/providers/NavigationProvider';
import { useGetActiveStoriesByTarget } from '~/v4/social/hooks/useGetActiveStories';

interface CommunityFeedProps {
Expand All @@ -38,19 +36,9 @@ interface CommunityFeedProps {
}

const CommunityFeed = ({ communityId, isNewCommunity, isOpen, toggleOpen }: CommunityFeedProps) => {
const { goToDraftStoryPage } = useNavigation();
const { stories } = useGetActiveStoriesByTarget({
targetId: communityId,
targetType: 'community',
options: {
orderBy: 'asc',
sortBy: 'createdAt',
},
});

const community = useCommunity(communityId);

const { canReview } = useCommunityPermission({ community });
const { canReview, canCreatePost } = useCommunityPermission({ community });

const { posts } = usePostsCollection({
targetId: communityId,
Expand Down Expand Up @@ -87,7 +75,7 @@ const CommunityFeed = ({ communityId, isNewCommunity, isOpen, toggleOpen }: Comm
<FormattedMessage id="sidebar.community" />
</HeadTitle>
</MobileContainer>
<CommunityInfo communityId={communityId} stories={stories} />
<CommunityInfo communityId={communityId} />
<FeedHeaderTabs
data-qa-anchor="community-feed-header"
tabs={tabs}
Expand All @@ -100,7 +88,7 @@ const CommunityFeed = ({ communityId, isNewCommunity, isOpen, toggleOpen }: Comm
targetType={'community'}
targetId={communityId}
readonly={!isJoined}
showPostCreator={isJoined}
showPostCreator={isJoined && canCreatePost}
feedType={'published'}
/>
)}
Expand Down
1 change: 1 addition & 0 deletions src/v4/social/constants/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export enum Permissions {
EditCommunityCategoryPermission = 'EDIT_COMMUNITY_CATEGORY',
DeleteCommunityCategoryPermission = 'DELETE_COMMUNITY_CATEGORY',
ManageStoryPermission = 'MANAGE_COMMUNITY_STORY',
CreatePivillegedPostPermission = 'CREATE_PRIVILEGED_POST',
}
53 changes: 29 additions & 24 deletions src/v4/social/pages/SelectPostTargetPage/SelectPostTargetPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import { usePageBehavior } from '~/v4/core/providers/PageBehaviorProvider';
import useSDK from '~/v4/core/hooks/useSDK';
import { Mode } from '~/v4/social/pages/PostComposerPage/';
import { Button } from '~/v4/core/natives/Button';
import { canCreatePostCommunity } from '~/v4/social/utils';

export function SelectPostTargetPage() {
const { client } = useSDK();

const pageId = 'select_post_target_page';
const { themeStyles } = useAmityPage({
pageId,
Expand All @@ -40,31 +43,33 @@ export function SelectPostTargetPage() {
node: intersectionNode,
});

const renderCommunity = communities.map((community) => {
return (
<div
onClick={() => {
AmityPostTargetSelectionPage?.goToPostComposerPage?.({
targetId: community.communityId,
targetType: 'community',
mode: Mode.CREATE,
community: community,
});
}}
key={community.communityId}
className={styles.selectPostTargetPage__timeline}
>
<div className={styles.selectPostTargetPage__communityAvatar}>
<CommunityAvatar pageId={pageId} community={community} />
</div>
<CommunityDisplayName pageId={pageId} community={community} />
<div>
{community.isOfficial && <CommunityOfficialBadge />}
{!community.isPublic && <CommunityPrivateBadge />}
const renderCommunity = communities
.filter((community) => canCreatePostCommunity(client, community))
.map((community) => {
return (
<div
onClick={() => {
AmityPostTargetSelectionPage?.goToPostComposerPage?.({
targetId: community.communityId,
targetType: 'community',
mode: Mode.CREATE,
community: community,
});
}}
key={community.communityId}
className={styles.selectPostTargetPage__timeline}
>
<div className={styles.selectPostTargetPage__communityAvatar}>
<CommunityAvatar pageId={pageId} community={community} />
</div>
<CommunityDisplayName pageId={pageId} community={community} />
<div>
{community.isOfficial && <CommunityOfficialBadge />}
{!community.isPublic && <CommunityPrivateBadge />}
</div>
</div>
</div>
);
});
);
});

return (
<div className={styles.selectPostTargetPage} style={themeStyles}>
Expand Down
19 changes: 19 additions & 0 deletions src/v4/social/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,22 @@ export function formatTimeAgo(dateString: string | Date | undefined) {
return `${hours}h`;
}
}

export function canCreatePostCommunity(
client: Amity.Client | null | undefined,
community: Amity.Community,
) {
if (!client) {
return false;
}

const communityPermission = client
.hasPermission(Permissions.CreatePivillegedPostPermission)
.community(community.communityId);

if (communityPermission || community.postSetting !== 'ONLY_ADMIN_CAN_POST') {
return true;
}

return false;
}

0 comments on commit a22c067

Please sign in to comment.