123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- import {useEffect, useState} from 'react';
- import styled from '@emotion/styled';
- import debounce from 'lodash/debounce';
- import isEqual from 'lodash/isEqual';
- import {hasEveryAccess} from 'sentry/components/acl/access';
- import AvatarList from 'sentry/components/avatar/avatarList';
- import TeamAvatar from 'sentry/components/avatar/teamAvatar';
- import Badge from 'sentry/components/badge/badge';
- import FeatureBadge from 'sentry/components/badge/featureBadge';
- import {Button} from 'sentry/components/button';
- import ButtonBar from 'sentry/components/buttonBar';
- import {CompactSelect} from 'sentry/components/compactSelect';
- import {CheckWrap} from 'sentry/components/compactSelect/styles';
- import UserBadge from 'sentry/components/idBadge/userBadge';
- import {InnerWrap, LeadingItems} from 'sentry/components/menuListItem';
- import {Tooltip} from 'sentry/components/tooltip';
- import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
- import {t, tct} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import type {Team} from 'sentry/types/organization';
- import type {User} from 'sentry/types/user';
- import {defined} from 'sentry/utils';
- import useOrganization from 'sentry/utils/useOrganization';
- import {useTeams} from 'sentry/utils/useTeams';
- import {useTeamsById} from 'sentry/utils/useTeamsById';
- import {useUser} from 'sentry/utils/useUser';
- import type {
- DashboardDetails,
- DashboardListItem,
- DashboardPermissions,
- } from 'sentry/views/dashboards/types';
- interface EditAccessSelectorProps {
- dashboard: DashboardDetails | DashboardListItem;
- listOnly?: boolean;
- onChangeEditAccess?: (newDashboardPermissions: DashboardPermissions) => void;
- }
- /**
- * Dropdown multiselect button to enable selective Dashboard editing access to
- * specific users and teams
- */
- function EditAccessSelector({
- dashboard,
- onChangeEditAccess,
- listOnly = false,
- }: EditAccessSelectorProps) {
- const currentUser: User = useUser();
- const dashboardCreator: User | undefined = dashboard.createdBy;
- const organization = useOrganization();
- const userCanEditDashboardPermissions =
- dashboardCreator?.id === currentUser.id ||
- hasEveryAccess(['org:write'], {organization});
- // Retrieves teams from the team store, which may contain only a subset of all teams
- const {teams: teamsToRender} = useTeamsById();
- const {onSearch} = useTeams();
- const teamIds: string[] = Object.values(teamsToRender).map(team => team.id);
- const [selectedOptions, setSelectedOptions] = useState<string[]>(getSelectedOptions());
- const [isMenuOpen, setMenuOpen] = useState<boolean>(false);
- const {teams: selectedTeam} = useTeamsById({ids: [selectedOptions[1]]});
- // Effect to update selectedOptions whenever the dashboard changes
- useEffect(() => {
- const teamIdsList: string[] = Object.values(teamsToRender).map(team => team.id);
- if (!defined(dashboard.permissions) || dashboard.permissions.isEditableByEveryone) {
- setSelectedOptions(['_creator', '_allUsers', ...teamIdsList]);
- } else {
- const permittedTeamIds =
- dashboard.permissions.teamsWithEditAccess?.map(teamId => String(teamId)) ?? [];
- setSelectedOptions(['_creator', ...permittedTeamIds]);
- }
- }, [dashboard, teamsToRender]);
- // Handles state change when dropdown options are selected
- const onSelectOptions = newSelectedOptions => {
- let newSelectedValues = newSelectedOptions.map(
- (option: {value: string}) => option.value
- );
- const areAllTeamsSelected = teamIds.every(teamId =>
- newSelectedValues.includes(teamId)
- );
- if (
- !selectedOptions.includes('_allUsers') &&
- newSelectedValues.includes('_allUsers')
- ) {
- newSelectedValues = ['_creator', '_allUsers', ...teamIds];
- } else if (
- selectedOptions.includes('_allUsers') &&
- !newSelectedValues.includes('_allUsers')
- ) {
- newSelectedValues = ['_creator'];
- } else {
- areAllTeamsSelected
- ? // selecting all teams deselects 'all users'
- (newSelectedValues = ['_creator', '_allUsers', ...teamIds])
- : // deselecting any team deselects 'all users'
- (newSelectedValues = newSelectedValues.filter(value => value !== '_allUsers'));
- }
- setSelectedOptions(newSelectedValues);
- };
- // Creates a permissions object based on the options selected
- function getDashboardPermissions() {
- return {
- isEditableByEveryone: selectedOptions.includes('_allUsers'),
- teamsWithEditAccess: selectedOptions.includes('_allUsers')
- ? []
- : selectedOptions
- .filter(option => option !== '_creator')
- .map(teamId => parseInt(teamId, 10)),
- };
- }
- // Gets selected options for the dropdown from dashboard object
- function getSelectedOptions(): string[] {
- if (!defined(dashboard.permissions) || dashboard.permissions.isEditableByEveryone) {
- return ['_creator', '_allUsers', ...teamIds];
- }
- const permittedTeamIds =
- dashboard.permissions.teamsWithEditAccess?.map(teamId => String(teamId)) ?? [];
- return ['_creator', ...permittedTeamIds];
- }
- // Dashboard creator option in the dropdown
- const makeCreatorOption = () => ({
- value: '_creator',
- label: (
- <UserBadge
- avatarSize={18}
- user={dashboardCreator}
- displayName={
- <StyledDisplayName>
- {dashboardCreator?.id === currentUser.id
- ? tct('You ([email])', {email: currentUser.email})
- : dashboardCreator?.email ||
- tct('You ([email])', {email: currentUser.email})}
- </StyledDisplayName>
- }
- displayEmail={t('Creator')}
- />
- ),
- textValue: `creator_${currentUser.email}`,
- disabled: true,
- });
- // Single team option in the dropdown
- const makeTeamOption = (team: Team) => ({
- value: team.id,
- label: `#${team.slug}`,
- leadingItems: <TeamAvatar team={team} size={18} />,
- });
- // Avatars/Badges in the Edit Access Selector Button
- const triggerAvatars =
- selectedOptions.includes('_allUsers') || !dashboardCreator ? (
- <StyledBadge key="_all" text={'All'} size={listOnly ? 26 : 20} />
- ) : selectedOptions.length === 2 ? (
- // Case where we display 1 Creator Avatar + 1 Team Avatar
- <StyledAvatarList
- key="avatar-list-2-badges"
- typeAvatars="users"
- users={[dashboardCreator]}
- teams={selectedTeam ? selectedTeam : []}
- maxVisibleAvatars={1}
- avatarSize={listOnly ? 30 : 25}
- renderUsersFirst
- tooltipOptions={{disabled: !userCanEditDashboardPermissions}}
- />
- ) : (
- // Case where we display 1 Creator Avatar + a Badge with no. of teams selected
- <StyledAvatarList
- key="avatar-list-many-teams"
- typeAvatars="users"
- users={Array(selectedOptions.length).fill(dashboardCreator)}
- maxVisibleAvatars={1}
- avatarSize={listOnly ? 30 : 25}
- tooltipOptions={{disabled: !userCanEditDashboardPermissions}}
- />
- );
- const allDropdownOptions = [
- makeCreatorOption(),
- {
- value: '_all_users_section',
- options: [
- {
- value: '_allUsers',
- label: t('All users'),
- disabled: !userCanEditDashboardPermissions,
- },
- ],
- },
- {
- value: '_teams',
- label: t('Teams'),
- options: teamsToRender.map(makeTeamOption),
- showToggleAllButton: userCanEditDashboardPermissions,
- disabled: !userCanEditDashboardPermissions,
- },
- ];
- // Save and Cancel Buttons
- const dropdownFooterButtons = (
- <FilterButtons>
- <Button
- size="sm"
- onClick={() => {
- setMenuOpen(false);
- if (isMenuOpen) {
- setSelectedOptions(getSelectedOptions());
- }
- }}
- disabled={!userCanEditDashboardPermissions}
- >
- {t('Cancel')}
- </Button>
- <Button
- size="sm"
- onClick={() => {
- const isDefaultState =
- !defined(dashboard.permissions) && selectedOptions.includes('_allUsers');
- const newDashboardPermissions = getDashboardPermissions();
- if (
- !isDefaultState &&
- !isEqual(newDashboardPermissions, dashboard.permissions)
- ) {
- onChangeEditAccess?.(newDashboardPermissions);
- }
- setMenuOpen(!isMenuOpen);
- }}
- priority="primary"
- disabled={
- !userCanEditDashboardPermissions ||
- isEqual(getDashboardPermissions(), dashboard.permissions)
- }
- >
- {t('Save Changes')}
- </Button>
- </FilterButtons>
- );
- const dropdownMenu = (
- <StyledCompactSelect
- data-test-id={'edit-access-dropdown'}
- size="sm"
- onChange={newSelectedOptions => {
- onSelectOptions(newSelectedOptions);
- }}
- multiple
- searchable
- options={allDropdownOptions}
- value={selectedOptions}
- triggerLabel={
- listOnly
- ? [triggerAvatars]
- : [
- <StyledFeatureBadge
- key="beta-badge"
- type="beta"
- title={t('This feature is available for early adopters and may change')}
- tooltipProps={{position: 'left', delay: 1000, isHoverable: true}}
- />,
- t('Edit Access:'),
- triggerAvatars,
- ]
- }
- triggerProps={{borderless: listOnly}}
- searchPlaceholder={t('Search Teams')}
- isOpen={isMenuOpen}
- onOpenChange={() => {
- setMenuOpen(!isMenuOpen);
- if (isMenuOpen) {
- setSelectedOptions(getSelectedOptions());
- }
- }}
- menuFooter={dropdownFooterButtons}
- onSearch={debounce(val => void onSearch(val), DEFAULT_DEBOUNCE_DURATION)}
- />
- );
- return (
- <Tooltip
- title={t('Only the creator of the dashboard can edit permissions')}
- disabled={userCanEditDashboardPermissions || isMenuOpen}
- >
- {dropdownMenu}
- </Tooltip>
- );
- }
- export default EditAccessSelector;
- const StyledCompactSelect = styled(CompactSelect)`
- ${InnerWrap} {
- align-items: center;
- }
- ${LeadingItems} {
- margin-top: 0;
- }
- ${CheckWrap} {
- padding-bottom: 0;
- }
- `;
- const StyledDisplayName = styled('div')`
- font-weight: normal;
- `;
- const StyledAvatarList = styled(AvatarList)`
- margin-left: 10px;
- margin-right: -3px;
- `;
- const StyledFeatureBadge = styled(FeatureBadge)`
- margin-left: 0px;
- margin-right: 6px;
- `;
- const StyledBadge = styled(Badge)<{size: number}>`
- color: ${p => p.theme.white};
- background: ${p => p.theme.purple300};
- padding: 0;
- height: ${p => p.size}px;
- width: ${p => p.size}px;
- display: flex;
- justify-content: center;
- align-items: center;
- `;
- const FilterButtons = styled(ButtonBar)`
- display: grid;
- gap: ${space(1.5)};
- margin-top: ${space(0.5)};
- margin-bottom: ${space(0.5)};
- justify-content: flex-end;
- `;
|