keyRow.tsx 3.4 KB

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