roleOverwriteWarning.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {PanelAlert} from 'sentry/components/panels';
  2. import Tooltip from 'sentry/components/tooltip';
  3. import {IconInfo} from 'sentry/icons';
  4. import {tct} from 'sentry/locale';
  5. import {OrgRole, TeamRole} from 'sentry/types';
  6. type Props = {
  7. orgRole: OrgRole['id'] | undefined;
  8. orgRoleList: OrgRole[];
  9. teamRoleList: TeamRole[];
  10. isSelf?: boolean;
  11. };
  12. export const RoleOverwriteIcon: React.FC<Props> = props => {
  13. const hasOverride = hasOrgRoleOverwrite(props);
  14. if (!hasOverride) {
  15. return null;
  16. }
  17. return (
  18. <Tooltip title={getOverwriteString(props)}>
  19. <IconInfo size="sm" color="gray300" />
  20. </Tooltip>
  21. );
  22. };
  23. export const RoleOverwritePanelAlert: React.FC<Props> = props => {
  24. const hasOverride = hasOrgRoleOverwrite(props);
  25. if (!hasOverride) {
  26. return null;
  27. }
  28. return <PanelAlert>{getOverwriteString(props)}</PanelAlert>;
  29. };
  30. /**
  31. * Check that the user's org role has a minimum team role that maps to the lowest
  32. * possible team role
  33. */
  34. export function hasOrgRoleOverwrite(props: Props) {
  35. const {orgRole, orgRoleList, teamRoleList} = props;
  36. const orgRoleObj = orgRoleList.find(r => r.id === orgRole);
  37. return teamRoleList.findIndex(r => r.id === orgRoleObj?.minimumTeamRole) > 0;
  38. }
  39. /**
  40. * Standardize string so situations where org-level vs team-level roles is easier to recognize
  41. */
  42. export function getOverwriteString(props: Props) {
  43. const {orgRole, orgRoleList, teamRoleList, isSelf} = props;
  44. const orgRoleObj = orgRoleList.find(r => r.id === orgRole);
  45. const teamRoleObj = teamRoleList.find(r => r.id === orgRoleObj?.minimumTeamRole);
  46. if (!orgRoleObj || !teamRoleObj) {
  47. return '';
  48. }
  49. return tct(
  50. '[selfNoun] organization role as [article] [orgRole] has granted [selfPronoun] a minimum team-level role of [teamRole]',
  51. {
  52. selfNoun: isSelf ? 'Your' : "This user's",
  53. selfPronoun: isSelf ? 'you' : 'them',
  54. article: 'AEIOU'.includes(orgRoleObj.name[0]) ? 'an' : 'a',
  55. orgRole: <strong>{orgRoleObj.name}</strong>,
  56. teamRole: <strong>{teamRoleObj.name}</strong>,
  57. }
  58. );
  59. }
  60. export default RoleOverwriteIcon;