apiNewToken.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {useState} from 'react';
  2. import ApiForm from 'sentry/components/forms/apiForm';
  3. import TextareaField from 'sentry/components/forms/fields/textareaField';
  4. import TextField from 'sentry/components/forms/fields/textField';
  5. import ExternalLink from 'sentry/components/links/externalLink';
  6. import Panel from 'sentry/components/panels/panel';
  7. import PanelBody from 'sentry/components/panels/panelBody';
  8. import PanelHeader from 'sentry/components/panels/panelHeader';
  9. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  10. import {t, tct} from 'sentry/locale';
  11. import type {Permissions} from 'sentry/types/integrations';
  12. import type {NewInternalAppApiToken} from 'sentry/types/user';
  13. import {browserHistory} from 'sentry/utils/browserHistory';
  14. import getDynamicText from 'sentry/utils/getDynamicText';
  15. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  16. import NewTokenHandler from 'sentry/views/settings/components/newTokenHandler';
  17. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  18. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  19. import PermissionSelection from 'sentry/views/settings/organizationDeveloperSettings/permissionSelection';
  20. const API_INDEX_ROUTE = '/settings/account/api/auth-tokens/';
  21. export default function ApiNewToken() {
  22. const [permissions, setPermissions] = useState<Permissions>({
  23. Event: 'no-access',
  24. Team: 'no-access',
  25. Member: 'no-access',
  26. Project: 'no-access',
  27. Release: 'no-access',
  28. Organization: 'no-access',
  29. Alerts: 'no-access',
  30. });
  31. const [newToken, setNewToken] = useState<NewInternalAppApiToken | null>(null);
  32. const [preview, setPreview] = useState<string>('');
  33. const getPreview = () => {
  34. let previewString = '';
  35. for (const k in permissions) {
  36. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  37. if (permissions[k] !== 'no-access') {
  38. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  39. previewString += `${k.toLowerCase()}:${permissions[k]}\n`;
  40. }
  41. }
  42. return previewString;
  43. };
  44. const onCancel = () => {
  45. browserHistory.push(normalizeUrl(API_INDEX_ROUTE));
  46. };
  47. const handleGoBack = () => {
  48. browserHistory.push(normalizeUrl(API_INDEX_ROUTE));
  49. };
  50. return (
  51. <SentryDocumentTitle title={t('Create User Auth Token')}>
  52. <div>
  53. <SettingsPageHeader title={t('Create New User Auth Token')} />
  54. <TextBlock>
  55. {t(
  56. "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."
  57. )}
  58. </TextBlock>
  59. <TextBlock>
  60. {tct(
  61. 'For more information on how to use the web API, see our [link:documentation].',
  62. {
  63. link: <ExternalLink href="https://docs.sentry.io/api/" />,
  64. }
  65. )}
  66. </TextBlock>
  67. {newToken !== null ? (
  68. <NewTokenHandler
  69. token={
  70. getDynamicText({value: newToken.token, fixed: 'CI_AUTH_TOKEN'}) ||
  71. 'CI_AUTH_TOKEN'
  72. }
  73. handleGoBack={handleGoBack}
  74. />
  75. ) : (
  76. <div>
  77. <ApiForm
  78. apiMethod="POST"
  79. apiEndpoint="/api-tokens/"
  80. initialData={{scopes: [], name: ''}}
  81. onSubmitSuccess={setNewToken}
  82. onCancel={onCancel}
  83. footerStyle={{
  84. marginTop: 0,
  85. paddingRight: 20,
  86. }}
  87. submitDisabled={Object.values(permissions).every(
  88. value => value === 'no-access'
  89. )}
  90. submitLabel={t('Create Token')}
  91. >
  92. <Panel>
  93. <PanelHeader>{t('General')}</PanelHeader>
  94. <PanelBody>
  95. <TextField
  96. name="name"
  97. label={t('Name')}
  98. help={t('A name to help you identify this token.')}
  99. />
  100. </PanelBody>
  101. </Panel>
  102. <Panel>
  103. <PanelHeader>{t('Permissions')}</PanelHeader>
  104. <PanelBody>
  105. <PermissionSelection
  106. appPublished={false}
  107. permissions={permissions}
  108. onChange={p => {
  109. setPermissions(p);
  110. setPreview(getPreview());
  111. }}
  112. />
  113. </PanelBody>
  114. <TextareaField
  115. name="permissions-preview"
  116. label={t('Permissions Preview')}
  117. help={t('Your token will have the following scopes.')}
  118. rows={3}
  119. autosize
  120. placeholder={preview}
  121. disabled
  122. />
  123. </Panel>
  124. </ApiForm>
  125. </div>
  126. )}
  127. </div>
  128. </SentryDocumentTitle>
  129. );
  130. }