projectSourceMaps.tsx 15 KB

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