roleOverwriteWarning.tsx 2.1 KB

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