projectSourceMaps.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 {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 {DebugIdBundleList} from 'sentry/views/settings/projectSourceMaps/debugIdBundleList';
  35. import {useDeleteDebugIdBundle} from 'sentry/views/settings/projectSourceMaps/useDeleteDebugIdBundle';
  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: SortBy.DESC_MODIFIED},
  198. },
  199. ],
  200. {
  201. staleTime: 0,
  202. keepPreviousData: true,
  203. enabled: tabDebugIdBundlesActive,
  204. }
  205. );
  206. const {mutate: deleteDebugIdBundle} = useDeleteDebugIdBundle({
  207. onSuccess: () => debugIdBundlesRefetch(),
  208. });
  209. const handleSearch = useCallback(
  210. (newQuery: string) => {
  211. router.push({
  212. ...location,
  213. query: {...location.query, cursor: undefined, query: newQuery},
  214. });
  215. },
  216. [router, location]
  217. );
  218. const handleSortChangeForModified = useCallback(() => {
  219. const newSortBy =
  220. sortBy !== SortBy.DESC_MODIFIED ? SortBy.DESC_MODIFIED : SortBy.ASC_MODIFIED;
  221. setSortBy(newSortBy);
  222. router.push({
  223. pathname: location.pathname,
  224. query: {
  225. ...location.query,
  226. cursor: undefined,
  227. sort: newSortBy,
  228. },
  229. });
  230. }, [location, router, sortBy]);
  231. const handleSortChangeForAdded = useCallback(() => {
  232. const newSortBy = sortBy !== SortBy.DESC_ADDED ? SortBy.DESC_ADDED : SortBy.ASC_ADDED;
  233. setSortBy(newSortBy);
  234. router.push({
  235. pathname: location.pathname,
  236. query: {
  237. ...location.query,
  238. cursor: undefined,
  239. sort: newSortBy,
  240. },
  241. });
  242. }, [location, router, sortBy]);
  243. const handleDeleteReleaseArtifacts = useCallback(
  244. async (name: string) => {
  245. addLoadingMessage(t('Removing artifacts\u2026'));
  246. try {
  247. await api.requestPromise(sourceMapsEndpoint, {
  248. method: 'DELETE',
  249. query: {name},
  250. });
  251. archivesRefetch();
  252. addSuccessMessage(t('Artifacts removed.'));
  253. } catch {
  254. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  255. }
  256. },
  257. [api, sourceMapsEndpoint, archivesRefetch]
  258. );
  259. const currentBundleType = tabDebugIdBundlesActive
  260. ? SourceMapsBundleType.DEBUG_ID
  261. : SourceMapsBundleType.RELEASE;
  262. const tableHeaders = [
  263. {
  264. component: tabDebugIdBundlesActive ? t('Bundle ID') : t('Name'),
  265. enabledFor: [SourceMapsBundleType.RELEASE, SourceMapsBundleType.DEBUG_ID],
  266. },
  267. {
  268. component: (
  269. <ArtifactsTotalColumn key="artifacts-total">
  270. {t('Artifacts')}
  271. </ArtifactsTotalColumn>
  272. ),
  273. enabledFor: [SourceMapsBundleType.RELEASE, SourceMapsBundleType.DEBUG_ID],
  274. },
  275. {
  276. component: (
  277. <DateUploadedColumn
  278. key="date-modified"
  279. data-test-id="date-modified-header"
  280. onClick={handleSortChangeForModified}
  281. >
  282. {t('Date Modified')}
  283. {(sortBy === SortBy.ASC_MODIFIED || sortBy === SortBy.DESC_MODIFIED) && (
  284. <Tooltip
  285. containerDisplayMode="inline-flex"
  286. title={
  287. sortBy === SortBy.DESC_MODIFIED
  288. ? t('Switch to ascending order')
  289. : t('Switch to descending order')
  290. }
  291. >
  292. <IconArrow
  293. direction={sortBy === SortBy.DESC_MODIFIED ? 'down' : 'up'}
  294. data-test-id="icon-arrow-modified"
  295. />
  296. </Tooltip>
  297. )}
  298. </DateUploadedColumn>
  299. ),
  300. enabledFor: [SourceMapsBundleType.DEBUG_ID],
  301. },
  302. {
  303. component: (
  304. <DateUploadedColumn
  305. key="date-uploaded"
  306. data-test-id="date-uploaded-header"
  307. onClick={handleSortChangeForAdded}
  308. >
  309. {t('Date Uploaded')}
  310. {(sortBy === SortBy.ASC_ADDED || sortBy === SortBy.DESC_ADDED) && (
  311. <Tooltip
  312. containerDisplayMode="inline-flex"
  313. title={
  314. sortBy === SortBy.DESC_ADDED
  315. ? t('Switch to ascending order')
  316. : t('Switch to descending order')
  317. }
  318. >
  319. <IconArrow
  320. direction={sortBy === SortBy.DESC_ADDED ? 'down' : 'up'}
  321. data-test-id="icon-arrow"
  322. />
  323. </Tooltip>
  324. )}
  325. </DateUploadedColumn>
  326. ),
  327. enabledFor: [SourceMapsBundleType.RELEASE, SourceMapsBundleType.DEBUG_ID],
  328. },
  329. {
  330. component: '',
  331. enabledFor: [SourceMapsBundleType.RELEASE, SourceMapsBundleType.DEBUG_ID],
  332. },
  333. ];
  334. const Table =
  335. currentBundleType === SourceMapsBundleType.DEBUG_ID
  336. ? ArtifactBundlesPanelTable
  337. : ReleaseBundlesPanelTable;
  338. return (
  339. <Fragment>
  340. <SettingsPageHeader title={t('Source Maps')} />
  341. <TextBlock>
  342. {tct(
  343. `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].`,
  344. {
  345. link: (
  346. <ExternalLink href="https://docs.sentry.io/platforms/javascript/sourcemaps/" />
  347. ),
  348. }
  349. )}
  350. </TextBlock>
  351. <NavTabs underlined>
  352. <ListLink
  353. to={{
  354. pathname: debugIdsUrl,
  355. query: location.query,
  356. }}
  357. index
  358. isActive={() => tabDebugIdBundlesActive}
  359. >
  360. {t('Artifact Bundles')}
  361. </ListLink>
  362. <ListLink
  363. to={{
  364. pathname: releaseBundlesUrl,
  365. query: location.query,
  366. }}
  367. isActive={() => !tabDebugIdBundlesActive}
  368. >
  369. {t('Release Bundles')}
  370. </ListLink>
  371. </NavTabs>
  372. <SearchBarWithMarginBottom
  373. placeholder={
  374. tabDebugIdBundlesActive
  375. ? t('Filter by Bundle ID, Debug ID or Release')
  376. : t('Filter by Name')
  377. }
  378. onSearch={handleSearch}
  379. query={query}
  380. />
  381. {tabDebugIdBundlesActive ? (
  382. <DebugIdBundleList
  383. isLoading={debugIdBundlesLoading}
  384. debugIdBundles={debugIdBundlesData}
  385. project={project}
  386. onDelete={bundleId =>
  387. deleteDebugIdBundle({bundleId, projectSlug: project.slug})
  388. }
  389. emptyMessage={
  390. query
  391. ? t('No artifact bundles match your search query.')
  392. : t('No artifact bundles found for this project.')
  393. }
  394. />
  395. ) : (
  396. <Table
  397. headers={tableHeaders
  398. .filter(header => header.enabledFor.includes(currentBundleType))
  399. .map(header => header.component)}
  400. emptyMessage={
  401. query
  402. ? t('No release bundles match your search query.')
  403. : t('No release bundles found for this project.')
  404. }
  405. isEmpty={(archivesData ?? []).length === 0}
  406. isLoading={archivesLoading}
  407. >
  408. {archivesData?.map(data => (
  409. <SourceMapsTableRow
  410. key={data.name}
  411. bundleType={SourceMapsBundleType.RELEASE}
  412. date={data.date}
  413. fileCount={data.fileCount}
  414. name={data.name}
  415. onDelete={handleDeleteReleaseArtifacts}
  416. link={`/settings/${organization.slug}/projects/${
  417. project.slug
  418. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  419. />
  420. ))}
  421. </Table>
  422. )}
  423. <Pagination
  424. pageLinks={
  425. tabDebugIdBundlesActive
  426. ? debugIdBundlesHeaders?.('Link') ?? ''
  427. : archivesHeaders?.('Link') ?? ''
  428. }
  429. />
  430. </Fragment>
  431. );
  432. }
  433. const ReleaseBundlesPanelTable = styled(PanelTable)`
  434. grid-template-columns:
  435. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  436. minmax(74px, max-content);
  437. > * {
  438. :nth-child(-n + 4) {
  439. :nth-child(4n-1) {
  440. cursor: pointer;
  441. }
  442. }
  443. }
  444. `;
  445. const ArtifactBundlesPanelTable = styled(PanelTable)`
  446. grid-template-columns:
  447. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content) minmax(
  448. 242px,
  449. max-content
  450. )
  451. minmax(74px, max-content);
  452. > * {
  453. :nth-child(-n + 5) {
  454. :nth-child(5n-1) {
  455. cursor: pointer;
  456. }
  457. }
  458. }
  459. `;
  460. const ArtifactsTotalColumn = styled('div')`
  461. text-align: right;
  462. justify-content: flex-end;
  463. align-items: center;
  464. display: flex;
  465. `;
  466. const DateUploadedColumn = styled('div')`
  467. display: flex;
  468. align-items: center;
  469. gap: ${space(0.5)};
  470. `;
  471. const Column = styled('div')`
  472. display: flex;
  473. align-items: center;
  474. overflow: hidden;
  475. `;
  476. const IDColumn = styled(Column)`
  477. line-height: 140%;
  478. flex-direction: column;
  479. justify-content: center;
  480. align-items: flex-start;
  481. gap: ${space(0.5)};
  482. word-break: break-word;
  483. `;
  484. const ActionsColumn = styled(Column)`
  485. justify-content: flex-end;
  486. `;
  487. const SearchBarWithMarginBottom = styled(SearchBar)`
  488. margin-bottom: ${space(3)};
  489. `;
  490. const NoArtifactsUploadedWrapper = styled('div')`
  491. display: flex;
  492. align-items: center;
  493. gap: ${space(0.5)};
  494. `;