Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 16x 10x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 1x 1x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 8x 8x 8x 8x 8x 6x 6x 6x 3x 3x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x | import { capitalize, uniq, uniqBy } from 'lodash-es';
import { memo, useMemo } from 'react';
import { FormattedMessage, FormattedNumber, useIntl } from 'react-intl';
import {
Badge,
CountBadge,
DropdownList,
Skeleton,
Tooltip,
UnstyledButton,
type DropdownListProps,
} from '@allshares/studio-design-system';
import { getAssignmentsWithStatus } from '@amalia/assignments/common/components';
import { UserPrettyFormat } from '@amalia/data-capture/fields/components';
import { assert } from '@amalia/ext/typescript';
import { useTeamsAssignments } from '@amalia/tenants/assignments/teams/state';
import { TeamRole, type TeamAssignmentWithUser } from '@amalia/tenants/assignments/teams/types';
import { getTeamDescendants, makeTeamsTree } from '@amalia/tenants/teams/shared/tree';
import { useTeams } from '@amalia/tenants/teams/state';
import { type TeamContract } from '@amalia/tenants/teams/types';
import { teamRoleMessages } from '@amalia/tenants/users/profile/components';
import { formatUserFullName, type UserContract } from '@amalia/tenants/users/types';
const useMapUserToDropdownItem = () => {
const { formatList, formatMessage } = useIntl();
return ({
userId,
teamId,
teamAssignments,
}: {
userId: UserContract['id'];
teamId: TeamContract['id'];
teamAssignments: TeamAssignmentWithUser[];
}) => {
const user = teamAssignments.find((assignment) => assignment.userId === userId)?.user;
const userAssignments = teamAssignments.filter((assignment) => assignment.userId === userId);
assert(user, 'user cannot be undefined here.');
assert(userAssignments, 'userAssignments cannot be undefined here.');
return {
key: `${teamId}-${userId}`,
filterLabel: formatUserFullName(user),
secondaryLabel: capitalize(
formatList(
[TeamRole.TEAM_MANAGER, TeamRole.TEAM_EMPLOYEE]
.map(
(role) =>
userAssignments.some((assignment) => assignment.teamRole === role) &&
formatMessage(teamRoleMessages[role]),
)
.filter(Boolean),
{ style: 'short', type: 'unit' },
),
),
label: (
<UserPrettyFormat
firstName={user.firstName}
lastName={user.lastName}
pictureURL={user.pictureURL}
/>
),
};
};
};
const useMapTeamToDropdownGroup = () => {
const mapUserToDropdownItem = useMapUserToDropdownItem();
return ({ team, teamAssignments }: { team: TeamContract; teamAssignments: TeamAssignmentWithUser[] }) => {
const assignmentsOfTeam = teamAssignments.filter((assignment) => assignment.teamId === team.id);
// Get user ids who are assigned to the descendant team.
const teamUserIds = uniq(assignmentsOfTeam.map((assignment) => assignment.userId));
return {
initialIsOpen: false,
label: team.name,
countBadge: (
<CountBadge>
<FormattedNumber value={teamUserIds.length} />
</CountBadge>
),
items: teamUserIds
.map((userId) =>
mapUserToDropdownItem({
userId,
teamId: team.id,
teamAssignments: assignmentsOfTeam,
}),
)
.toSorted((a, b) => a.filterLabel.localeCompare(b.filterLabel)),
};
};
};
export type ManagerAccessBadgeProps = {
readonly teamId: TeamContract['id'];
};
export const ManagerAccessBadge = memo(function ManagerAccessBadge({ teamId }: ManagerAccessBadgeProps) {
const mapTeamToDropdownGroup = useMapTeamToDropdownGroup();
const { teamsList, isPending: isTeamsPending } = useTeams({ showArchivedTeams: true, sortByName: true });
const teamNodes = useMemo(() => makeTeamsTree(teamsList), [teamsList]);
const teamNode = teamNodes.find((node) => node.team.id === teamId);
const teamDescendants = useMemo(() => (teamNode ? getTeamDescendants<TeamContract>(teamNode) : []), [teamNode]);
const { data: teamAssignments, isPending: isAssignmentsPending } = useTeamsAssignments({
teamsIds: [teamNode?.team.id, ...teamDescendants.map((descendant) => descendant.id)].filter(Boolean),
relations: ['user'],
});
const activeTeamAssignments = useMemo(
() => (teamAssignments ? getAssignmentsWithStatus(teamAssignments, 'active') : []),
[teamAssignments],
);
const isPending = isTeamsPending || isAssignmentsPending;
const elligibleAssignments = useMemo(
() =>
// Ignore manager assignments for the current team, since managers don't have access to statements of other managers unless they are also assigned as employees.
activeTeamAssignments.filter(
(assignment) => assignment.teamRole === TeamRole.TEAM_EMPLOYEE || assignment.teamId !== teamNode?.team.id,
),
[activeTeamAssignments, teamNode?.team.id],
);
const [usersCount, teamsCount] = useMemo(() => {
const usersCount = uniqBy(elligibleAssignments, 'userId').length;
const teamsCount = uniqBy(elligibleAssignments, 'teamId').length;
return [usersCount, teamsCount];
}, [elligibleAssignments]);
const dropdownListItems: DropdownListProps['items'] = useMemo(
() =>
[
teamNode && mapTeamToDropdownGroup({ team: teamNode.team, teamAssignments: elligibleAssignments }),
...teamDescendants
.toSorted((a, b) => a.name.localeCompare(b.name))
.map((descendantTeam) =>
mapTeamToDropdownGroup({ team: descendantTeam, teamAssignments: elligibleAssignments }),
),
].filter(Boolean),
[teamNode, teamDescendants, elligibleAssignments, mapTeamToDropdownGroup],
);
return (
<Skeleton
shape="round"
visible={isPending}
>
<DropdownList
disabled={isPending || !usersCount}
id="manager-access"
items={dropdownListItems}
>
<UnstyledButton disabled={!usersCount}>
<Tooltip
disabled={!!usersCount}
content={
<FormattedMessage defaultMessage="This team has no assigned employees and no children teams.{br}Managers will only have access to their own data." />
}
>
<Badge variant={usersCount > 0 ? 'primary' : 'grey'}>
<FormattedMessage
defaultMessage="Access to {usersCount, plural, one {# user} other {# users}} from {teamsCount, plural, one {# team} other {# teams}}"
values={{ usersCount, teamsCount }}
/>
</Badge>
</Tooltip>
</UnstyledButton>
</DropdownList>
</Skeleton>
);
});
|