projectSourceMaps.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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/panelTable';
  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
  360. to={{
  361. pathname: debugIdsUrl,
  362. query: location.query,
  363. }}
  364. index
  365. isActive={() => tabDebugIdBundlesActive}
  366. >
  367. {t('Artifact Bundles')}
  368. </ListLink>
  369. <ListLink
  370. to={{
  371. pathname: releaseBundlesUrl,
  372. query: location.query,
  373. }}
  374. isActive={() => !tabDebugIdBundlesActive}
  375. >
  376. {t('Release Bundles')}
  377. </ListLink>
  378. </NavTabs>
  379. <SearchBarWithMarginBottom
  380. placeholder={
  381. tabDebugIdBundlesActive
  382. ? t('Filter by Bundle ID, Debug ID or Release')
  383. : t('Filter by Name')
  384. }
  385. onSearch={handleSearch}
  386. query={query}
  387. />
  388. <Table
  389. headers={tableHeaders
  390. .filter(header => header.enabledFor.includes(currentBundleType))
  391. .map(header => header.component)}
  392. emptyMessage={
  393. query
  394. ? tct('No [tabName] match your search query.', {
  395. tabName: tabDebugIdBundlesActive
  396. ? t('artifact bundles')
  397. : t('release bundles'),
  398. })
  399. : tct('No [tabName] found for this project.', {
  400. tabName: tabDebugIdBundlesActive
  401. ? t('artifact bundles')
  402. : t('release bundles'),
  403. })
  404. }
  405. isEmpty={
  406. (tabDebugIdBundlesActive ? debugIdBundlesData ?? [] : archivesData ?? [])
  407. .length === 0
  408. }
  409. isLoading={tabDebugIdBundlesActive ? debugIdBundlesLoading : archivesLoading}
  410. >
  411. {tabDebugIdBundlesActive
  412. ? debugIdBundlesData?.map(data => (
  413. <SourceMapsTableRow
  414. key={data.bundleId}
  415. bundleType={SourceMapsBundleType.DEBUG_ID}
  416. dateModified={data.dateModified}
  417. date={data.date}
  418. fileCount={data.fileCount}
  419. name={data.bundleId}
  420. onDelete={handleDelete}
  421. link={`/settings/${organization.slug}/projects/${
  422. project.slug
  423. }/source-maps/artifact-bundles/${encodeURIComponent(data.bundleId)}`}
  424. idColumnDetails={
  425. // TODO(Pri): Move the loading to the component once fully transitioned to associations.
  426. !debugIdBundlesLoading &&
  427. (data.associations ? (
  428. <Associations associations={data.associations} />
  429. ) : (
  430. <DebugIdBundlesTags dist={data.dist} release={data.release} />
  431. ))
  432. }
  433. />
  434. ))
  435. : archivesData?.map(data => (
  436. <SourceMapsTableRow
  437. key={data.name}
  438. bundleType={SourceMapsBundleType.RELEASE}
  439. date={data.date}
  440. fileCount={data.fileCount}
  441. name={data.name}
  442. onDelete={handleDelete}
  443. link={`/settings/${organization.slug}/projects/${
  444. project.slug
  445. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  446. />
  447. ))}
  448. </Table>
  449. <Pagination
  450. pageLinks={
  451. tabDebugIdBundlesActive
  452. ? debugIdBundlesHeaders?.('Link') ?? ''
  453. : archivesHeaders?.('Link') ?? ''
  454. }
  455. />
  456. </Fragment>
  457. );
  458. }
  459. const ReleaseBundlesPanelTable = styled(PanelTable)`
  460. grid-template-columns:
  461. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  462. minmax(74px, max-content);
  463. > * {
  464. :nth-child(-n + 4) {
  465. :nth-child(4n-1) {
  466. cursor: pointer;
  467. }
  468. }
  469. }
  470. `;
  471. const ArtifactBundlesPanelTable = styled(PanelTable)`
  472. grid-template-columns:
  473. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content) minmax(
  474. 242px,
  475. max-content
  476. )
  477. minmax(74px, max-content);
  478. > * {
  479. :nth-child(-n + 5) {
  480. :nth-child(5n-1) {
  481. cursor: pointer;
  482. }
  483. }
  484. }
  485. `;
  486. const ArtifactsTotalColumn = styled('div')`
  487. text-align: right;
  488. justify-content: flex-end;
  489. align-items: center;
  490. display: flex;
  491. `;
  492. const DateUploadedColumn = styled('div')`
  493. display: flex;
  494. align-items: center;
  495. gap: ${space(0.5)};
  496. `;
  497. const Column = styled('div')`
  498. display: flex;
  499. align-items: center;
  500. overflow: hidden;
  501. `;
  502. const IDColumn = styled(Column)`
  503. line-height: 140%;
  504. flex-direction: column;
  505. justify-content: center;
  506. align-items: flex-start;
  507. gap: ${space(0.5)};
  508. word-break: break-word;
  509. `;
  510. const ActionsColumn = styled(Column)`
  511. justify-content: flex-end;
  512. `;
  513. const SearchBarWithMarginBottom = styled(SearchBar)`
  514. margin-bottom: ${space(3)};
  515. `;
  516. const NoArtifactsUploadedWrapper = styled('div')`
  517. display: flex;
  518. align-items: center;
  519. gap: ${space(0.5)};
  520. `;