projectSourceMaps.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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(sourceMapsEndpoint, {
  185. method: 'DELETE',
  186. query: {name},
  187. });
  188. tabDebugIdBundlesActive ? debugIdBundlesRefetch() : archivesRefetch();
  189. addSuccessMessage(t('Artifacts removed.'));
  190. } catch {
  191. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  192. }
  193. },
  194. [
  195. api,
  196. sourceMapsEndpoint,
  197. tabDebugIdBundlesActive,
  198. debugIdBundlesRefetch,
  199. archivesRefetch,
  200. ]
  201. );
  202. return (
  203. <Fragment>
  204. <SettingsPageHeader title={t('Source Maps')} />
  205. <TextBlock>
  206. {tct(
  207. `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].`,
  208. {
  209. link: (
  210. <ExternalLink href="https://docs.sentry.io/platforms/javascript/sourcemaps/" />
  211. ),
  212. }
  213. )}
  214. </TextBlock>
  215. <NavTabs underlined>
  216. <ListLink to={releaseBundlesUrl} index isActive={() => !tabDebugIdBundlesActive}>
  217. {t('Release Bundles')}
  218. </ListLink>
  219. <ListLink to={debugIdsUrl} isActive={() => tabDebugIdBundlesActive}>
  220. {t('Debug ID Bundles')}
  221. </ListLink>
  222. </NavTabs>
  223. <SearchBarWithMarginBottom
  224. placeholder={
  225. tabDebugIdBundlesActive ? t('Filter by Bundle ID') : t('Filter by Name')
  226. }
  227. onSearch={handleSearch}
  228. query={query}
  229. />
  230. <StyledPanelTable
  231. headers={[
  232. tabDebugIdBundlesActive ? t('Bundle ID') : t('Name'),
  233. <ArtifactsTotalColumn key="artifacts-total">
  234. {t('Artifacts')}
  235. </ArtifactsTotalColumn>,
  236. <DateUploadedColumn key="date-uploaded" onClick={handleSortChange}>
  237. {t('Date Uploaded')}
  238. <Tooltip
  239. containerDisplayMode="inline-flex"
  240. title={
  241. sortBy === SORT_BY.DESC
  242. ? t('Switch to ascending order')
  243. : t('Switch to descending order')
  244. }
  245. >
  246. <IconArrow
  247. direction={sortBy === SORT_BY.DESC ? 'down' : 'up'}
  248. data-test-id="icon-arrow"
  249. />
  250. </Tooltip>
  251. </DateUploadedColumn>,
  252. '',
  253. ]}
  254. emptyMessage={
  255. query
  256. ? tct('No [tabName] match your search query.', {
  257. tabName: tabDebugIdBundlesActive
  258. ? t('debug ID bundles')
  259. : t('release bundles'),
  260. })
  261. : tct('No [tabName] found for this project.', {
  262. tabName: tabDebugIdBundlesActive
  263. ? t('debug ID bundles')
  264. : t('release bundles'),
  265. })
  266. }
  267. isEmpty={
  268. (tabDebugIdBundlesActive
  269. ? debugIdBundlesData?.[0] ?? []
  270. : archivesData?.[0] ?? []
  271. ).length === 0
  272. }
  273. isLoading={tabDebugIdBundlesActive ? debugIdBundlesLoading : archivesLoading}
  274. >
  275. {tabDebugIdBundlesActive
  276. ? debugIdBundlesData?.[0].map(data => (
  277. <SourceMapsTableRow
  278. key={data.bundleId}
  279. date={data.date}
  280. fileCount={data.fileCount}
  281. name={data.bundleId}
  282. onDelete={handleDelete}
  283. link={`/settings/${organization.slug}/projects/${
  284. project.slug
  285. }/source-maps/debug-id-bundles/${encodeURIComponent(data.bundleId)}`}
  286. idColumnDetails={
  287. <DebugIdBundlesTags dist={data.dist} release={data.release} />
  288. }
  289. />
  290. ))
  291. : archivesData?.[0].map(data => (
  292. <SourceMapsTableRow
  293. key={data.name}
  294. date={data.date}
  295. fileCount={data.fileCount}
  296. name={data.name}
  297. onDelete={handleDelete}
  298. link={`/settings/${organization.slug}/projects/${
  299. project.slug
  300. }/source-maps/release-bundles/${encodeURIComponent(data.name)}`}
  301. />
  302. ))}
  303. </StyledPanelTable>
  304. <Pagination
  305. pageLinks={
  306. tabDebugIdBundlesActive
  307. ? debugIdBundlesData?.[2]?.getResponseHeader('Link') ?? ''
  308. : archivesData?.[2]?.getResponseHeader('Link') ?? ''
  309. }
  310. />
  311. </Fragment>
  312. );
  313. }
  314. const StyledPanelTable = styled(PanelTable)`
  315. grid-template-columns:
  316. minmax(120px, 1fr) minmax(120px, max-content) minmax(242px, max-content)
  317. minmax(74px, max-content);
  318. > * {
  319. :nth-child(-n + 4) {
  320. :nth-child(4n-1) {
  321. cursor: pointer;
  322. }
  323. }
  324. }
  325. `;
  326. const ArtifactsTotalColumn = styled('div')`
  327. text-align: right;
  328. justify-content: flex-end;
  329. align-items: center;
  330. display: flex;
  331. `;
  332. const DateUploadedColumn = styled('div')`
  333. display: flex;
  334. align-items: center;
  335. gap: ${space(0.5)};
  336. `;
  337. const Column = styled('div')`
  338. display: flex;
  339. align-items: center;
  340. overflow: hidden;
  341. `;
  342. const IDColumn = styled(Column)`
  343. line-height: 140%;
  344. flex-direction: column;
  345. justify-content: center;
  346. align-items: flex-start;
  347. gap: ${space(0.5)};
  348. word-break: break-word;
  349. `;
  350. const ActionsColumn = styled(Column)`
  351. justify-content: flex-end;
  352. `;
  353. const SearchBarWithMarginBottom = styled(SearchBar)`
  354. margin-bottom: ${space(3)};
  355. `;