index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import Access from 'sentry/components/acl/access';
  5. import {Button} from 'sentry/components/button';
  6. import ExternalLink from 'sentry/components/links/externalLink';
  7. import LoadingError from 'sentry/components/loadingError';
  8. import {PanelTable} from 'sentry/components/panels/panelTable';
  9. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  10. import {t, tct} from 'sentry/locale';
  11. import type {Organization, OrgAuthToken, Project} from 'sentry/types';
  12. import {handleXhrErrorResponse} from 'sentry/utils/handleXhrErrorResponse';
  13. import {
  14. setApiQueryData,
  15. useApiQuery,
  16. useMutation,
  17. useQueryClient,
  18. } from 'sentry/utils/queryClient';
  19. import type RequestError from 'sentry/utils/requestError/requestError';
  20. import useApi from 'sentry/utils/useApi';
  21. import withOrganization from 'sentry/utils/withOrganization';
  22. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  23. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  24. import {OrganizationAuthTokensAuthTokenRow} from 'sentry/views/settings/organizationAuthTokens/authTokenRow';
  25. type FetchOrgAuthTokensResponse = OrgAuthToken[];
  26. type FetchOrgAuthTokensParameters = {
  27. orgSlug: string;
  28. };
  29. type RevokeTokenQueryVariables = {
  30. token: OrgAuthToken;
  31. };
  32. export const makeFetchOrgAuthTokensForOrgQueryKey = ({
  33. orgSlug,
  34. }: FetchOrgAuthTokensParameters) =>
  35. [`/organizations/${orgSlug}/org-auth-tokens/`] as const;
  36. function TokenList({
  37. organization,
  38. tokenList,
  39. isRevoking,
  40. revokeToken,
  41. }: {
  42. isRevoking: boolean;
  43. organization: Organization;
  44. tokenList: OrgAuthToken[];
  45. revokeToken?: (data: {token: OrgAuthToken}) => void;
  46. }) {
  47. const apiEndpoint = `/organizations/${organization.slug}/projects/`;
  48. const projectIds = tokenList
  49. .map(token => token.projectLastUsedId)
  50. .filter(id => !!id) as string[];
  51. const idQueryParams = projectIds.map(id => `id:${id}`).join(' ');
  52. const hasProjects = projectIds.length > 0;
  53. const {data: projects, isLoading: isLoadingProjects} = useApiQuery<Project[]>(
  54. [apiEndpoint, {query: {query: idQueryParams}}],
  55. {
  56. staleTime: 0,
  57. enabled: hasProjects,
  58. }
  59. );
  60. return (
  61. <Fragment>
  62. {tokenList.map(token => {
  63. const projectLastUsed = token.projectLastUsedId
  64. ? projects?.find(p => p.id === token.projectLastUsedId)
  65. : undefined;
  66. return (
  67. <OrganizationAuthTokensAuthTokenRow
  68. key={token.id}
  69. organization={organization}
  70. token={token}
  71. isRevoking={isRevoking}
  72. revokeToken={revokeToken ? () => revokeToken({token}) : undefined}
  73. projectLastUsed={projectLastUsed}
  74. isProjectLoading={hasProjects && isLoadingProjects}
  75. />
  76. );
  77. })}
  78. </Fragment>
  79. );
  80. }
  81. export function OrganizationAuthTokensIndex({
  82. organization,
  83. }: {
  84. organization: Organization;
  85. }) {
  86. const api = useApi();
  87. const queryClient = useQueryClient();
  88. const {
  89. isLoading,
  90. isError,
  91. data: tokenList,
  92. refetch: refetchTokenList,
  93. } = useApiQuery<FetchOrgAuthTokensResponse>(
  94. makeFetchOrgAuthTokensForOrgQueryKey({orgSlug: organization.slug}),
  95. {
  96. staleTime: Infinity,
  97. }
  98. );
  99. const {mutate: handleRevokeToken, isLoading: isRevoking} = useMutation<
  100. {},
  101. RequestError,
  102. RevokeTokenQueryVariables
  103. >({
  104. mutationFn: ({token}) =>
  105. api.requestPromise(
  106. `/organizations/${organization.slug}/org-auth-tokens/${token.id}/`,
  107. {
  108. method: 'DELETE',
  109. }
  110. ),
  111. onSuccess: (_data, {token}) => {
  112. addSuccessMessage(t('Revoked auth token for the organization.'));
  113. setApiQueryData(
  114. queryClient,
  115. makeFetchOrgAuthTokensForOrgQueryKey({orgSlug: organization.slug}),
  116. oldData => {
  117. if (!Array.isArray(oldData)) {
  118. return oldData;
  119. }
  120. return oldData.filter(oldToken => oldToken.id !== token.id);
  121. }
  122. );
  123. },
  124. onError: error => {
  125. const message = t('Failed to revoke the auth token for the organization.');
  126. handleXhrErrorResponse(message, error);
  127. addErrorMessage(message);
  128. },
  129. });
  130. const createNewToken = (
  131. <Button
  132. priority="primary"
  133. size="sm"
  134. to={`/settings/${organization.slug}/auth-tokens/new-token/`}
  135. data-test-id="create-token"
  136. >
  137. {t('Create New Token')}
  138. </Button>
  139. );
  140. return (
  141. <Access access={['org:write']}>
  142. {({hasAccess}) => (
  143. <Fragment>
  144. <SentryDocumentTitle title={t('Auth Tokens')} />
  145. <SettingsPageHeader title={t('Auth Tokens')} action={createNewToken} />
  146. <TextBlock>
  147. {t(
  148. 'Organization Auth Tokens can be used in many places to interact with Sentry programatically. For example, they can be used for sentry-cli, bundler plugins or similar uses cases.'
  149. )}
  150. </TextBlock>
  151. <TextBlock>
  152. {tct(
  153. 'For more information on how to use the web API, see our [link:documentation].',
  154. {
  155. link: <ExternalLink href="https://docs.sentry.io/api/" />,
  156. }
  157. )}
  158. </TextBlock>
  159. <ResponsivePanelTable
  160. isLoading={isLoading || isError}
  161. isEmpty={!isLoading && !tokenList?.length}
  162. loader={
  163. isError ? (
  164. <LoadingError
  165. message={t('Failed to load auth tokens for the organization.')}
  166. onRetry={refetchTokenList}
  167. />
  168. ) : undefined
  169. }
  170. emptyMessage={t("You haven't created any authentication tokens yet.")}
  171. headers={[t('Auth token'), t('Created'), t('Last access'), '']}
  172. >
  173. {!isError && !isLoading && !!tokenList?.length && (
  174. <TokenList
  175. organization={organization}
  176. tokenList={tokenList}
  177. isRevoking={isRevoking}
  178. revokeToken={hasAccess ? handleRevokeToken : undefined}
  179. />
  180. )}
  181. </ResponsivePanelTable>
  182. </Fragment>
  183. )}
  184. </Access>
  185. );
  186. }
  187. export function tokenPreview(tokenLastCharacters: string, tokenPrefix = '') {
  188. return `${tokenPrefix}************${tokenLastCharacters}`;
  189. }
  190. export default withOrganization(OrganizationAuthTokensIndex);
  191. const ResponsivePanelTable = styled(PanelTable)`
  192. @media (max-width: ${p => p.theme.breakpoints.small}) {
  193. grid-template-columns: 1fr 1fr;
  194. > *:nth-child(4n + 2),
  195. *:nth-child(4n + 3) {
  196. display: none;
  197. }
  198. }
  199. `;