index.tsx 6.7 KB

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