projectSourceMaps.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. getResponseHeader: archivesHeaders,
  144. isLoading: archivesLoading,
  145. refetch: archivesRefetch,
  146. } = useApiQuery<SourceMapsArchive[]>(
  147. [
  148. sourceMapsEndpoint,
  149. {
  150. query: {query, cursor, sortBy},
  151. },
  152. ],
  153. {
  154. staleTime: 0,
  155. keepPreviousData: true,
  156. enabled: !tabDebugIdBundlesActive,
  157. }
  158. );
  159. const {
  160. data: debugIdBundlesData,
  161. getResponseHeader: debugIdBundlesHeaders,
  162. isLoading: debugIdBundlesLoading,
  163. refetch: debugIdBundlesRefetch,
  164. } = useApiQuery<DebugIdBundle[]>(
  165. [
  166. debugIdBundlesEndpoint,
  167. {
  168. query: {query, cursor, sortBy},
  169. },
  170. ],
  171. {
  172. staleTime: 0,
  173. keepPreviousData: true,
  174. enabled: tabDebugIdBundlesActive,
  175. }
  176. );
  177. const handleSearch = useCallback(
  178. (newQuery: string) => {
  179. router.push({
  180. ...location,
  181. query: {...location.query, cursor: undefined, query: newQuery},
  182. });
  183. },
  184. [router, location]
  185. );
  186. const handleSortChange = useCallback(() => {
  187. router.push({
  188. pathname: location.pathname,
  189. query: {
  190. ...location.query,
  191. cursor: undefined,
  192. sort: sortBy === SORT_BY.ASC ? SORT_BY.DESC : SORT_BY.ASC,
  193. },
  194. });
  195. }, [location, router, sortBy]);
  196. const handleDelete = useCallback(
  197. async (name: string) => {
  198. addLoadingMessage(t('Removing artifacts\u2026'));
  199. try {
  200. await api.requestPromise(
  201. tabDebugIdBundlesActive ? debugIdBundlesEndpoint : sourceMapsEndpoint,
  202. {
  203. method: 'DELETE',
  204. query: tabDebugIdBundlesActive ? {bundleId: name} : {name},
  205. }
  206. );
  207. tabDebugIdBundlesActive ? debugIdBundlesRefetch() : archivesRefetch();
  208. addSuccessMessage(t('Artifacts removed.'));
  209. } catch {
  210. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  211. }
  212. },
  213. [
  214. api,
  215. sourceMapsEndpoint,
  216. tabDebugIdBundlesActive,
  217. debugIdBundlesRefetch,
  218. archivesRefetch,
  219. debugIdBundlesEndpoint,
  220. ]
  221. );
  222. return (
  223. <Fragment>
  224. <SettingsPageHeader title={t('Source Maps')} />
  225. <TextBlock>
  226. {tct(
  227. `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].`,
  228. {
  229. link: (
  230. <ExternalLink href="https://docs.sentry.io/platforms/javascript/sourcemaps/" />
  231. ),
  232. }
  233. )}
  234. </TextBlock>
  235. <NavTabs underlined>
  236. <ListLink to={releaseBundlesUrl} index isActive={() => !tabDebugIdBundlesActive}>
  237. {t('Release Bundles')}
  238. </ListLink>
  239. <ListLink to={debugIdsUrl} isActive={() => tabDebugIdBundlesActive}>
  240. {t('Artifact Bundles')}
  241. </ListLink>
  242. </NavTabs>
  243. <SearchBarWithMarginBottom
  244. placeholder={
  245. tabDebugIdBundlesActive ? t('Filter by Bundle ID') : t('Filter by Name')
  246. }
  247. onSearch={handleSearch}
  248. query={query}
  249. />
  250. <StyledPanelTable
  251. headers={[
  252. tabDebugIdBundlesActive ? t('Bundle ID') : t('Name'),
  253. <ArtifactsTotalColumn key="artifacts-total">
  254. {t('Artifacts')}
  255. </ArtifactsTotalColumn>,
  256. <DateUploadedColumn key="date-uploaded" onClick={handleSortChange}>
  257. {t('Date Uploaded')}
  258. <Tooltip
  259. containerDisplayMode="inline-flex"
  260. title={
  261. sortBy === SORT_BY.DESC
  262. ? t('Switch to ascending order')
  263. : t('Switch to descending order')
  264. }
  265. >
  266. <IconArrow
  267. direction={sortBy === SORT_BY.DESC ? 'down' : 'up'}
  268. data-test-id="icon-arrow"
  269. />
  270. </Tooltip>
  271. </DateUploadedColumn>,
  272. '',
  273. ]}
  274. emptyMessage={
  275. query
  276. ? tct('No [tabName] match your search query.', {
  277. tabName: tabDebugIdBundlesActive
  278. ? t('debug ID bundles')
  279. : t('release bundles'),
  280. })
  281. : tct('No [tabName] found for this project.', {
  282. tabName: tabDebugIdBundlesActive
  283. ? t('debug ID bundles')
  284. : t('release bundles'),
  285. })
  286. }
  287. isEmpty={
  288. (tabDebugIdBundlesActive ? debugIdBundlesData ?? [] : archivesData ?? [])
  289. .length === 0
  290. }
  291. isLoading={tabDebugIdBundlesActive ? debugIdBundlesLoading : archivesLoading}
  292. >
  293. {tabDebugIdBundlesActive
  294. ? debugIdBundlesData?.map(data => (
  295. <SourceMapsTableRow
  296. key={data.bundleId}
  297. bundleType={SourceMapsBundleType.DebugId}
  298. date={data.date}
  299. fileCount={data.fileCount}
  300. name={data.bundleId}
  301. onDelete={handleDelete}
  302. link={`/settings/${organization.slug}/projects/${
  303. project.slug
  304. }/source-maps/artifact-bundles/${encodeURIComponent(data.bundleId)}`}
  305. idColumnDetails={
  306. <DebugIdBundlesTags dist={data.dist} release={data.release} />
  307. }
  308. />
  309. ))
  310. : archivesData?.map(data => (
  311. <SourceMapsTableRow
  312. key={data.name}
  313. bundleType={SourceMapsBundleType.Release}
  314. date={data.date}
  315. fileCount={data.fileCount}
  316. name={data.name}
  317. onDelete={handleDelete}
  318. link={`/settings/${organization.slug}/projects/${
  319. project.slug
  320. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  321. />
  322. ))}
  323. </StyledPanelTable>
  324. <Pagination
  325. pageLinks={
  326. tabDebugIdBundlesActive
  327. ? debugIdBundlesHeaders?.('Link') ?? ''
  328. : archivesHeaders?.('Link') ?? ''
  329. }
  330. />
  331. </Fragment>
  332. );
  333. }
  334. const StyledPanelTable = styled(PanelTable)`
  335. grid-template-columns:
  336. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  337. minmax(74px, max-content);
  338. > * {
  339. :nth-child(-n + 4) {
  340. :nth-child(4n-1) {
  341. cursor: pointer;
  342. }
  343. }
  344. }
  345. `;
  346. const ArtifactsTotalColumn = styled('div')`
  347. text-align: right;
  348. justify-content: flex-end;
  349. align-items: center;
  350. display: flex;
  351. `;
  352. const DateUploadedColumn = styled('div')`
  353. display: flex;
  354. align-items: center;
  355. gap: ${space(0.5)};
  356. `;
  357. const Column = styled('div')`
  358. display: flex;
  359. align-items: center;
  360. overflow: hidden;
  361. `;
  362. const IDColumn = styled(Column)`
  363. line-height: 140%;
  364. flex-direction: column;
  365. justify-content: center;
  366. align-items: flex-start;
  367. gap: ${space(0.5)};
  368. word-break: break-word;
  369. `;
  370. const ActionsColumn = styled(Column)`
  371. justify-content: flex-end;
  372. `;
  373. const SearchBarWithMarginBottom = styled(SearchBar)`
  374. margin-bottom: ${space(3)};
  375. `;
  376. const IconWrapper = styled('div')`
  377. display: flex;
  378. `;