diff --git a/components/StateHandlers/ActionSheetStateHandler.tsx b/components/StateHandlers/ActionSheetStateHandler.tsx index fbdd07982..59a8ef90d 100644 --- a/components/StateHandlers/ActionSheetStateHandler.tsx +++ b/components/StateHandlers/ActionSheetStateHandler.tsx @@ -15,6 +15,10 @@ export default function ActionSheetStateHandler() { options: ActionSheetOptions, callback: (i?: number | undefined) => void | Promise ) => { + if (options.options.length === 1) { + callback(0); + return; + } useAppStore.getState().setActionSheetShown(true); _showActionSheetWithOptions(options, (i?: number | undefined) => { useAppStore.getState().setActionSheetShown(false); diff --git a/containers/GroupScreenMembersTable.tsx b/containers/GroupScreenMembersTable.tsx index 8dd9b7bd8..930ae1b91 100644 --- a/containers/GroupScreenMembersTable.tsx +++ b/containers/GroupScreenMembersTable.tsx @@ -7,6 +7,7 @@ import { actionSheetColors, textSecondaryColor } from "@styles/colors"; import { getAccountIsAdmin, getAccountIsSuperAdmin, + getAddressIsAdmin, getAddressIsSuperAdmin, } from "@utils/groupUtils/adminUtils"; import { getGroupMemberActions } from "@utils/groupUtils/getGroupMemberActions"; @@ -77,6 +78,11 @@ export const GroupScreenMembersTable: FC = memo( [currentAccount, members] ); + const currentAccountIsAdmin = useMemo( + () => getAddressIsAdmin(members, currentAccount), + [currentAccount, members] + ); + const tableViewItems = useMemo(() => { const items: TableViewItemType[] = []; @@ -103,13 +109,14 @@ export const GroupScreenMembersTable: FC = memo( revokeSuperAdminIndex, removeIndex, destructiveButtonIndex, - } = getGroupMemberActions( - groupPermissionPolicy, + } = getGroupMemberActions({ + groupPermissionLevel: groupPermissionPolicy, isCurrentUser, isSuperAdmin, isAdmin, - currentAccountIsSuperAdmin - ); + currentAccountIsSuperAdmin, + currentAccountIsAdmin, + }); showActionSheetWithOptions( { options, @@ -201,6 +208,7 @@ export const GroupScreenMembersTable: FC = memo( colorScheme, currentAccount, currentAccountIsSuperAdmin, + currentAccountIsAdmin, groupPermissionPolicy, mappedData, members, diff --git a/features/conversation/conversation-message/conversation-message-content-types/conversation-message-chat-group-update.tsx b/features/conversation/conversation-message/conversation-message-content-types/conversation-message-chat-group-update.tsx index 16ef8f749..ed5bd46f2 100644 --- a/features/conversation/conversation-message/conversation-message-content-types/conversation-message-chat-group-update.tsx +++ b/features/conversation/conversation-message/conversation-message-content-types/conversation-message-chat-group-update.tsx @@ -112,7 +112,7 @@ export function ChatGroupMemberLeft({ inboxId }: IChatGroupMemberLeftProps) { {readableName} - {translate("group_member_joined")} + {translate("group_member_left")} ); diff --git a/utils/groupUtils/getGroupMemberActions.test.ts b/utils/groupUtils/getGroupMemberActions.test.ts index 1311ff6a1..dbe475f19 100644 --- a/utils/groupUtils/getGroupMemberActions.test.ts +++ b/utils/groupUtils/getGroupMemberActions.test.ts @@ -3,85 +3,91 @@ import { getGroupMemberActions } from "./getGroupMemberActions"; describe("getGroupMemberActions", () => { test("should return correct actions when user can promote to admin", () => { - const result = getGroupMemberActions( - { + const result = getGroupMemberActions({ + groupPermissionLevel: { addAdminPolicy: "allow", } as PermissionPolicySet, - false, // isCurrentUser - false, // isSuperAdmin - false, // isAdmin - true // currentAccountIsSuperAdmin - ); + isCurrentUser: false, + isSuperAdmin: false, + isAdmin: false, + currentAccountIsSuperAdmin: true, + currentAccountIsAdmin: true, + }); expect(result.options).toContain("Promote to admin"); }); test("should return correct actions when user can promote to super admin", () => { - const result = getGroupMemberActions( - { + const result = getGroupMemberActions({ + groupPermissionLevel: { addAdminPolicy: "allow", } as PermissionPolicySet, - false, // isCurrentUser - false, // isSuperAdmin - false, // isAdmin - true // currentAccountIsSuperAdmin - ); + isCurrentUser: false, + isSuperAdmin: false, + isAdmin: false, + currentAccountIsSuperAdmin: true, + currentAccountIsAdmin: true, + }); expect(result.options).toContain("Promote to super admin"); }); test("should return correct actions when user can revoke admin", () => { - const result = getGroupMemberActions( - { - addAdminPolicy: "allow", + const result = getGroupMemberActions({ + groupPermissionLevel: { + removeAdminPolicy: "allow", } as PermissionPolicySet, - false, // isCurrentUser - false, // isSuperAdmin - true, // isAdmin - true // currentAccountIsSuperAdmin - ); + isCurrentUser: false, + isSuperAdmin: false, + isAdmin: true, + currentAccountIsSuperAdmin: true, + currentAccountIsAdmin: true, + }); expect(result.options).toContain("Revoke admin"); }); test("should return correct actions when user can revoke super admin", () => { - const result = getGroupMemberActions( - { + const result = getGroupMemberActions({ + groupPermissionLevel: { addAdminPolicy: "allow", } as PermissionPolicySet, - false, // isCurrentUser - true, // isSuperAdmin - false, // isAdmin - true // currentAccountIsSuperAdmin - ); + isCurrentUser: false, + isSuperAdmin: true, + isAdmin: false, + currentAccountIsSuperAdmin: true, + currentAccountIsAdmin: true, + }); expect(result.options).toContain("Revoke super admin"); }); test("should return correct actions when user can remove from group", () => { - const result = getGroupMemberActions( - { + const result = getGroupMemberActions({ + groupPermissionLevel: { removeMemberPolicy: "allow", } as PermissionPolicySet, - false, // isCurrentUser - false, // isSuperAdmin - false, // isAdmin - true // currentAccountIsSuperAdmin - ); + isCurrentUser: false, + isSuperAdmin: false, + isAdmin: false, + currentAccountIsSuperAdmin: true, + currentAccountIsAdmin: true, + }); expect(result.options).toContain("Remove from group"); }); test("should not include admin actions for the current user", () => { - const result = getGroupMemberActions( - { + const result = getGroupMemberActions({ + groupPermissionLevel: { addAdminPolicy: "allow", } as PermissionPolicySet, - true, // isCurrentUser - false, // isSuperAdmin - false, // isAdmin - true // currentAccountIsSuperAdmin - ); + isCurrentUser: true, + isSuperAdmin: false, + isAdmin: false, + currentAccountIsSuperAdmin: true, + currentAccountIsAdmin: true, + }); expect(result.options).not.toContain("Promote to admin"); expect(result.options).not.toContain("Promote to super admin"); diff --git a/utils/groupUtils/getGroupMemberActions.ts b/utils/groupUtils/getGroupMemberActions.ts index c84c48d53..cd5f8beb3 100644 --- a/utils/groupUtils/getGroupMemberActions.ts +++ b/utils/groupUtils/getGroupMemberActions.ts @@ -2,30 +2,57 @@ import { translate } from "@i18n"; import { PermissionPolicySet } from "@xmtp/react-native-sdk/build/lib/types/PermissionPolicySet"; import { userCanDoGroupActions } from "./userCanDoGroupActions"; -export const getGroupMemberActions = ( - groupPermissionLevel: PermissionPolicySet | undefined, - isCurrentUser: boolean, - isSuperAdmin: boolean, - isAdmin: boolean, - currentAccountIsSuperAdmin: boolean -) => { +type GetGroupMemberActionsProps = { + groupPermissionLevel: PermissionPolicySet | undefined; + isCurrentUser: boolean; + isSuperAdmin: boolean; + isAdmin: boolean; + currentAccountIsSuperAdmin: boolean; + currentAccountIsAdmin: boolean; +}; + +export const getGroupMemberActions = ({ + groupPermissionLevel, + isCurrentUser, + isSuperAdmin, + isAdmin, + currentAccountIsSuperAdmin, + currentAccountIsAdmin, +}: GetGroupMemberActionsProps) => { const canRemove = !isCurrentUser && userCanDoGroupActions( groupPermissionLevel, "removeMemberPolicy", - isSuperAdmin, - isAdmin + currentAccountIsSuperAdmin, + currentAccountIsAdmin ); const canPromoteToSuperAdmin = - currentAccountIsSuperAdmin && !isSuperAdmin && !isCurrentUser; + !isSuperAdmin && !isCurrentUser && currentAccountIsSuperAdmin; const canPromoteToAdmin = - !isCurrentUser && currentAccountIsSuperAdmin && !isAdmin && !isSuperAdmin; + !isCurrentUser && + !isAdmin && + !isSuperAdmin && + userCanDoGroupActions( + groupPermissionLevel, + "addAdminPolicy", + currentAccountIsSuperAdmin, + currentAccountIsAdmin + ); + const canRevokeAdmin = - !isCurrentUser && currentAccountIsSuperAdmin && isAdmin && !isSuperAdmin; + !isCurrentUser && + isAdmin && + !isSuperAdmin && + userCanDoGroupActions( + groupPermissionLevel, + "removeAdminPolicy", + currentAccountIsSuperAdmin, + currentAccountIsAdmin + ); const canRevokeSuperAdmin = !isCurrentUser && currentAccountIsSuperAdmin && isSuperAdmin; - const options = ["Profile page"]; + const options = [translate("group_screen_member_actions.profile_page")]; let cancelButtonIndex = 1; let promoteAdminIndex: number | undefined = undefined; if (canPromoteToAdmin) { @@ -54,6 +81,7 @@ export const getGroupMemberActions = ( cancelButtonIndex++; } let removeIndex: number | undefined = undefined; + if (canRemove) { removeIndex = options.length; options.push(translate("group_screen_member_actions.remove_member")); diff --git a/utils/groupUtils/userCanDoGroupActions.ts b/utils/groupUtils/userCanDoGroupActions.ts index a872abecf..09769b112 100644 --- a/utils/groupUtils/userCanDoGroupActions.ts +++ b/utils/groupUtils/userCanDoGroupActions.ts @@ -1,6 +1,6 @@ import type { PermissionPolicySet } from "@xmtp/react-native-sdk/build/lib/types/PermissionPolicySet"; -type MemberRole = "admin" | "super_admin" | "member"; +type MemberRole = "admin" | "superAdmin" | "member"; type GetMemberRoleParams = { isSuperAdmin: boolean; @@ -11,7 +11,7 @@ const getMemberRole = ({ isSuperAdmin, isAdmin, }: GetMemberRoleParams): MemberRole => { - if (isSuperAdmin) return "super_admin"; + if (isSuperAdmin) return "superAdmin"; if (isAdmin) return "admin"; return "member"; }; @@ -29,7 +29,7 @@ export const userCanDoGroupActions = ( if (policy === "deny") return false; if ( policy === "admin" && - (memberRole === "admin" || memberRole === "super_admin") + (memberRole === "admin" || memberRole === "superAdmin") ) return true; if (policy === memberRole) return true;