import React, { useState } from 'react'; import Input from 'shared/components/Input'; import updateApolloCache from 'shared/utils/cache'; import produce from 'immer'; import Button from 'shared/components/Button'; import { useCurrentUser, PermissionLevel, PermissionObjectType } from 'App/context'; import Select from 'shared/components/Select'; import { useGetTeamQuery, RoleCode, useCreateTeamMemberMutation, useDeleteTeamMemberMutation, useUpdateTeamMemberRoleMutation, GetTeamQuery, GetTeamDocument, } from 'shared/generated/graphql'; import { UserPlus, Checkmark } from 'shared/icons'; import styled, { css } from 'styled-components/macro'; import { usePopup, Popup } from 'shared/components/PopupMenu'; import TaskAssignee from 'shared/components/TaskAssignee'; import Member from 'shared/components/Member'; import ControlledInput from 'shared/components/ControlledInput'; import NOOP from 'shared/utils/noop'; const MemberListWrapper = styled.div` flex: 1 1; `; const SearchInput = styled(ControlledInput)` margin: 0 12px; `; const UserMember = styled(Member)` padding: 4px 0; cursor: pointer; &:hover { background: rgba(${props => props.theme.colors.bg.primary}, 0.4); } border-radius: 6px; `; const TeamMemberList = styled.div` margin: 8px 12px; `; type UserManagementPopupProps = { users: Array; teamMembers: Array; onAddTeamMember: (userID: string) => void; }; const UserManagementPopup: React.FC = ({ users, teamMembers, onAddTeamMember }) => { return ( {users .filter(u => u.id !== teamMembers.find(p => p.id === u.id)?.id) .map(user => ( onAddTeamMember(user.id)} showName member={user} taskID="" /> ))} ); }; export const RoleCheckmark = styled(Checkmark)` padding-left: 4px; `; const permissions = [ { code: 'owner', name: 'Owner', description: 'Can view, create and edit team projects, and change settings for the team. Will have admin rights on all projects in this team. Can delete the team and all team projects.', }, { code: 'admin', name: 'Admin', description: 'Can view, create and edit team projects, and change settings for the team. Will have admin rights on all projects in this team.', }, { code: 'member', name: 'Member', description: 'Can view, create, and edit team projects, but not change settings.' }, ]; export const RoleName = styled.div` font-size: 14px; font-weight: 700; `; export const RoleDescription = styled.div` margin-top: 4px; font-size: 14px; `; export const MiniProfileActions = styled.ul` list-style-type: none; `; export const MiniProfileActionWrapper = styled.li``; export const MiniProfileActionItem = styled.span<{ disabled?: boolean }>` color: #c2c6dc; display: block; font-weight: 400; padding: 6px 12px; position: relative; text-decoration: none; ${props => props.disabled ? css` user-select: none; pointer-events: none; color: rgba(${props.theme.colors.text.primary}, 0.4); ` : css` cursor: pointer; &:hover { background: rgb(115, 103, 240); } `} `; export const Content = styled.div` padding: 0 12px 12px; `; export const CurrentPermission = styled.span` margin-left: 4px; color: rgba(${props => props.theme.colors.text.secondary}, 0.4); `; export const Separator = styled.div` height: 1px; border-top: 1px solid #414561; margin: 0.25rem !important; `; export const WarningText = styled.span` display: flex; color: rgba(${props => props.theme.colors.text.primary}, 0.4); padding: 6px; `; export const DeleteDescription = styled.div` font-size: 14px; color: rgba(${props => props.theme.colors.text.primary}); `; export const RemoveMemberButton = styled(Button)` margin-top: 16px; padding: 6px 12px; width: 100%; `; type TeamRoleManagerPopupProps = { currentUserID: string; subject: User; members: Array; warning?: string | null; canChangeRole: boolean; onChangeRole: (roleCode: RoleCode) => void; onRemoveFromTeam?: (newOwnerID: string | null) => void; }; const TeamRoleManagerPopup: React.FC = ({ members, warning, subject, currentUserID, canChangeRole, onRemoveFromTeam, onChangeRole, }) => { const { hidePopup, setTab } = usePopup(); const [orphanedProjectOwner, setOrphanedProjectOwner] = useState<{ label: string; value: string } | null>(null); return ( <> {subject.role && ( { setTab(1); }} > Change permissions... {`(${subject.role.name})`} )} {onRemoveFromTeam && ( { setTab(2); }} > {currentUserID === subject.id ? 'Leave team...' : 'Remove from team...'} )} {warning && ( <> {warning} )} hidePopup()} tab={1}> {permissions .filter(p => (subject.role && subject.role.code === 'owner') || p.code !== 'owner') .map(perm => ( { if (onChangeRole && subject.role && perm.code !== subject.role.code) { switch (perm.code) { case 'owner': onChangeRole(RoleCode.Owner); break; case 'admin': onChangeRole(RoleCode.Admin); break; case 'member': onChangeRole(RoleCode.Member); break; default: break; } hidePopup(); } }} > {perm.name} {subject.role && perm.code === subject.role.code && } {perm.description} ))} {subject.role && subject.role.code === 'owner' && ( <> You can not change roles because there must be an owner. )} hidePopup()} tab={2}> The member will be removed from all team project tasks. They will receive a notification. {subject.owned && subject.owned.projects.length !== 0 && ( <> {`The member is the owner of ${subject.owned.projects.length} project${ subject.owned.projects.length > 1 ? 's' : '' }. You can give the projects a new owner but it is not needed`}