apiTokens.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {
  2. addErrorMessage,
  3. addLoadingMessage,
  4. addSuccessMessage,
  5. } from 'app/actionCreators/indicator';
  6. import AlertLink from 'app/components/alertLink';
  7. import Button from 'app/components/button';
  8. import {Panel, PanelBody, PanelHeader} from 'app/components/panels';
  9. import {t, tct} from 'app/locale';
  10. import {InternalAppApiToken, Organization} from 'app/types';
  11. import withOrganization from 'app/utils/withOrganization';
  12. import AsyncView from 'app/views/asyncView';
  13. import ApiTokenRow from 'app/views/settings/account/apiTokenRow';
  14. import EmptyMessage from 'app/views/settings/components/emptyMessage';
  15. import SettingsPageHeader from 'app/views/settings/components/settingsPageHeader';
  16. import TextBlock from 'app/views/settings/components/text/textBlock';
  17. type Props = {
  18. organization: Organization;
  19. } & AsyncView['props'];
  20. type State = {
  21. tokenList: InternalAppApiToken[] | null;
  22. } & AsyncView['state'];
  23. export class ApiTokens extends AsyncView<Props, State> {
  24. getTitle() {
  25. return t('API Tokens');
  26. }
  27. getDefaultState() {
  28. return {
  29. ...super.getDefaultState(),
  30. tokenList: [],
  31. };
  32. }
  33. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  34. return [['tokenList', '/api-tokens/']];
  35. }
  36. handleRemoveToken = (token: InternalAppApiToken) => {
  37. addLoadingMessage();
  38. const oldTokenList = this.state.tokenList;
  39. this.setState(
  40. state => ({
  41. tokenList: state.tokenList?.filter(tk => tk.token !== token.token) ?? [],
  42. }),
  43. async () => {
  44. try {
  45. await this.api.requestPromise('/api-tokens/', {
  46. method: 'DELETE',
  47. data: {token: token.token},
  48. });
  49. addSuccessMessage(t('Removed token'));
  50. } catch (_err) {
  51. addErrorMessage(t('Unable to remove token. Please try again.'));
  52. this.setState({
  53. tokenList: oldTokenList,
  54. });
  55. }
  56. }
  57. );
  58. };
  59. renderBody() {
  60. const {organization} = this.props;
  61. const {tokenList} = this.state;
  62. const isEmpty = !Array.isArray(tokenList) || tokenList.length === 0;
  63. const action = (
  64. <Button
  65. priority="primary"
  66. size="small"
  67. to="/settings/account/api/auth-tokens/new-token/"
  68. data-test-id="create-token"
  69. >
  70. {t('Create New Token')}
  71. </Button>
  72. );
  73. return (
  74. <div>
  75. <SettingsPageHeader title="Auth Tokens" action={action} />
  76. <AlertLink
  77. to={`/settings/${organization?.slug ?? ''}/developer-settings/new-internal`}
  78. >
  79. {t(
  80. "Auth Tokens are tied to the logged in user, meaning they'll stop working if the user leaves the organization! We suggest using internal integrations to create/manage tokens tied to the organization instead."
  81. )}
  82. </AlertLink>
  83. <TextBlock>
  84. {t(
  85. "Authentication tokens allow you to perform actions against the Sentry API on behalf of your account. They're the easiest way to get started using the API."
  86. )}
  87. </TextBlock>
  88. <TextBlock>
  89. {tct(
  90. 'For more information on how to use the web API, see our [link:documentation].',
  91. {
  92. link: <a href="https://docs.sentry.io/api/" />,
  93. }
  94. )}
  95. </TextBlock>
  96. <Panel>
  97. <PanelHeader>{t('Auth Token')}</PanelHeader>
  98. <PanelBody>
  99. {isEmpty && (
  100. <EmptyMessage>
  101. {t("You haven't created any authentication tokens yet.")}
  102. </EmptyMessage>
  103. )}
  104. {tokenList?.map(token => (
  105. <ApiTokenRow
  106. key={token.token}
  107. token={token}
  108. onRemove={this.handleRemoveToken}
  109. />
  110. ))}
  111. </PanelBody>
  112. </Panel>
  113. </div>
  114. );
  115. }
  116. }
  117. export default withOrganization(ApiTokens);