projectSourceMaps.tsx 13 KB

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