Skip to content

Commit

Permalink
fix(useServerPermissions): isRoleOrderOk logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BrickheadJohnny committed Jul 25, 2024
1 parent d6b0ec2 commit bd0ca4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ServerSetupCard = ({ onSubmit, serverId }: Props): JSX.Element => {
const revalidatePermissions = useSubmit(() =>
mutate().then((newPermissions) => {
if (newPermissions?.hasAllPermissions && newPermissions?.isRoleOrderOk) {
onSubmit()
onSubmit?.()
}
return newPermissions
})
Expand Down Expand Up @@ -86,8 +86,8 @@ const ServerSetupCard = ({ onSubmit, serverId }: Props): JSX.Element => {
colorScheme="DISCORD"
onClick={() => {
captureEvent("[discord setup] rechecking role order")
revalidatePermissions.onSubmit().then(({ isRoleOrderOk }) => {
if (!isRoleOrderOk) {
revalidatePermissions.onSubmit().then((res) => {
if (!res?.isRoleOrderOk) {
start()
}
})
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/useServerPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ export type PermissionsResponse = {
function mapPermissions(permissions: PermissionsResponse) {
const permissionsArray = Object.values(permissions.permissions)

// Only checking that it is not on the bottom
/**
* If there are only 2 roles (everyone and Guild.xyz Bot), then we can accept the default role order
* Otherwise, we check if there's at least one role below the Guild.xyz Bot role
*/
const isRoleOrderOk =
// TODO: Checking by roleName feels sketchy, maybe we could return a flag for each entry, which indicates the relevant role
permissions.roleOrders.length === 2 ||
permissions.roleOrders.find(({ roleName }) => roleName === GUILD_BOT_ROLE_NAME)
.rolePosition !== 1
?.rolePosition !== 1

return {
...permissions,
Expand Down

0 comments on commit bd0ca4b

Please sign in to comment.