projectSourceMaps.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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
  259. ? t('Filter by Bundle ID, Debug ID or Release')
  260. : t('Filter by Name')
  261. }
  262. onSearch={handleSearch}
  263. query={query}
  264. />
  265. <StyledPanelTable
  266. headers={[
  267. tabDebugIdBundlesActive ? t('Bundle ID') : t('Name'),
  268. <ArtifactsTotalColumn key="artifacts-total">
  269. {t('Artifacts')}
  270. </ArtifactsTotalColumn>,
  271. <DateUploadedColumn key="date-uploaded" onClick={handleSortChange}>
  272. {t('Date Uploaded')}
  273. <Tooltip
  274. containerDisplayMode="inline-flex"
  275. title={
  276. sortBy === SortBy.DESC
  277. ? t('Switch to ascending order')
  278. : t('Switch to descending order')
  279. }
  280. >
  281. <IconArrow
  282. direction={sortBy === SortBy.DESC ? 'down' : 'up'}
  283. data-test-id="icon-arrow"
  284. />
  285. </Tooltip>
  286. </DateUploadedColumn>,
  287. '',
  288. ]}
  289. emptyMessage={
  290. query
  291. ? tct('No [tabName] match your search query.', {
  292. tabName: tabDebugIdBundlesActive
  293. ? t('artifact bundles')
  294. : t('release bundles'),
  295. })
  296. : tct('No [tabName] found for this project.', {
  297. tabName: tabDebugIdBundlesActive
  298. ? t('artifact bundles')
  299. : t('release bundles'),
  300. })
  301. }
  302. isEmpty={
  303. (tabDebugIdBundlesActive ? debugIdBundlesData ?? [] : archivesData ?? [])
  304. .length === 0
  305. }
  306. isLoading={tabDebugIdBundlesActive ? debugIdBundlesLoading : archivesLoading}
  307. >
  308. {tabDebugIdBundlesActive
  309. ? debugIdBundlesData?.map(data => (
  310. <SourceMapsTableRow
  311. key={data.bundleId}
  312. bundleType={SourceMapsBundleType.DEBUG_ID}
  313. date={data.date}
  314. fileCount={data.fileCount}
  315. name={data.bundleId}
  316. onDelete={handleDelete}
  317. link={`/settings/${organization.slug}/projects/${
  318. project.slug
  319. }/source-maps/artifact-bundles/${encodeURIComponent(data.bundleId)}`}
  320. idColumnDetails={
  321. // TODO(Pri): Move the loading to the component once fully transitioned to associations.
  322. !debugIdBundlesLoading &&
  323. (data.associations ? (
  324. <Associations associations={data.associations} />
  325. ) : (
  326. <DebugIdBundlesTags dist={data.dist} release={data.release} />
  327. ))
  328. }
  329. />
  330. ))
  331. : archivesData?.map(data => (
  332. <SourceMapsTableRow
  333. key={data.name}
  334. bundleType={SourceMapsBundleType.RELEASE}
  335. date={data.date}
  336. fileCount={data.fileCount}
  337. name={data.name}
  338. onDelete={handleDelete}
  339. link={`/settings/${organization.slug}/projects/${
  340. project.slug
  341. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  342. />
  343. ))}
  344. </StyledPanelTable>
  345. <Pagination
  346. pageLinks={
  347. tabDebugIdBundlesActive
  348. ? debugIdBundlesHeaders?.('Link') ?? ''
  349. : archivesHeaders?.('Link') ?? ''
  350. }
  351. />
  352. </Fragment>
  353. );
  354. }
  355. const StyledPanelTable = styled(PanelTable)`
  356. grid-template-columns:
  357. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  358. minmax(74px, max-content);
  359. > * {
  360. :nth-child(-n + 4) {
  361. :nth-child(4n-1) {
  362. cursor: pointer;
  363. }
  364. }
  365. }
  366. `;
  367. const ArtifactsTotalColumn = styled('div')`
  368. text-align: right;
  369. justify-content: flex-end;
  370. align-items: center;
  371. display: flex;
  372. `;
  373. const DateUploadedColumn = styled('div')`
  374. display: flex;
  375. align-items: center;
  376. gap: ${space(0.5)};
  377. `;
  378. const Column = styled('div')`
  379. display: flex;
  380. align-items: center;
  381. overflow: hidden;
  382. `;
  383. const IDColumn = styled(Column)`
  384. line-height: 140%;
  385. flex-direction: column;
  386. justify-content: center;
  387. align-items: flex-start;
  388. gap: ${space(0.5)};
  389. word-break: break-word;
  390. `;
  391. const ActionsColumn = styled(Column)`
  392. justify-content: flex-end;
  393. `;
  394. const SearchBarWithMarginBottom = styled(SearchBar)`
  395. margin-bottom: ${space(3)};
  396. `;
  397. const NoArtifactsUploadedWrapper = styled('div')`
  398. display: flex;
  399. align-items: center;
  400. gap: ${space(0.5)};
  401. `;