projectSourceMaps.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. isPending: 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. isPending: 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. // TODO(__SENTRY_USING_REACT_ROUTER_SIX): We can remove this later, react
  341. // router 6 handles empty query objects without appending a trailing ?
  342. const linkLocation = {
  343. ...(location.query && Object.keys(location.query).length > 0
  344. ? {query: location.query}
  345. : {}),
  346. };
  347. return (
  348. <Fragment>
  349. <SettingsPageHeader title={t('Source Maps')} />
  350. <TextBlock>
  351. {tct(
  352. `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].`,
  353. {
  354. link: (
  355. <ExternalLink href="https://docs.sentry.io/platforms/javascript/sourcemaps/" />
  356. ),
  357. }
  358. )}
  359. </TextBlock>
  360. <NavTabs underlined>
  361. <ListLink
  362. to={{
  363. pathname: debugIdsUrl,
  364. ...linkLocation,
  365. }}
  366. index
  367. isActive={() => tabDebugIdBundlesActive}
  368. >
  369. {t('Artifact Bundles')}
  370. </ListLink>
  371. <ListLink
  372. to={{
  373. pathname: releaseBundlesUrl,
  374. ...linkLocation,
  375. }}
  376. isActive={() => !tabDebugIdBundlesActive}
  377. >
  378. {t('Release Bundles')}
  379. </ListLink>
  380. </NavTabs>
  381. <SearchBarWithMarginBottom
  382. placeholder={
  383. tabDebugIdBundlesActive
  384. ? t('Filter by Bundle ID, Debug ID or Release')
  385. : t('Filter by Name')
  386. }
  387. onSearch={handleSearch}
  388. query={query}
  389. />
  390. {tabDebugIdBundlesActive ? (
  391. <DebugIdBundleList
  392. isLoading={debugIdBundlesLoading}
  393. debugIdBundles={debugIdBundlesData}
  394. project={project}
  395. onDelete={bundleId =>
  396. deleteDebugIdBundle({bundleId, projectSlug: project.slug})
  397. }
  398. emptyMessage={
  399. query
  400. ? t('No artifact bundles match your search query.')
  401. : t('No artifact bundles found for this project.')
  402. }
  403. />
  404. ) : (
  405. <Table
  406. headers={tableHeaders
  407. .filter(header => header.enabledFor.includes(currentBundleType))
  408. .map(header => header.component)}
  409. emptyMessage={
  410. query
  411. ? t('No release bundles match your search query.')
  412. : t('No release bundles found for this project.')
  413. }
  414. isEmpty={(archivesData ?? []).length === 0}
  415. isLoading={archivesLoading}
  416. >
  417. {archivesData?.map(data => (
  418. <SourceMapsTableRow
  419. key={data.name}
  420. bundleType={SourceMapsBundleType.RELEASE}
  421. date={data.date}
  422. fileCount={data.fileCount}
  423. name={data.name}
  424. onDelete={handleDeleteReleaseArtifacts}
  425. link={`/settings/${organization.slug}/projects/${
  426. project.slug
  427. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  428. />
  429. ))}
  430. </Table>
  431. )}
  432. <Pagination
  433. pageLinks={
  434. tabDebugIdBundlesActive
  435. ? debugIdBundlesHeaders?.('Link') ?? ''
  436. : archivesHeaders?.('Link') ?? ''
  437. }
  438. />
  439. </Fragment>
  440. );
  441. }
  442. const ReleaseBundlesPanelTable = styled(PanelTable)`
  443. grid-template-columns:
  444. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  445. minmax(74px, max-content);
  446. > * {
  447. :nth-child(-n + 4) {
  448. :nth-child(4n-1) {
  449. cursor: pointer;
  450. }
  451. }
  452. }
  453. `;
  454. const ArtifactBundlesPanelTable = styled(PanelTable)`
  455. grid-template-columns:
  456. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content) minmax(
  457. 242px,
  458. max-content
  459. )
  460. minmax(74px, max-content);
  461. > * {
  462. :nth-child(-n + 5) {
  463. :nth-child(5n-1) {
  464. cursor: pointer;
  465. }
  466. }
  467. }
  468. `;
  469. const ArtifactsTotalColumn = styled('div')`
  470. text-align: right;
  471. justify-content: flex-end;
  472. align-items: center;
  473. display: flex;
  474. `;
  475. const DateUploadedColumn = styled('div')`
  476. display: flex;
  477. align-items: center;
  478. gap: ${space(0.5)};
  479. `;
  480. const Column = styled('div')`
  481. display: flex;
  482. align-items: center;
  483. overflow: hidden;
  484. `;
  485. const IDColumn = styled(Column)`
  486. line-height: 140%;
  487. flex-direction: column;
  488. justify-content: center;
  489. align-items: flex-start;
  490. gap: ${space(0.5)};
  491. word-break: break-word;
  492. `;
  493. const ActionsColumn = styled(Column)`
  494. justify-content: flex-end;
  495. `;
  496. const SearchBarWithMarginBottom = styled(SearchBar)`
  497. margin-bottom: ${space(3)};
  498. `;
  499. const NoArtifactsUploadedWrapper = styled('div')`
  500. display: flex;
  501. align-items: center;
  502. gap: ${space(0.5)};
  503. `;