keySettings.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {Component, Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. addSuccessMessage,
  7. } from 'sentry/actionCreators/indicator';
  8. import {Client} from 'sentry/api';
  9. import Access from 'sentry/components/acl/access';
  10. import Button from 'sentry/components/button';
  11. import Confirm from 'sentry/components/confirm';
  12. import DateTime from 'sentry/components/dateTime';
  13. import Field from 'sentry/components/forms/field';
  14. import BooleanField from 'sentry/components/forms/fields/booleanField';
  15. import SelectField from 'sentry/components/forms/fields/selectField';
  16. import TextField from 'sentry/components/forms/fields/textField';
  17. import Form from 'sentry/components/forms/form';
  18. import ExternalLink from 'sentry/components/links/externalLink';
  19. import {Panel, PanelAlert, PanelBody, PanelHeader} from 'sentry/components/panels';
  20. import TextCopyInput from 'sentry/components/textCopyInput';
  21. import {t, tct} from 'sentry/locale';
  22. import getDynamicText from 'sentry/utils/getDynamicText';
  23. import KeyRateLimitsForm from 'sentry/views/settings/project/projectKeys/details/keyRateLimitsForm';
  24. import ProjectKeyCredentials from 'sentry/views/settings/project/projectKeys/projectKeyCredentials';
  25. import {ProjectKey} from 'sentry/views/settings/project/projectKeys/types';
  26. type Props = {
  27. api: Client;
  28. data: ProjectKey;
  29. onRemove: () => void;
  30. } & Pick<
  31. RouteComponentProps<
  32. {
  33. keyId: string;
  34. orgId: string;
  35. projectId: string;
  36. },
  37. {}
  38. >,
  39. 'params'
  40. >;
  41. type State = {
  42. error: boolean;
  43. loading: boolean;
  44. };
  45. class KeySettings extends Component<Props, State> {
  46. state: State = {
  47. loading: false,
  48. error: false,
  49. };
  50. handleRemove = async () => {
  51. if (this.state.loading) {
  52. return;
  53. }
  54. addLoadingMessage(t('Revoking key\u2026'));
  55. const {api, onRemove, params} = this.props;
  56. const {keyId, orgId, projectId} = params;
  57. try {
  58. await api.requestPromise(`/projects/${orgId}/${projectId}/keys/${keyId}/`, {
  59. method: 'DELETE',
  60. });
  61. onRemove();
  62. addSuccessMessage(t('Revoked key'));
  63. } catch (_err) {
  64. this.setState({
  65. error: true,
  66. loading: false,
  67. });
  68. addErrorMessage(t('Unable to revoke key'));
  69. }
  70. };
  71. render() {
  72. const {keyId, orgId, projectId} = this.props.params;
  73. const {data} = this.props;
  74. const apiEndpoint = `/projects/${orgId}/${projectId}/keys/${keyId}/`;
  75. const loaderLink = getDynamicText({
  76. value: data.dsn.cdn,
  77. fixed: '__JS_SDK_LOADER_URL__',
  78. });
  79. return (
  80. <Access access={['project:write']}>
  81. {({hasAccess}) => (
  82. <Fragment>
  83. <Form
  84. saveOnBlur
  85. allowUndo
  86. apiEndpoint={apiEndpoint}
  87. apiMethod="PUT"
  88. initialData={data}
  89. >
  90. <Panel>
  91. <PanelHeader>{t('Details')}</PanelHeader>
  92. <PanelBody>
  93. <TextField
  94. name="name"
  95. label={t('Name')}
  96. disabled={!hasAccess}
  97. required={false}
  98. maxLength={64}
  99. />
  100. <BooleanField
  101. name="isActive"
  102. label={t('Enabled')}
  103. required={false}
  104. disabled={!hasAccess}
  105. help="Accept events from this key? This may be used to temporarily suspend a key."
  106. />
  107. <Field label={t('Created')}>
  108. <div className="controls">
  109. <DateTime date={data.dateCreated} />
  110. </div>
  111. </Field>
  112. </PanelBody>
  113. </Panel>
  114. </Form>
  115. <KeyRateLimitsForm
  116. params={this.props.params}
  117. data={data}
  118. disabled={!hasAccess}
  119. />
  120. <Form saveOnBlur apiEndpoint={apiEndpoint} apiMethod="PUT" initialData={data}>
  121. <Panel>
  122. <PanelHeader>{t('JavaScript Loader')}</PanelHeader>
  123. <PanelBody>
  124. <Field
  125. help={tct(
  126. 'Copy this script into your website to setup your JavaScript SDK without any additional configuration. [link]',
  127. {
  128. link: (
  129. <ExternalLink href="https://docs.sentry.io/platforms/javascript/install/lazy-load-sentry/">
  130. What does the script provide?
  131. </ExternalLink>
  132. ),
  133. }
  134. )}
  135. inline={false}
  136. flexibleControlStateSize
  137. >
  138. <TextCopyInput>
  139. {`<script src='${loaderLink}' crossorigin="anonymous"></script>`}
  140. </TextCopyInput>
  141. </Field>
  142. <SelectField
  143. name="browserSdkVersion"
  144. options={
  145. data.browserSdk
  146. ? data.browserSdk.choices.map(([value, label]) => ({
  147. value,
  148. label,
  149. }))
  150. : []
  151. }
  152. placeholder={t('4.x')}
  153. allowClear={false}
  154. disabled={!hasAccess}
  155. help={t(
  156. 'Select the version of the SDK that should be loaded. Note that it can take a few minutes until this change is live.'
  157. )}
  158. />
  159. </PanelBody>
  160. </Panel>
  161. </Form>
  162. <Panel>
  163. <PanelHeader>{t('Credentials')}</PanelHeader>
  164. <PanelBody>
  165. <PanelAlert type="info" showIcon>
  166. {t(
  167. 'Your credentials are coupled to a public and secret key. Different clients will require different credentials, so make sure you check the documentation before plugging things in.'
  168. )}
  169. </PanelAlert>
  170. <ProjectKeyCredentials
  171. projectId={`${data.projectId}`}
  172. data={data}
  173. showPublicKey
  174. showSecretKey
  175. showProjectId
  176. />
  177. </PanelBody>
  178. </Panel>
  179. <Access access={['project:admin']}>
  180. <Panel>
  181. <PanelHeader>{t('Revoke Key')}</PanelHeader>
  182. <PanelBody>
  183. <Field
  184. label={t('Revoke Key')}
  185. help={t(
  186. 'Revoking this key will immediately remove and suspend the credentials. This action is irreversible.'
  187. )}
  188. >
  189. <div>
  190. <Confirm
  191. priority="danger"
  192. message={t(
  193. 'Are you sure you want to revoke this key? This will immediately remove and suspend the credentials.'
  194. )}
  195. onConfirm={this.handleRemove}
  196. confirmText={t('Revoke Key')}
  197. >
  198. <Button priority="danger">{t('Revoke Key')}</Button>
  199. </Confirm>
  200. </div>
  201. </Field>
  202. </PanelBody>
  203. </Panel>
  204. </Access>
  205. </Fragment>
  206. )}
  207. </Access>
  208. );
  209. }
  210. }
  211. export default KeySettings;