projectSourceMaps.tsx 11 KB

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