projectSourceMaps.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import {Fragment, useCallback} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {
  5. addErrorMessage,
  6. addLoadingMessage,
  7. addSuccessMessage,
  8. } from 'sentry/actionCreators/indicator';
  9. import Access from 'sentry/components/acl/access';
  10. import {Button} from 'sentry/components/button';
  11. import Confirm from 'sentry/components/confirm';
  12. import Count from 'sentry/components/count';
  13. import DateTime from 'sentry/components/dateTime';
  14. import ExternalLink from 'sentry/components/links/externalLink';
  15. import Link from 'sentry/components/links/link';
  16. import ListLink from 'sentry/components/links/listLink';
  17. import NavTabs from 'sentry/components/navTabs';
  18. import Pagination from 'sentry/components/pagination';
  19. import {PanelTable} from 'sentry/components/panels';
  20. import SearchBar from 'sentry/components/searchBar';
  21. import TextOverflow from 'sentry/components/textOverflow';
  22. import {Tooltip} from 'sentry/components/tooltip';
  23. import Version from 'sentry/components/version';
  24. import {IconArrow, IconDelete} from 'sentry/icons';
  25. import {t, tct} from 'sentry/locale';
  26. import {space} from 'sentry/styles/space';
  27. import {Project} from 'sentry/types';
  28. import {useQuery} from 'sentry/utils/queryClient';
  29. import {decodeScalar} from 'sentry/utils/queryString';
  30. import recreateRoute from 'sentry/utils/recreateRoute';
  31. import useApi from 'sentry/utils/useApi';
  32. import useOrganization from 'sentry/utils/useOrganization';
  33. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  34. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  35. enum SORT_BY {
  36. ASC = 'date_added',
  37. DESC = '-date_added',
  38. }
  39. type Props = RouteComponentProps<{}, {}> & {
  40. project: Project;
  41. };
  42. export function ProjectSourceMaps({routes, params, location, router, project}: Props) {
  43. const api = useApi();
  44. const organization = useOrganization();
  45. const baseUrl = recreateRoute('', {routes, params, stepBack: -1});
  46. const tabDebugIdBundlesActive = location.pathname.endsWith('debug-id-bundles/');
  47. const query = decodeScalar(location.query.query);
  48. const sortBy = location.query.sort ?? SORT_BY.DESC;
  49. const cursor = location.query.cursor ?? '';
  50. const sourceMapsEndpoint = `/projects/${organization.slug}/${project.slug}/files/source-maps/`;
  51. const debugIdBundlesEndpoint = `/projects/${organization.slug}/${project.slug}/files/artifact-bundles/`;
  52. const {
  53. data: archivesData,
  54. isLoading: archivesLoading,
  55. refetch: archivesRefetch,
  56. } = useQuery(
  57. [
  58. sourceMapsEndpoint,
  59. {
  60. query: {query, cursor, orderby: sortBy},
  61. },
  62. ],
  63. () => {
  64. return api.requestPromise(sourceMapsEndpoint, {
  65. query: {query, cursor, orderby: sortBy},
  66. includeAllArgs: true,
  67. });
  68. },
  69. {
  70. staleTime: 0,
  71. keepPreviousData: true,
  72. enabled: !tabDebugIdBundlesActive,
  73. }
  74. );
  75. const {
  76. data: debugIdBundlesData,
  77. isLoading: debugIdBundlesLoading,
  78. refetch: debugIdBundlesRefetch,
  79. } = useQuery(
  80. [
  81. debugIdBundlesEndpoint,
  82. {
  83. query: {query, cursor, orderby: sortBy},
  84. },
  85. ],
  86. () => {
  87. return api.requestPromise(debugIdBundlesEndpoint, {
  88. query: {query, cursor, orderby: sortBy},
  89. includeAllArgs: true,
  90. });
  91. },
  92. {
  93. staleTime: 0,
  94. keepPreviousData: true,
  95. enabled: tabDebugIdBundlesActive,
  96. }
  97. );
  98. const data = tabDebugIdBundlesActive
  99. ? debugIdBundlesData?.[0] ?? []
  100. : archivesData?.[0] ?? [];
  101. const pageLinks = tabDebugIdBundlesActive
  102. ? debugIdBundlesData?.[2]?.getResponseHeader('Link') ?? ''
  103. : archivesData?.[2]?.getResponseHeader('Link') ?? '';
  104. const loading = tabDebugIdBundlesActive ? debugIdBundlesLoading : archivesLoading;
  105. const refetch = tabDebugIdBundlesActive ? debugIdBundlesRefetch : archivesRefetch;
  106. const handleSearch = useCallback(
  107. (newQuery: string) => {
  108. router.push({
  109. ...location,
  110. query: {...location.query, cursor: undefined, query: newQuery},
  111. });
  112. },
  113. [router, location]
  114. );
  115. const handleSortChange = useCallback(() => {
  116. router.push({
  117. pathname: location.pathname,
  118. query: {
  119. ...location.query,
  120. cursor: undefined,
  121. sort: sortBy === SORT_BY.ASC ? SORT_BY.DESC : SORT_BY.ASC,
  122. },
  123. });
  124. }, [location, router, sortBy]);
  125. const handleDelete = useCallback(
  126. async (name: string) => {
  127. addLoadingMessage(t('Removing artifacts\u2026'));
  128. try {
  129. await api.requestPromise(sourceMapsEndpoint, {
  130. method: 'DELETE',
  131. query: {name},
  132. });
  133. refetch();
  134. addSuccessMessage(t('Artifacts removed.'));
  135. } catch {
  136. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  137. }
  138. },
  139. [api, sourceMapsEndpoint, refetch]
  140. );
  141. return (
  142. <Fragment>
  143. <SettingsPageHeader title={t('Source Maps')} />
  144. <TextBlock>
  145. {tct(
  146. `These source map archives help Sentry identify where to look when Javascript is minified. By providing this information, you can get better context for your stack traces when debugging. To learn more about source maps, [link: read the docs].`,
  147. {
  148. link: (
  149. <ExternalLink href="https://docs.sentry.io/platforms/javascript/sourcemaps/" />
  150. ),
  151. }
  152. )}
  153. </TextBlock>
  154. <NavTabs underlined>
  155. <ListLink to={baseUrl} index isActive={() => !tabDebugIdBundlesActive}>
  156. {t('Release Bundles')}
  157. </ListLink>
  158. <ListLink
  159. to={`${baseUrl}debug-id-bundles/`}
  160. index
  161. isActive={() => tabDebugIdBundlesActive}
  162. >
  163. {t('Debug ID Bundles')}
  164. </ListLink>
  165. </NavTabs>
  166. <SearchBarWithMarginBottom
  167. placeholder={t('Search')}
  168. onSearch={handleSearch}
  169. query={query}
  170. />
  171. <StyledPanelTable
  172. headers={[
  173. t('Bundle'),
  174. <ArtifactsColumn key="artifacts">{t('Artifacts')}</ArtifactsColumn>,
  175. <DateUploadedColumn key="date-uploaded" onClick={handleSortChange}>
  176. {t('Date Uploaded')}
  177. <Tooltip
  178. containerDisplayMode="inline-flex"
  179. title={
  180. sortBy === SORT_BY.DESC
  181. ? t('Switch to ascending order')
  182. : t('Switch to descending order')
  183. }
  184. >
  185. <IconArrow direction={sortBy === SORT_BY.DESC ? 'down' : 'up'} />
  186. </Tooltip>
  187. </DateUploadedColumn>,
  188. '',
  189. ]}
  190. emptyMessage={
  191. query
  192. ? tct('No [tabName] match your search query.', {
  193. tabName: tabDebugIdBundlesActive
  194. ? t('debug ID bundles')
  195. : t('release bundles'),
  196. })
  197. : tct('No [tabName] found for this project.', {
  198. tabName: tabDebugIdBundlesActive
  199. ? t('debug ID bundles')
  200. : t('release bundles'),
  201. })
  202. }
  203. isEmpty={data.length === 0}
  204. isLoading={loading}
  205. >
  206. {data.map(({name, date, fileCount}) => {
  207. return (
  208. <Fragment key={name}>
  209. <Column>
  210. <TextOverflow>
  211. <Link
  212. to={`/settings/${organization.slug}/projects/${
  213. project.slug
  214. }/source-maps/${encodeURIComponent(name)}`}
  215. >
  216. <Version version={name} anchor={false} tooltipRawVersion truncate />
  217. </Link>
  218. </TextOverflow>
  219. </Column>
  220. <ArtifactsColumn>
  221. <Count value={fileCount} />
  222. </ArtifactsColumn>
  223. <Column>
  224. <DateTime date={date} timeZone />
  225. </Column>
  226. <ActionsColumn>
  227. <Access access={['project:releases']}>
  228. {({hasAccess}) => (
  229. <Tooltip
  230. disabled={hasAccess}
  231. title={t('You do not have permission to delete artifacts.')}
  232. >
  233. <Confirm
  234. onConfirm={() => handleDelete(name)}
  235. message={t(
  236. 'Are you sure you want to remove all artifacts in this archive?'
  237. )}
  238. disabled={!hasAccess}
  239. >
  240. <Button
  241. size="sm"
  242. icon={<IconDelete size="sm" />}
  243. title={t('Remove All Artifacts')}
  244. aria-label={t('Remove All Artifacts')}
  245. disabled={!hasAccess}
  246. />
  247. </Confirm>
  248. </Tooltip>
  249. )}
  250. </Access>
  251. </ActionsColumn>
  252. </Fragment>
  253. );
  254. })}
  255. </StyledPanelTable>
  256. <Pagination pageLinks={pageLinks} />
  257. </Fragment>
  258. );
  259. }
  260. const StyledPanelTable = styled(PanelTable)`
  261. grid-template-columns:
  262. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  263. minmax(74px, max-content);
  264. > * {
  265. :nth-child(-n + 4) {
  266. :nth-child(4n-1) {
  267. cursor: pointer;
  268. }
  269. }
  270. }
  271. `;
  272. const SearchBarWithMarginBottom = styled(SearchBar)`
  273. margin-bottom: ${space(3)};
  274. `;
  275. const ArtifactsColumn = styled('div')`
  276. text-align: right;
  277. justify-content: flex-end;
  278. `;
  279. const DateUploadedColumn = styled('div')`
  280. display: flex;
  281. align-items: center;
  282. gap: ${space(0.5)};
  283. `;
  284. const Column = styled('div')`
  285. display: flex;
  286. align-items: center;
  287. overflow: hidden;
  288. `;
  289. const ActionsColumn = styled(Column)`
  290. justify-content: flex-end;
  291. `;