projectSourceMaps.tsx 13 KB

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