projectSourceMaps.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. import {Fragment, useCallback, useEffect, useState} 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 QuestionTooltip from 'sentry/components/questionTooltip';
  21. import SearchBar from 'sentry/components/searchBar';
  22. import {Tooltip} from 'sentry/components/tooltip';
  23. import {IconArrow, IconDelete} 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 {Associations} from 'sentry/views/settings/projectSourceMaps/associations';
  35. import {DebugIdBundlesTags} from 'sentry/views/settings/projectSourceMaps/debugIdBundlesTags';
  36. enum SortBy {
  37. ASC_ADDED = 'date_added',
  38. DESC_ADDED = '-date_added',
  39. ASC_MODIFIED = 'date_modified',
  40. DESC_MODIFIED = '-date_modified',
  41. }
  42. enum SourceMapsBundleType {
  43. RELEASE,
  44. DEBUG_ID,
  45. }
  46. function SourceMapsTableRow({
  47. bundleType,
  48. onDelete,
  49. name,
  50. fileCount,
  51. link,
  52. dateModified,
  53. date,
  54. idColumnDetails,
  55. }: {
  56. bundleType: SourceMapsBundleType;
  57. date: string;
  58. fileCount: number;
  59. link: string;
  60. name: string;
  61. onDelete: (name: string) => void;
  62. dateModified?: string;
  63. idColumnDetails?: React.ReactNode;
  64. }) {
  65. const isEmptyReleaseBundle =
  66. bundleType === SourceMapsBundleType.RELEASE && fileCount === -1;
  67. const showDateModified =
  68. bundleType === SourceMapsBundleType.DEBUG_ID && dateModified !== undefined;
  69. return (
  70. <Fragment>
  71. <IDColumn>
  72. {isEmptyReleaseBundle ? name : <Link to={link}>{name}</Link>}
  73. {idColumnDetails}
  74. </IDColumn>
  75. <ArtifactsTotalColumn>
  76. {isEmptyReleaseBundle ? (
  77. <NoArtifactsUploadedWrapper>
  78. <QuestionTooltip
  79. size="xs"
  80. position="top"
  81. title={t('A Release was created, but no artifacts were uploaded')}
  82. />
  83. {'0'}
  84. </NoArtifactsUploadedWrapper>
  85. ) : (
  86. <Count value={fileCount} />
  87. )}
  88. </ArtifactsTotalColumn>
  89. {showDateModified && (
  90. <Column>
  91. <DateTime date={dateModified} timeZone />
  92. </Column>
  93. )}
  94. <Column>
  95. {isEmptyReleaseBundle ? t('(no value)') : <DateTime date={date} timeZone />}
  96. </Column>
  97. <ActionsColumn>
  98. {isEmptyReleaseBundle ? (
  99. <Button
  100. size="sm"
  101. icon={<IconDelete size="sm" />}
  102. title={t('No bundle to delete')}
  103. aria-label={t('No bundle to delete')}
  104. disabled
  105. />
  106. ) : (
  107. <Access access={['project:releases']}>
  108. {({hasAccess}) => (
  109. <Tooltip
  110. disabled={hasAccess}
  111. title={t('You do not have permission to delete artifacts.')}
  112. >
  113. <Confirm
  114. onConfirm={() => onDelete(name)}
  115. message={t(
  116. 'Are you sure you want to remove all artifacts in this archive?'
  117. )}
  118. disabled={!hasAccess}
  119. >
  120. <Button
  121. size="sm"
  122. icon={<IconDelete size="sm" />}
  123. title={t('Remove All Artifacts')}
  124. aria-label={t('Remove All Artifacts')}
  125. disabled={!hasAccess}
  126. />
  127. </Confirm>
  128. </Tooltip>
  129. )}
  130. </Access>
  131. )}
  132. </ActionsColumn>
  133. </Fragment>
  134. );
  135. }
  136. type Props = RouteComponentProps<{orgId: string; projectId: string}, {}> & {
  137. project: Project;
  138. };
  139. export function ProjectSourceMaps({location, router, project}: Props) {
  140. const api = useApi();
  141. const organization = useOrganization();
  142. // endpoints
  143. const sourceMapsEndpoint = `/projects/${organization.slug}/${project.slug}/files/source-maps/`;
  144. const debugIdBundlesEndpoint = `/projects/${organization.slug}/${project.slug}/files/artifact-bundles/`;
  145. // tab urls
  146. const releaseBundlesUrl = normalizeUrl(
  147. `/settings/${organization.slug}/projects/${project.slug}/source-maps/release-bundles/`
  148. );
  149. const debugIdsUrl = normalizeUrl(
  150. `/settings/${organization.slug}/projects/${project.slug}/source-maps/artifact-bundles/`
  151. );
  152. const sourceMapsUrl = normalizeUrl(
  153. `/settings/${organization.slug}/projects/${project.slug}/source-maps/`
  154. );
  155. const tabDebugIdBundlesActive = location.pathname === debugIdsUrl;
  156. // query params
  157. const query = decodeScalar(location.query.query);
  158. const [sortBy, setSortBy] = useState(
  159. location.query.sort ?? tabDebugIdBundlesActive
  160. ? SortBy.DESC_MODIFIED
  161. : SortBy.DESC_ADDED
  162. );
  163. // The default sorting order changes based on the tab.
  164. const cursor = location.query.cursor ?? '';
  165. useEffect(() => {
  166. if (location.pathname === sourceMapsUrl) {
  167. router.replace(debugIdsUrl);
  168. }
  169. }, [location.pathname, sourceMapsUrl, debugIdsUrl, router]);
  170. const {
  171. data: archivesData,
  172. getResponseHeader: archivesHeaders,
  173. isLoading: archivesLoading,
  174. refetch: archivesRefetch,
  175. } = useApiQuery<SourceMapsArchive[]>(
  176. [
  177. sourceMapsEndpoint,
  178. {
  179. query: {query, cursor, sortBy},
  180. },
  181. ],
  182. {
  183. staleTime: 0,
  184. keepPreviousData: true,
  185. enabled: !tabDebugIdBundlesActive,
  186. }
  187. );
  188. const {
  189. data: debugIdBundlesData,
  190. getResponseHeader: debugIdBundlesHeaders,
  191. isLoading: debugIdBundlesLoading,
  192. refetch: debugIdBundlesRefetch,
  193. } = useApiQuery<DebugIdBundle[]>(
  194. [
  195. debugIdBundlesEndpoint,
  196. {
  197. query: {query, cursor, sortBy},
  198. },
  199. ],
  200. {
  201. staleTime: 0,
  202. keepPreviousData: true,
  203. enabled: tabDebugIdBundlesActive,
  204. }
  205. );
  206. const handleSearch = useCallback(
  207. (newQuery: string) => {
  208. router.push({
  209. ...location,
  210. query: {...location.query, cursor: undefined, query: newQuery},
  211. });
  212. },
  213. [router, location]
  214. );
  215. const handleSortChangeForModified = useCallback(() => {
  216. const newSortBy =
  217. sortBy !== SortBy.DESC_MODIFIED ? SortBy.DESC_MODIFIED : SortBy.ASC_MODIFIED;
  218. setSortBy(newSortBy);
  219. router.push({
  220. pathname: location.pathname,
  221. query: {
  222. ...location.query,
  223. cursor: undefined,
  224. sort: newSortBy,
  225. },
  226. });
  227. }, [location, router, sortBy]);
  228. const handleSortChangeForAdded = useCallback(() => {
  229. const newSortBy = sortBy !== SortBy.DESC_ADDED ? SortBy.DESC_ADDED : SortBy.ASC_ADDED;
  230. setSortBy(newSortBy);
  231. router.push({
  232. pathname: location.pathname,
  233. query: {
  234. ...location.query,
  235. cursor: undefined,
  236. sort: newSortBy,
  237. },
  238. });
  239. }, [location, router, sortBy]);
  240. const handleDelete = useCallback(
  241. async (name: string) => {
  242. addLoadingMessage(t('Removing artifacts\u2026'));
  243. try {
  244. await api.requestPromise(
  245. tabDebugIdBundlesActive ? debugIdBundlesEndpoint : sourceMapsEndpoint,
  246. {
  247. method: 'DELETE',
  248. query: tabDebugIdBundlesActive ? {bundleId: name} : {name},
  249. }
  250. );
  251. tabDebugIdBundlesActive ? debugIdBundlesRefetch() : archivesRefetch();
  252. addSuccessMessage(t('Artifacts removed.'));
  253. } catch {
  254. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  255. }
  256. },
  257. [
  258. api,
  259. sourceMapsEndpoint,
  260. tabDebugIdBundlesActive,
  261. debugIdBundlesRefetch,
  262. archivesRefetch,
  263. debugIdBundlesEndpoint,
  264. ]
  265. );
  266. const currentBundleType = tabDebugIdBundlesActive
  267. ? SourceMapsBundleType.DEBUG_ID
  268. : SourceMapsBundleType.RELEASE;
  269. const tableHeaders = [
  270. {
  271. component: tabDebugIdBundlesActive ? t('Bundle ID') : t('Name'),
  272. enabledFor: [SourceMapsBundleType.RELEASE, SourceMapsBundleType.DEBUG_ID],
  273. },
  274. {
  275. component: (
  276. <ArtifactsTotalColumn key="artifacts-total">
  277. {t('Artifacts')}
  278. </ArtifactsTotalColumn>
  279. ),
  280. enabledFor: [SourceMapsBundleType.RELEASE, SourceMapsBundleType.DEBUG_ID],
  281. },
  282. {
  283. component: (
  284. <DateUploadedColumn
  285. key="date-modified"
  286. data-test-id="date-modified-header"
  287. onClick={handleSortChangeForModified}
  288. >
  289. {t('Date Modified')}
  290. {(sortBy === SortBy.ASC_MODIFIED || sortBy === SortBy.DESC_MODIFIED) && (
  291. <Tooltip
  292. containerDisplayMode="inline-flex"
  293. title={
  294. sortBy === SortBy.DESC_MODIFIED
  295. ? t('Switch to ascending order')
  296. : t('Switch to descending order')
  297. }
  298. >
  299. <IconArrow
  300. direction={sortBy === SortBy.DESC_MODIFIED ? 'down' : 'up'}
  301. data-test-id="icon-arrow-modified"
  302. />
  303. </Tooltip>
  304. )}
  305. </DateUploadedColumn>
  306. ),
  307. enabledFor: [SourceMapsBundleType.DEBUG_ID],
  308. },
  309. {
  310. component: (
  311. <DateUploadedColumn
  312. key="date-uploaded"
  313. data-test-id="date-uploaded-header"
  314. onClick={handleSortChangeForAdded}
  315. >
  316. {t('Date Uploaded')}
  317. {(sortBy === SortBy.ASC_ADDED || sortBy === SortBy.DESC_ADDED) && (
  318. <Tooltip
  319. containerDisplayMode="inline-flex"
  320. title={
  321. sortBy === SortBy.DESC_ADDED
  322. ? t('Switch to ascending order')
  323. : t('Switch to descending order')
  324. }
  325. >
  326. <IconArrow
  327. direction={sortBy === SortBy.DESC_ADDED ? 'down' : 'up'}
  328. data-test-id="icon-arrow"
  329. />
  330. </Tooltip>
  331. )}
  332. </DateUploadedColumn>
  333. ),
  334. enabledFor: [SourceMapsBundleType.RELEASE, SourceMapsBundleType.DEBUG_ID],
  335. },
  336. {
  337. component: '',
  338. enabledFor: [SourceMapsBundleType.RELEASE, SourceMapsBundleType.DEBUG_ID],
  339. },
  340. ];
  341. const Table =
  342. currentBundleType === SourceMapsBundleType.DEBUG_ID
  343. ? ArtifactBundlesPanelTable
  344. : ReleaseBundlesPanelTable;
  345. return (
  346. <Fragment>
  347. <SettingsPageHeader title={t('Source Maps')} />
  348. <TextBlock>
  349. {tct(
  350. `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].`,
  351. {
  352. link: (
  353. <ExternalLink href="https://docs.sentry.io/platforms/javascript/sourcemaps/" />
  354. ),
  355. }
  356. )}
  357. </TextBlock>
  358. <NavTabs underlined>
  359. <ListLink to={debugIdsUrl} index isActive={() => tabDebugIdBundlesActive}>
  360. {t('Artifact Bundles')}
  361. </ListLink>
  362. <ListLink to={releaseBundlesUrl} isActive={() => !tabDebugIdBundlesActive}>
  363. {t('Release Bundles')}
  364. </ListLink>
  365. </NavTabs>
  366. <SearchBarWithMarginBottom
  367. placeholder={
  368. tabDebugIdBundlesActive
  369. ? t('Filter by Bundle ID, Debug ID or Release')
  370. : t('Filter by Name')
  371. }
  372. onSearch={handleSearch}
  373. query={query}
  374. />
  375. <Table
  376. headers={tableHeaders
  377. .filter(header => header.enabledFor.includes(currentBundleType))
  378. .map(header => header.component)}
  379. emptyMessage={
  380. query
  381. ? tct('No [tabName] match your search query.', {
  382. tabName: tabDebugIdBundlesActive
  383. ? t('artifact bundles')
  384. : t('release bundles'),
  385. })
  386. : tct('No [tabName] found for this project.', {
  387. tabName: tabDebugIdBundlesActive
  388. ? t('artifact bundles')
  389. : t('release bundles'),
  390. })
  391. }
  392. isEmpty={
  393. (tabDebugIdBundlesActive ? debugIdBundlesData ?? [] : archivesData ?? [])
  394. .length === 0
  395. }
  396. isLoading={tabDebugIdBundlesActive ? debugIdBundlesLoading : archivesLoading}
  397. >
  398. {tabDebugIdBundlesActive
  399. ? debugIdBundlesData?.map(data => (
  400. <SourceMapsTableRow
  401. key={data.bundleId}
  402. bundleType={SourceMapsBundleType.DEBUG_ID}
  403. dateModified={data.dateModified}
  404. date={data.date}
  405. fileCount={data.fileCount}
  406. name={data.bundleId}
  407. onDelete={handleDelete}
  408. link={`/settings/${organization.slug}/projects/${
  409. project.slug
  410. }/source-maps/artifact-bundles/${encodeURIComponent(data.bundleId)}`}
  411. idColumnDetails={
  412. // TODO(Pri): Move the loading to the component once fully transitioned to associations.
  413. !debugIdBundlesLoading &&
  414. (data.associations ? (
  415. <Associations associations={data.associations} />
  416. ) : (
  417. <DebugIdBundlesTags dist={data.dist} release={data.release} />
  418. ))
  419. }
  420. />
  421. ))
  422. : archivesData?.map(data => (
  423. <SourceMapsTableRow
  424. key={data.name}
  425. bundleType={SourceMapsBundleType.RELEASE}
  426. date={data.date}
  427. fileCount={data.fileCount}
  428. name={data.name}
  429. onDelete={handleDelete}
  430. link={`/settings/${organization.slug}/projects/${
  431. project.slug
  432. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  433. />
  434. ))}
  435. </Table>
  436. <Pagination
  437. pageLinks={
  438. tabDebugIdBundlesActive
  439. ? debugIdBundlesHeaders?.('Link') ?? ''
  440. : archivesHeaders?.('Link') ?? ''
  441. }
  442. />
  443. </Fragment>
  444. );
  445. }
  446. const ReleaseBundlesPanelTable = styled(PanelTable)`
  447. grid-template-columns:
  448. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  449. minmax(74px, max-content);
  450. > * {
  451. :nth-child(-n + 4) {
  452. :nth-child(4n-1) {
  453. cursor: pointer;
  454. }
  455. }
  456. }
  457. `;
  458. const ArtifactBundlesPanelTable = styled(PanelTable)`
  459. grid-template-columns:
  460. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content) minmax(
  461. 242px,
  462. max-content
  463. )
  464. minmax(74px, max-content);
  465. > * {
  466. :nth-child(-n + 5) {
  467. :nth-child(5n-1) {
  468. cursor: pointer;
  469. }
  470. }
  471. }
  472. `;
  473. const ArtifactsTotalColumn = styled('div')`
  474. text-align: right;
  475. justify-content: flex-end;
  476. align-items: center;
  477. display: flex;
  478. `;
  479. const DateUploadedColumn = styled('div')`
  480. display: flex;
  481. align-items: center;
  482. gap: ${space(0.5)};
  483. `;
  484. const Column = styled('div')`
  485. display: flex;
  486. align-items: center;
  487. overflow: hidden;
  488. `;
  489. const IDColumn = styled(Column)`
  490. line-height: 140%;
  491. flex-direction: column;
  492. justify-content: center;
  493. align-items: flex-start;
  494. gap: ${space(0.5)};
  495. word-break: break-word;
  496. `;
  497. const ActionsColumn = styled(Column)`
  498. justify-content: flex-end;
  499. `;
  500. const SearchBarWithMarginBottom = styled(SearchBar)`
  501. margin-bottom: ${space(3)};
  502. `;
  503. const NoArtifactsUploadedWrapper = styled('div')`
  504. display: flex;
  505. align-items: center;
  506. gap: ${space(0.5)};
  507. `;