roleOverwriteWarning.tsx 2.1 KB

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