keyRow.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {RouteComponentProps} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import ClippedBox from 'sentry/components/clippedBox';
  5. import Confirm from 'sentry/components/confirm';
  6. import Link from 'sentry/components/links/link';
  7. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  8. import {IconDelete} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import {Scope} from 'sentry/types';
  12. import recreateRoute from 'sentry/utils/recreateRoute';
  13. import ProjectKeyCredentials from 'sentry/views/settings/project/projectKeys/projectKeyCredentials';
  14. import {ProjectKey} from 'sentry/views/settings/project/projectKeys/types';
  15. type Props = {
  16. access: Set<Scope>;
  17. data: ProjectKey;
  18. onRemove: (data: ProjectKey) => void;
  19. onToggle: (isActive: boolean, data: ProjectKey) => void;
  20. orgId: string;
  21. projectId: string;
  22. } & Pick<RouteComponentProps<{}, {}>, 'routes' | 'location' | 'params'>;
  23. function KeyRow({data, onRemove, onToggle, access, routes, location, params}: Props) {
  24. const handleEnable = () => onToggle(true, data);
  25. const handleDisable = () => onToggle(false, data);
  26. const editUrl = recreateRoute(`${data.id}/`, {routes, params, location});
  27. const controlActive = access.has('project:write');
  28. return (
  29. <Panel>
  30. <PanelHeader hasButtons>
  31. <Title disabled={!data.isActive}>
  32. <PanelHeaderLink to={editUrl}>{data.label}</PanelHeaderLink>
  33. {!data.isActive && (
  34. <small>
  35. {' \u2014 '}
  36. {t('Disabled')}
  37. </small>
  38. )}
  39. </Title>
  40. <Controls>
  41. <Button to={editUrl} size="sm">
  42. {t('Configure')}
  43. </Button>
  44. <Confirm
  45. onConfirm={data.isActive ? handleDisable : handleEnable}
  46. confirmText={data.isActive ? t('Disable Key') : t('Enable Key')}
  47. message={
  48. data.isActive
  49. ? t('Are you sure you want to disable this key?')
  50. : t('Are you sure you want to enable this key?')
  51. }
  52. >
  53. <Button size="sm" disabled={!controlActive}>
  54. {data.isActive ? t('Disable') : t('Enable')}
  55. </Button>
  56. </Confirm>
  57. <Confirm
  58. priority="danger"
  59. onConfirm={() => onRemove(data)}
  60. confirmText={t('Remove Key')}
  61. message={t(
  62. 'Are you sure you want to remove this key? This action is irreversible.'
  63. )}
  64. >
  65. <Button
  66. size="sm"
  67. disabled={!controlActive}
  68. icon={<IconDelete />}
  69. aria-label={t('Delete')}
  70. />
  71. </Confirm>
  72. </Controls>
  73. </PanelHeader>
  74. <StyledClippedBox clipHeight={300} defaultClipped btnText={t('Expand')}>
  75. <StyledPanelBody disabled={!data.isActive}>
  76. <ProjectKeyCredentials projectId={`${data.projectId}`} data={data} />
  77. </StyledPanelBody>
  78. </StyledClippedBox>
  79. </Panel>
  80. );
  81. }
  82. export default KeyRow;
  83. const StyledClippedBox = styled(ClippedBox)`
  84. padding: 0;
  85. margin: 0;
  86. > *:last-child {
  87. padding-bottom: ${space(3)};
  88. }
  89. `;
  90. const PanelHeaderLink = styled(Link)`
  91. color: ${p => p.theme.subText};
  92. `;
  93. const Title = styled('div')<{disabled: boolean}>`
  94. flex: 1;
  95. ${p => (p.disabled ? 'opacity: 0.5;' : '')};
  96. margin-right: ${space(1)};
  97. `;
  98. const Controls = styled('div')`
  99. display: grid;
  100. align-items: center;
  101. gap: ${space(1)};
  102. grid-auto-flow: column;
  103. `;
  104. const StyledPanelBody = styled(PanelBody)<{disabled: boolean}>`
  105. ${p => (p.disabled ? 'opacity: 0.5;' : '')};
  106. `;