projectSourceMaps.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import {Fragment, useCallback} 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 SearchBar from 'sentry/components/searchBar';
  21. import {Tooltip} from 'sentry/components/tooltip';
  22. import {IconArrow, IconDelete, IconWarning} from 'sentry/icons';
  23. import {t, tct} from 'sentry/locale';
  24. import {space} from 'sentry/styles/space';
  25. import {DebugIdBundle, Project, SourceMapsArchive} from 'sentry/types';
  26. import {useApiQuery} from 'sentry/utils/queryClient';
  27. import {decodeScalar} from 'sentry/utils/queryString';
  28. import useApi from 'sentry/utils/useApi';
  29. import useOrganization from 'sentry/utils/useOrganization';
  30. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  31. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  32. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  33. import {DebugIdBundlesTags} from 'sentry/views/settings/projectSourceMaps/debugIdBundlesTags';
  34. enum SORT_BY {
  35. ASC = 'date_added',
  36. DESC = '-date_added',
  37. }
  38. enum SourceMapsBundleType {
  39. Release,
  40. DebugId,
  41. }
  42. function SourceMapsTableRow({
  43. bundleType,
  44. onDelete,
  45. name,
  46. fileCount,
  47. link,
  48. date,
  49. idColumnDetails,
  50. }: {
  51. bundleType: SourceMapsBundleType;
  52. date: string;
  53. fileCount: number;
  54. link: string;
  55. name: string;
  56. onDelete: (name: string) => void;
  57. idColumnDetails?: React.ReactNode;
  58. }) {
  59. const isEmptyReleaseBundle =
  60. bundleType === SourceMapsBundleType.Release && fileCount === -1;
  61. return (
  62. <Fragment>
  63. <IDColumn>
  64. {isEmptyReleaseBundle ? name : <Link to={link}>{name}</Link>}
  65. {idColumnDetails}
  66. </IDColumn>
  67. <ArtifactsTotalColumn>
  68. {isEmptyReleaseBundle ? (
  69. <Tooltip title={t('No bundle connected to this release')}>
  70. <IconWrapper>
  71. <IconWarning color="warning" size="sm" />
  72. </IconWrapper>
  73. </Tooltip>
  74. ) : (
  75. <Count value={fileCount} />
  76. )}
  77. </ArtifactsTotalColumn>
  78. <Column>
  79. {isEmptyReleaseBundle ? t('(no value)') : <DateTime date={date} timeZone />}
  80. </Column>
  81. <ActionsColumn>
  82. {isEmptyReleaseBundle ? (
  83. <Button
  84. size="sm"
  85. icon={<IconDelete size="sm" />}
  86. title={t('No bundle to delete')}
  87. aria-label={t('No bundle to delete')}
  88. disabled
  89. />
  90. ) : (
  91. <Access access={['project:releases']}>
  92. {({hasAccess}) => (
  93. <Tooltip
  94. disabled={hasAccess}
  95. title={t('You do not have permission to delete artifacts.')}
  96. >
  97. <Confirm
  98. onConfirm={() => onDelete(name)}
  99. message={t(
  100. 'Are you sure you want to remove all artifacts in this archive?'
  101. )}
  102. disabled={!hasAccess}
  103. >
  104. <Button
  105. size="sm"
  106. icon={<IconDelete size="sm" />}
  107. title={t('Remove All Artifacts')}
  108. aria-label={t('Remove All Artifacts')}
  109. disabled={!hasAccess}
  110. />
  111. </Confirm>
  112. </Tooltip>
  113. )}
  114. </Access>
  115. )}
  116. </ActionsColumn>
  117. </Fragment>
  118. );
  119. }
  120. type Props = RouteComponentProps<{orgId: string; projectId: string}, {}> & {
  121. project: Project;
  122. };
  123. export function ProjectSourceMaps({location, router, project}: Props) {
  124. const api = useApi();
  125. const organization = useOrganization();
  126. // query params
  127. const query = decodeScalar(location.query.query);
  128. const sortBy = location.query.sort ?? SORT_BY.DESC;
  129. const cursor = location.query.cursor ?? '';
  130. // endpoints
  131. const sourceMapsEndpoint = `/projects/${organization.slug}/${project.slug}/files/source-maps/`;
  132. const debugIdBundlesEndpoint = `/projects/${organization.slug}/${project.slug}/files/artifact-bundles/`;
  133. // tab urls
  134. const releaseBundlesUrl = normalizeUrl(
  135. `/settings/${organization.slug}/projects/${project.slug}/source-maps/release-bundles/`
  136. );
  137. const debugIdsUrl = normalizeUrl(
  138. `/settings/${organization.slug}/projects/${project.slug}/source-maps/artifact-bundles/`
  139. );
  140. const tabDebugIdBundlesActive = location.pathname === debugIdsUrl;
  141. const {
  142. data: archivesData,
  143. isLoading: archivesLoading,
  144. refetch: archivesRefetch,
  145. } = useApiQuery<[SourceMapsArchive[], any, any]>(
  146. [
  147. sourceMapsEndpoint,
  148. {
  149. query: {query, cursor, sortBy},
  150. },
  151. ],
  152. () => {
  153. return api.requestPromise(sourceMapsEndpoint, {
  154. query: {query, cursor, sortBy},
  155. includeAllArgs: true,
  156. });
  157. },
  158. {
  159. staleTime: 0,
  160. keepPreviousData: true,
  161. enabled: !tabDebugIdBundlesActive,
  162. }
  163. );
  164. const {
  165. data: debugIdBundlesData,
  166. isLoading: debugIdBundlesLoading,
  167. refetch: debugIdBundlesRefetch,
  168. } = useApiQuery<[DebugIdBundle[], any, any]>(
  169. [
  170. debugIdBundlesEndpoint,
  171. {
  172. query: {query, cursor, sortBy},
  173. },
  174. ],
  175. () => {
  176. return api.requestPromise(debugIdBundlesEndpoint, {
  177. query: {query, cursor, sortBy},
  178. includeAllArgs: true,
  179. });
  180. },
  181. {
  182. staleTime: 0,
  183. keepPreviousData: true,
  184. enabled: tabDebugIdBundlesActive,
  185. }
  186. );
  187. const handleSearch = useCallback(
  188. (newQuery: string) => {
  189. router.push({
  190. ...location,
  191. query: {...location.query, cursor: undefined, query: newQuery},
  192. });
  193. },
  194. [router, location]
  195. );
  196. const handleSortChange = useCallback(() => {
  197. router.push({
  198. pathname: location.pathname,
  199. query: {
  200. ...location.query,
  201. cursor: undefined,
  202. sort: sortBy === SORT_BY.ASC ? SORT_BY.DESC : SORT_BY.ASC,
  203. },
  204. });
  205. }, [location, router, sortBy]);
  206. const handleDelete = useCallback(
  207. async (name: string) => {
  208. addLoadingMessage(t('Removing artifacts\u2026'));
  209. try {
  210. await api.requestPromise(
  211. tabDebugIdBundlesActive ? debugIdBundlesEndpoint : sourceMapsEndpoint,
  212. {
  213. method: 'DELETE',
  214. query: tabDebugIdBundlesActive ? {bundleId: name} : {name},
  215. }
  216. );
  217. tabDebugIdBundlesActive ? debugIdBundlesRefetch() : archivesRefetch();
  218. addSuccessMessage(t('Artifacts removed.'));
  219. } catch {
  220. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  221. }
  222. },
  223. [
  224. api,
  225. sourceMapsEndpoint,
  226. tabDebugIdBundlesActive,
  227. debugIdBundlesRefetch,
  228. archivesRefetch,
  229. debugIdBundlesEndpoint,
  230. ]
  231. );
  232. return (
  233. <Fragment>
  234. <SettingsPageHeader title={t('Source Maps')} />
  235. <TextBlock>
  236. {tct(
  237. `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].`,
  238. {
  239. link: (
  240. <ExternalLink href="https://docs.sentry.io/platforms/javascript/sourcemaps/" />
  241. ),
  242. }
  243. )}
  244. </TextBlock>
  245. <NavTabs underlined>
  246. <ListLink to={releaseBundlesUrl} index isActive={() => !tabDebugIdBundlesActive}>
  247. {t('Release Bundles')}
  248. </ListLink>
  249. <ListLink to={debugIdsUrl} isActive={() => tabDebugIdBundlesActive}>
  250. {t('Artifact Bundles')}
  251. </ListLink>
  252. </NavTabs>
  253. <SearchBarWithMarginBottom
  254. placeholder={
  255. tabDebugIdBundlesActive ? t('Filter by Bundle ID') : t('Filter by Name')
  256. }
  257. onSearch={handleSearch}
  258. query={query}
  259. />
  260. <StyledPanelTable
  261. headers={[
  262. tabDebugIdBundlesActive ? t('Bundle ID') : t('Name'),
  263. <ArtifactsTotalColumn key="artifacts-total">
  264. {t('Artifacts')}
  265. </ArtifactsTotalColumn>,
  266. <DateUploadedColumn key="date-uploaded" onClick={handleSortChange}>
  267. {t('Date Uploaded')}
  268. <Tooltip
  269. containerDisplayMode="inline-flex"
  270. title={
  271. sortBy === SORT_BY.DESC
  272. ? t('Switch to ascending order')
  273. : t('Switch to descending order')
  274. }
  275. >
  276. <IconArrow
  277. direction={sortBy === SORT_BY.DESC ? 'down' : 'up'}
  278. data-test-id="icon-arrow"
  279. />
  280. </Tooltip>
  281. </DateUploadedColumn>,
  282. '',
  283. ]}
  284. emptyMessage={
  285. query
  286. ? tct('No [tabName] match your search query.', {
  287. tabName: tabDebugIdBundlesActive
  288. ? t('debug ID bundles')
  289. : t('release bundles'),
  290. })
  291. : tct('No [tabName] found for this project.', {
  292. tabName: tabDebugIdBundlesActive
  293. ? t('debug ID bundles')
  294. : t('release bundles'),
  295. })
  296. }
  297. isEmpty={
  298. (tabDebugIdBundlesActive
  299. ? debugIdBundlesData?.[0] ?? []
  300. : archivesData?.[0] ?? []
  301. ).length === 0
  302. }
  303. isLoading={tabDebugIdBundlesActive ? debugIdBundlesLoading : archivesLoading}
  304. >
  305. {tabDebugIdBundlesActive
  306. ? debugIdBundlesData?.[0].map(data => (
  307. <SourceMapsTableRow
  308. key={data.bundleId}
  309. bundleType={SourceMapsBundleType.DebugId}
  310. date={data.date}
  311. fileCount={data.fileCount}
  312. name={data.bundleId}
  313. onDelete={handleDelete}
  314. link={`/settings/${organization.slug}/projects/${
  315. project.slug
  316. }/source-maps/artifact-bundles/${encodeURIComponent(data.bundleId)}`}
  317. idColumnDetails={
  318. <DebugIdBundlesTags dist={data.dist} release={data.release} />
  319. }
  320. />
  321. ))
  322. : archivesData?.[0].map(data => (
  323. <SourceMapsTableRow
  324. key={data.name}
  325. bundleType={SourceMapsBundleType.Release}
  326. date={data.date}
  327. fileCount={data.fileCount}
  328. name={data.name}
  329. onDelete={handleDelete}
  330. link={`/settings/${organization.slug}/projects/${
  331. project.slug
  332. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  333. />
  334. ))}
  335. </StyledPanelTable>
  336. <Pagination
  337. pageLinks={
  338. tabDebugIdBundlesActive
  339. ? debugIdBundlesData?.[2]?.getResponseHeader('Link') ?? ''
  340. : archivesData?.[2]?.getResponseHeader('Link') ?? ''
  341. }
  342. />
  343. </Fragment>
  344. );
  345. }
  346. const StyledPanelTable = styled(PanelTable)`
  347. grid-template-columns:
  348. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  349. minmax(74px, max-content);
  350. > * {
  351. :nth-child(-n + 4) {
  352. :nth-child(4n-1) {
  353. cursor: pointer;
  354. }
  355. }
  356. }
  357. `;
  358. const ArtifactsTotalColumn = styled('div')`
  359. text-align: right;
  360. justify-content: flex-end;
  361. align-items: center;
  362. display: flex;
  363. `;
  364. const DateUploadedColumn = styled('div')`
  365. display: flex;
  366. align-items: center;
  367. gap: ${space(0.5)};
  368. `;
  369. const Column = styled('div')`
  370. display: flex;
  371. align-items: center;
  372. overflow: hidden;
  373. `;
  374. const IDColumn = styled(Column)`
  375. line-height: 140%;
  376. flex-direction: column;
  377. justify-content: center;
  378. align-items: flex-start;
  379. gap: ${space(0.5)};
  380. word-break: break-word;
  381. `;
  382. const ActionsColumn = styled(Column)`
  383. justify-content: flex-end;
  384. `;
  385. const SearchBarWithMarginBottom = styled(SearchBar)`
  386. margin-bottom: ${space(3)};
  387. `;
  388. const IconWrapper = styled('div')`
  389. display: flex;
  390. `;