Skip to content

Commit

Permalink
fix: 🐛 Hide "settings" from server context menu when you don't have p…
Browse files Browse the repository at this point in the history
…ermissions to access it.
  • Loading branch information
SupertigerDev committed Jan 7, 2023
1 parent 7c900ae commit 929d4ce
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/chat-api/Bitwise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Bitwise {
description?: string;
bit: number;
icon?: string
showSettings?: boolean; // determine should this permission reveal the "settings" option context menu
}

export const USER_BADGES = {
Expand Down Expand Up @@ -31,12 +32,14 @@ export const CHANNEL_PERMISSIONS = {
icon: 'mail'
}
}

export const ROLE_PERMISSIONS = {
ADMIN: {
name: 'servers.rolePermissions.admin',
description: 'servers.rolePermissions.adminDescription',
bit: 1,
// icon: 'mail'
showSettings: true,
},
SEND_MESSAGE: {
name: 'servers.rolePermissions.sendMessage',
Expand All @@ -49,23 +52,27 @@ export const ROLE_PERMISSIONS = {
description: 'servers.rolePermissions.manageRolesDescription',
// icon: 'mail',
bit: 4,
showSettings: true,
},
MANAGE_CHANNELS: {
name: 'servers.rolePermissions.manageChannels',
description: 'servers.rolePermissions.manageChannelsDescription',
// icon: 'mail',
bit: 8,
showSettings: true,
},
KICK: {
name: 'servers.rolePermissions.kick',
description: 'servers.rolePermissions.kickDescription',
bit: 16,
// icon: 'mail'
showSettings: true,
},
BAN: {
name: 'servers.rolePermissions.ban',
description: 'servers.rolePermissions.banDescription',
bit: 32,
showSettings: true,
// icon: 'mail'
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/ServerSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ServerSetting {
icon: string;
requiredRolePermission?: Bitwise,
hideDrawer?: boolean
element: any
element: any,
}

const serverSettings: ServerSetting[] = [
Expand Down
17 changes: 15 additions & 2 deletions src/components/servers/context-menu/ContextMenuServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import RouterEndpoints from "@/common/RouterEndpoints";
import ContextMenu, { ContextMenuProps } from "@/components/ui/context-menu/ContextMenu";
import useStore from "@/chat-api/store/useStore";
import { useNavigate } from "@nerimity/solid-router";
import { Bitwise, ROLE_PERMISSIONS } from "@/chat-api/Bitwise";

type Props = Omit<ContextMenuProps, 'items'> & {
serverId?: string
Expand All @@ -11,7 +12,7 @@ type Props = Omit<ContextMenuProps, 'items'> & {
export default function ContextMenuServer (props: Props) {

const navigate = useNavigate();
const {account, servers} = useStore();
const {account, servers, serverMembers} = useStore();

const server = () => servers.get(props.serverId!);

Expand All @@ -22,12 +23,24 @@ export default function ContextMenuServer (props: Props) {
await server()?.leave();
}

const member = () => serverMembers.get(props.serverId!, account.user()?.id!);

const showSettings = () => {
if (isServerCreator()) return true;
return Object.values(ROLE_PERMISSIONS).find((p: Bitwise) => {
if (!member()?.hasPermission(p)) return false;
if (p.showSettings) return true;
return false;
})

};

return (
<ContextMenu {...props} items={[
{icon: 'markunread_mailbox', label: "Mark As Read", disabled: true},
{separator: true},
{icon: 'mail', label: "Invites", onClick: () => navigate(RouterEndpoints.SERVER_SETTINGS_INVITES(props.serverId!))},
{icon: 'settings', label: "Settings", onClick: () => navigate(RouterEndpoints.SERVER_SETTINGS_GENERAL(props.serverId!))},
...(showSettings() ? [{icon: 'settings', label: "Settings", onClick: () => navigate(RouterEndpoints.SERVER_SETTINGS_GENERAL(props.serverId!))}] : []),
{separator: true},
{icon: 'copy', label: "Copy ID", onClick: () => copyToClipboard(props.serverId!)},
{separator: true, show: !isServerCreator()},
Expand Down

0 comments on commit 929d4ce

Please sign in to comment.