projectSourceMaps.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 Tag from 'sentry/components/tag';
  22. import TextOverflow from 'sentry/components/textOverflow';
  23. import {Tooltip} from 'sentry/components/tooltip';
  24. import Version from 'sentry/components/version';
  25. import {IconArrow, IconDelete} from 'sentry/icons';
  26. import {t, tct} from 'sentry/locale';
  27. import {space} from 'sentry/styles/space';
  28. import {DebugIdBundle, Project, SourceMapsArchive} from 'sentry/types';
  29. import {useQuery} from 'sentry/utils/queryClient';
  30. import {decodeScalar} from 'sentry/utils/queryString';
  31. import useApi from 'sentry/utils/useApi';
  32. import useOrganization from 'sentry/utils/useOrganization';
  33. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  34. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  35. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  36. enum SORT_BY {
  37. ASC = 'date_added',
  38. DESC = '-date_added',
  39. }
  40. function SourceMapsTableRow({
  41. onDelete,
  42. name,
  43. fileCount,
  44. link,
  45. date,
  46. idColumnDetails,
  47. }: {
  48. date: string;
  49. fileCount: number;
  50. link: string;
  51. name: string;
  52. onDelete: (name: string) => void;
  53. idColumnDetails?: React.ReactNode;
  54. }) {
  55. return (
  56. <Fragment>
  57. <IDColumn>
  58. <TextOverflow>
  59. <Link to={link}>
  60. <Version version={name} anchor={false} tooltipRawVersion truncate={false} />
  61. </Link>
  62. </TextOverflow>
  63. {idColumnDetails}
  64. </IDColumn>
  65. <ArtifactsTotalColumn>
  66. <Count value={fileCount} />
  67. </ArtifactsTotalColumn>
  68. <Column>
  69. <DateTime date={date} timeZone />
  70. </Column>
  71. <ActionsColumn>
  72. <Access access={['project:releases']}>
  73. {({hasAccess}) => (
  74. <Tooltip
  75. disabled={hasAccess}
  76. title={t('You do not have permission to delete artifacts.')}
  77. >
  78. <Confirm
  79. onConfirm={() => onDelete(name)}
  80. message={t(
  81. 'Are you sure you want to remove all artifacts in this archive?'
  82. )}
  83. disabled={!hasAccess}
  84. >
  85. <Button
  86. size="sm"
  87. icon={<IconDelete size="sm" />}
  88. title={t('Remove All Artifacts')}
  89. aria-label={t('Remove All Artifacts')}
  90. disabled={!hasAccess}
  91. />
  92. </Confirm>
  93. </Tooltip>
  94. )}
  95. </Access>
  96. </ActionsColumn>
  97. </Fragment>
  98. );
  99. }
  100. type Props = RouteComponentProps<{orgId: string; projectId: string}, {}> & {
  101. project: Project;
  102. };
  103. export function ProjectSourceMaps({location, router, project}: Props) {
  104. const api = useApi();
  105. const organization = useOrganization();
  106. // query params
  107. const query = decodeScalar(location.query.query);
  108. const sortBy = location.query.sort ?? SORT_BY.DESC;
  109. const cursor = location.query.cursor ?? '';
  110. // endpoints
  111. const sourceMapsEndpoint = `/projects/${organization.slug}/${project.slug}/files/source-maps/`;
  112. const debugIdBundlesEndpoint = `/projects/${organization.slug}/${project.slug}/files/artifact-bundles/`;
  113. // tab urls
  114. const releaseBundlesUrl = normalizeUrl(
  115. `/settings/${organization.slug}/projects/${project.slug}/source-maps/release-bundles/`
  116. );
  117. const debugIdsUrl = normalizeUrl(
  118. `/settings/${organization.slug}/projects/${project.slug}/source-maps/debug-id-bundles/`
  119. );
  120. const tabDebugIdBundlesActive = location.pathname === debugIdsUrl;
  121. const {
  122. data: archivesData,
  123. isLoading: archivesLoading,
  124. refetch: archivesRefetch,
  125. } = useQuery<[SourceMapsArchive[], any, any]>(
  126. [
  127. sourceMapsEndpoint,
  128. {
  129. query: {query, cursor, sortBy},
  130. },
  131. ],
  132. () => {
  133. return api.requestPromise(sourceMapsEndpoint, {
  134. query: {query, cursor, sortBy},
  135. includeAllArgs: true,
  136. });
  137. },
  138. {
  139. staleTime: 0,
  140. keepPreviousData: true,
  141. enabled: !tabDebugIdBundlesActive,
  142. }
  143. );
  144. const {
  145. data: debugIdBundlesData,
  146. isLoading: debugIdBundlesLoading,
  147. refetch: debugIdBundlesRefetch,
  148. } = useQuery<[DebugIdBundle[], any, any]>(
  149. [
  150. debugIdBundlesEndpoint,
  151. {
  152. query: {query, cursor, sortBy},
  153. },
  154. ],
  155. () => {
  156. return api.requestPromise(debugIdBundlesEndpoint, {
  157. query: {query, cursor, sortBy},
  158. includeAllArgs: true,
  159. });
  160. },
  161. {
  162. staleTime: 0,
  163. keepPreviousData: true,
  164. enabled: tabDebugIdBundlesActive,
  165. }
  166. );
  167. const handleSearch = useCallback(
  168. (newQuery: string) => {
  169. router.push({
  170. ...location,
  171. query: {...location.query, cursor: undefined, query: newQuery},
  172. });
  173. },
  174. [router, location]
  175. );
  176. const handleSortChange = useCallback(() => {
  177. router.push({
  178. pathname: location.pathname,
  179. query: {
  180. ...location.query,
  181. cursor: undefined,
  182. sort: sortBy === SORT_BY.ASC ? SORT_BY.DESC : SORT_BY.ASC,
  183. },
  184. });
  185. }, [location, router, sortBy]);
  186. const handleDelete = useCallback(
  187. async (name: string) => {
  188. addLoadingMessage(t('Removing artifacts\u2026'));
  189. try {
  190. await api.requestPromise(sourceMapsEndpoint, {
  191. method: 'DELETE',
  192. query: {name},
  193. });
  194. tabDebugIdBundlesActive ? debugIdBundlesRefetch() : archivesRefetch();
  195. addSuccessMessage(t('Artifacts removed.'));
  196. } catch {
  197. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  198. }
  199. },
  200. [
  201. api,
  202. sourceMapsEndpoint,
  203. tabDebugIdBundlesActive,
  204. debugIdBundlesRefetch,
  205. archivesRefetch,
  206. ]
  207. );
  208. return (
  209. <Fragment>
  210. <SettingsPageHeader title={t('Source Maps')} />
  211. <TextBlock>
  212. {tct(
  213. `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].`,
  214. {
  215. link: (
  216. <ExternalLink href="https://docs.sentry.io/platforms/javascript/sourcemaps/" />
  217. ),
  218. }
  219. )}
  220. </TextBlock>
  221. <NavTabs underlined>
  222. <ListLink to={releaseBundlesUrl} index isActive={() => !tabDebugIdBundlesActive}>
  223. {t('Release Bundles')}
  224. </ListLink>
  225. <ListLink to={debugIdsUrl} isActive={() => tabDebugIdBundlesActive}>
  226. {t('Debug ID Bundles')}
  227. </ListLink>
  228. </NavTabs>
  229. <SearchBarWithMarginBottom
  230. placeholder={
  231. tabDebugIdBundlesActive ? t('Filter by Bundle ID') : t('Filter by Name')
  232. }
  233. onSearch={handleSearch}
  234. query={query}
  235. />
  236. <StyledPanelTable
  237. headers={[
  238. tabDebugIdBundlesActive ? t('Bundle ID') : t('Name'),
  239. <ArtifactsTotalColumn key="artifacts-total">
  240. {t('Artifacts')}
  241. </ArtifactsTotalColumn>,
  242. <DateUploadedColumn key="date-uploaded" onClick={handleSortChange}>
  243. {t('Date Uploaded')}
  244. <Tooltip
  245. containerDisplayMode="inline-flex"
  246. title={
  247. sortBy === SORT_BY.DESC
  248. ? t('Switch to ascending order')
  249. : t('Switch to descending order')
  250. }
  251. >
  252. <IconArrow
  253. direction={sortBy === SORT_BY.DESC ? 'down' : 'up'}
  254. data-test-id="icon-arrow"
  255. />
  256. </Tooltip>
  257. </DateUploadedColumn>,
  258. '',
  259. ]}
  260. emptyMessage={
  261. query
  262. ? tct('No [tabName] match your search query.', {
  263. tabName: tabDebugIdBundlesActive
  264. ? t('debug ID bundles')
  265. : t('release bundles'),
  266. })
  267. : tct('No [tabName] found for this project.', {
  268. tabName: tabDebugIdBundlesActive
  269. ? t('debug ID bundles')
  270. : t('release bundles'),
  271. })
  272. }
  273. isEmpty={
  274. (tabDebugIdBundlesActive
  275. ? debugIdBundlesData?.[0] ?? []
  276. : archivesData?.[0] ?? []
  277. ).length === 0
  278. }
  279. isLoading={tabDebugIdBundlesActive ? debugIdBundlesLoading : archivesLoading}
  280. >
  281. {tabDebugIdBundlesActive
  282. ? debugIdBundlesData?.[0].map(data => (
  283. <SourceMapsTableRow
  284. key={data.bundleId}
  285. date={data.date}
  286. fileCount={data.fileCount}
  287. name={data.bundleId}
  288. onDelete={handleDelete}
  289. link={`/settings/${organization.slug}/projects/${
  290. project.slug
  291. }/source-maps/debug-id-bundles/${encodeURIComponent(data.bundleId)}`}
  292. idColumnDetails={
  293. <Tags>
  294. {data.dist && (
  295. <Tag
  296. tooltipText={tct('Associated with release "[distribution]"', {
  297. distribution: data.dist,
  298. })}
  299. type="info"
  300. >
  301. {data.dist}
  302. </Tag>
  303. )}
  304. {data.release && (
  305. <Tag
  306. tooltipText={tct('Associated with release "[releaseName]"', {
  307. releaseName: data.release,
  308. })}
  309. type="info"
  310. >
  311. {data.release}
  312. </Tag>
  313. )}
  314. {!data.dist && !data.release && (
  315. <Tag
  316. tooltipText={t('Not associated with a release or distribution')}
  317. >
  318. {t('none')}
  319. </Tag>
  320. )}
  321. </Tags>
  322. }
  323. />
  324. ))
  325. : archivesData?.[0].map(data => (
  326. <SourceMapsTableRow
  327. key={data.name}
  328. date={data.date}
  329. fileCount={data.fileCount}
  330. name={data.name}
  331. onDelete={handleDelete}
  332. link={`/settings/${organization.slug}/projects/${
  333. project.slug
  334. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  335. />
  336. ))}
  337. </StyledPanelTable>
  338. <Pagination
  339. pageLinks={
  340. tabDebugIdBundlesActive
  341. ? debugIdBundlesData?.[2]?.getResponseHeader('Link') ?? ''
  342. : archivesData?.[2]?.getResponseHeader('Link') ?? ''
  343. }
  344. />
  345. </Fragment>
  346. );
  347. }
  348. const StyledPanelTable = styled(PanelTable)`
  349. grid-template-columns:
  350. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  351. minmax(74px, max-content);
  352. > * {
  353. :nth-child(-n + 4) {
  354. :nth-child(4n-1) {
  355. cursor: pointer;
  356. }
  357. }
  358. }
  359. `;
  360. const ArtifactsTotalColumn = styled('div')`
  361. text-align: right;
  362. justify-content: flex-end;
  363. align-items: center;
  364. display: flex;
  365. `;
  366. const DateUploadedColumn = styled('div')`
  367. display: flex;
  368. align-items: center;
  369. gap: ${space(0.5)};
  370. `;
  371. const Column = styled('div')`
  372. display: flex;
  373. align-items: center;
  374. overflow: hidden;
  375. `;
  376. const IDColumn = styled(Column)`
  377. line-height: 140%;
  378. flex-direction: column;
  379. justify-content: center;
  380. align-items: flex-start;
  381. gap: ${space(0.5)};
  382. `;
  383. const ActionsColumn = styled(Column)`
  384. justify-content: flex-end;
  385. `;
  386. const Tags = styled('div')`
  387. display: flex;
  388. flex-wrap: wrap;
  389. gap: ${space(0.5)};
  390. `;
  391. const SearchBarWithMarginBottom = styled(SearchBar)`
  392. margin-bottom: ${space(3)};
  393. `;