|
| 1 | +import { useUserHighestRolePosition, useUserServerPerms } from '@/lib/store'; |
| 2 | +import { decrementRolePosition, deleteRole, incrementRolePosition, updateRole } from '@/services/roles.service'; |
| 3 | +import { Role } from '@/types/dbtypes'; |
| 4 | +import { ServerPermissions } from '@/types/permissions'; |
| 5 | +import { useSupabaseClient } from '@supabase/auth-helpers-react'; |
| 6 | +import { useForm } from 'react-hook-form'; |
| 7 | +import { toast } from 'react-toastify'; |
| 8 | + |
| 9 | +const permissionEnumToInfoMap = new Map([ |
| 10 | + [ServerPermissions.OWNER, ['Owner', '']], |
| 11 | + [ServerPermissions.ADMINISTRATOR, ['Administrator', 'Grants all permissions in the server (assign this with caution!)']], |
| 12 | + [ServerPermissions.MANAGE_CHANNELS, ['Manage Channels', 'Allows the user to create, edit, and delete channels']], |
| 13 | + [ServerPermissions.MANAGE_ROLES, ['Manage Roles', 'Allows the user to create, edit, and delete roles']], |
| 14 | + [ServerPermissions.MANAGE_SERVER, ['Manage Server', 'Allows the user to edit server settings like name, descripion, and icon']], |
| 15 | + [ServerPermissions.MANAGE_USERS, ['Manage Users', 'Allows the user to kick and ban users']], |
| 16 | + [ServerPermissions.MANAGE_MESSAGES, ['Manage Messages', 'Allows the user to delete and pin messages']], |
| 17 | + [ServerPermissions.MANAGE_INVITES, ['Manage Invites', 'Allows the user to edit, and delete invites']], |
| 18 | + [ServerPermissions.CREATE_INVITES, ['Create Invites', 'Allows the user to create invites']], |
| 19 | +]); |
| 20 | + |
| 21 | +type RoleEditFormResult = { |
| 22 | + name: string; |
| 23 | + position: number; |
| 24 | + permissions: string | string[]; |
| 25 | + color: string; |
| 26 | +} |
| 27 | + |
| 28 | +export function RoleEditForm({role, roles_length, max_role_position }: {role: Role, roles_length: number, max_role_position: number }) { |
| 29 | + const { register, handleSubmit, formState: { errors } } = useForm<RoleEditFormResult>(); |
| 30 | + const supabase = useSupabaseClient(); |
| 31 | + const userPerms = useUserServerPerms(); |
| 32 | + const userHighestRolePosition = max_role_position; // useUserHighestRolePosition(); |
| 33 | + |
| 34 | + const isFormDisabled = role.is_system || role.position <= userHighestRolePosition; |
| 35 | + const serverPermissions = Object.entries(ServerPermissions).filter( |
| 36 | + ([key, value]) => typeof value === 'number' && key !== 'NONE' && key !== 'OWNER' |
| 37 | + ); |
| 38 | + |
| 39 | + const onSubmit = async (formData: RoleEditFormResult) => { |
| 40 | + if (typeof formData.permissions === 'string') { |
| 41 | + formData.permissions = [ formData.permissions ]; |
| 42 | + } |
| 43 | + |
| 44 | + else if (typeof formData.permissions === 'boolean') { |
| 45 | + formData.permissions = [ '0' ]; |
| 46 | + } |
| 47 | + |
| 48 | + const perms = formData.permissions.reduce((acc, cur) => acc | parseInt(cur), 0); |
| 49 | + |
| 50 | + // If a role is being edited, update it |
| 51 | + const { error } = await updateRole( |
| 52 | + supabase, |
| 53 | + role.id, |
| 54 | + formData.name, |
| 55 | + role.position, |
| 56 | + perms, |
| 57 | + formData.color.replace('#', '') |
| 58 | + ); |
| 59 | + |
| 60 | + if (error) { |
| 61 | + console.error(error); |
| 62 | + toast.error('Failed to update role'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + toast.success('Role saved'); |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 71 | + <p className="text-base text-gray-400 text-left w-full">Display</p> |
| 72 | + <div className="w-full space-x-2 mb-2 flex flex-row"> |
| 73 | + <input |
| 74 | + type="text" |
| 75 | + className="text-xl rounded-md h-7 p-2 bg-grey-900 w-full" |
| 76 | + disabled={isFormDisabled} |
| 77 | + defaultValue={role.name} |
| 78 | + placeholder='Role Name' |
| 79 | + {...register('name', { required: true })} |
| 80 | + /> |
| 81 | + |
| 82 | + <input |
| 83 | + type="color" |
| 84 | + className="rounded-md h-7" |
| 85 | + disabled={isFormDisabled} |
| 86 | + defaultValue={`#${(role.color || 'a9aaab')}`} |
| 87 | + {...register('color', { required: true })} |
| 88 | + /> |
| 89 | + </div> |
| 90 | + <p className="text-base text-gray-400 text-left w-full">Heirarchy position</p> |
| 91 | + <div className="flex flex-row space-x-2"> |
| 92 | + <button |
| 93 | + className="border-2 border-gray-400 rounded-md h-6 w-full mt-2 disabled:border-grey-800" |
| 94 | + type="button" |
| 95 | + onClick={async () => { |
| 96 | + console.log('up'); |
| 97 | + await incrementRolePosition(supabase, role.id); |
| 98 | + }} |
| 99 | + disabled={isFormDisabled || role.position === 1 || role.position - 1 <= userHighestRolePosition} |
| 100 | + > |
| 101 | + Up |
| 102 | + </button> |
| 103 | + <button |
| 104 | + className="border-2 border-gray-400 rounded-md h-6 w-full mt-2 disabled:border-grey-800" |
| 105 | + type="button" |
| 106 | + onClick={async () => { |
| 107 | + console.log('down'); |
| 108 | + await decrementRolePosition(supabase, role.id); |
| 109 | + }} |
| 110 | + disabled={isFormDisabled || role.position === roles_length - 2} |
| 111 | + > |
| 112 | + Down |
| 113 | + </button> |
| 114 | + </div> |
| 115 | + <p className="text-base text-gray-400 text-left w-full">Permissions</p> |
| 116 | + {serverPermissions.map(([permission, value]) => ( |
| 117 | + <div key={permission} className="flex flex-row w-full"> |
| 118 | + <input |
| 119 | + type="checkbox" |
| 120 | + className="w-5 h-5 mr-2 checked:accent-green-600 disabled:accent-green-600/50" |
| 121 | + value={value} |
| 122 | + defaultChecked={( |
| 123 | + !role |
| 124 | + // @ts-expect-error permission is always a keyof ServerPermissions. This error will never be thrown |
| 125 | + || (role.permissions & value as number) === ServerPermissions[permission] |
| 126 | + )} |
| 127 | + disabled={ |
| 128 | + isFormDisabled |
| 129 | + || ((userPerms & value as number) === 0) |
| 130 | + || (permission === 'OWNER') |
| 131 | + || (permission === 'ADMINISTRATOR' && (userPerms & 2) !== 2) |
| 132 | + } |
| 133 | + {...register('permissions')} |
| 134 | + /> |
| 135 | + <div> |
| 136 | + {/* @ts-expect-error permission is always a keyof ServerPermissions. This error will never be thrown */} |
| 137 | + <label> {permissionEnumToInfoMap.get(ServerPermissions[permission])[0]}</label> |
| 138 | + {/* @ts-expect-error permission is always a keyof ServerPermissions. This error will never be thrown */} |
| 139 | + <p className="text-sm text-gray-400 text-left w-full">{permissionEnumToInfoMap.get(ServerPermissions[permission])[1]}</p> |
| 140 | + </div> |
| 141 | + |
| 142 | + </div> |
| 143 | + ))} |
| 144 | + <div className="flex flex-row space-x-2 w-full"> |
| 145 | + <button |
| 146 | + className="bg-red-500 hover:bg-red-700 disabled:bg-grey-600 rounded-md h-6 mt-2 w-full" |
| 147 | + type="button" |
| 148 | + disabled={isFormDisabled} |
| 149 | + onClick={async () => { |
| 150 | + console.log('delete role'); |
| 151 | + const { error } = await deleteRole(supabase, role.id); |
| 152 | + |
| 153 | + if (error) { |
| 154 | + console.error(error); |
| 155 | + toast.error('Failed to delete role'); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + toast.success('Role deleted'); |
| 160 | + }} |
| 161 | + > |
| 162 | + Delete |
| 163 | + </button> |
| 164 | + <button |
| 165 | + className="bg-green-500 hover:bg-green-700 disabled:bg-grey-600 rounded-md h-6 mt-2 w-full" |
| 166 | + disabled={isFormDisabled} |
| 167 | + type="submit" |
| 168 | + > |
| 169 | + Save |
| 170 | + </button> |
| 171 | + </div> |
| 172 | + </form> |
| 173 | + ); |
| 174 | +} |
0 commit comments