projectSourceMapsArtifacts.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. import {Fragment, useCallback} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Role} from 'sentry/components/acl/role';
  5. import Tag from 'sentry/components/badge/tag';
  6. import {LinkButton} from 'sentry/components/button';
  7. import FileSize from 'sentry/components/fileSize';
  8. import Link from 'sentry/components/links/link';
  9. import Pagination from 'sentry/components/pagination';
  10. import Panel from 'sentry/components/panels/panel';
  11. import {PanelTable} from 'sentry/components/panels/panelTable';
  12. import SearchBar from 'sentry/components/searchBar';
  13. import TimeSince from 'sentry/components/timeSince';
  14. import {Tooltip} from 'sentry/components/tooltip';
  15. import {IconClock, IconDownload} from 'sentry/icons';
  16. import {t, tct} from 'sentry/locale';
  17. import {space} from 'sentry/styles/space';
  18. import type {Project} from 'sentry/types/project';
  19. import type {Artifact} from 'sentry/types/release';
  20. import type {DebugIdBundleArtifact} from 'sentry/types/sourceMaps';
  21. import {useApiQuery} from 'sentry/utils/queryClient';
  22. import {decodeScalar} from 'sentry/utils/queryString';
  23. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  24. import useApi from 'sentry/utils/useApi';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  27. import {DebugIdBundleDeleteButton} from 'sentry/views/settings/projectSourceMaps/debugIdBundleDeleteButton';
  28. import {DebugIdBundleDetails} from 'sentry/views/settings/projectSourceMaps/debugIdBundleDetails';
  29. import {useDeleteDebugIdBundle} from 'sentry/views/settings/projectSourceMaps/useDeleteDebugIdBundle';
  30. enum DebugIdBundleArtifactType {
  31. INVALID = 0,
  32. SOURCE = 1,
  33. MINIFIED_SOURCE = 2,
  34. SOURCE_MAP = 3,
  35. INDEXED_RAM_BUNDLE = 4,
  36. }
  37. const debugIdBundleTypeLabels = {
  38. [DebugIdBundleArtifactType.INVALID]: t('Invalid'),
  39. [DebugIdBundleArtifactType.SOURCE]: t('Source'),
  40. [DebugIdBundleArtifactType.MINIFIED_SOURCE]: t('Minified'),
  41. [DebugIdBundleArtifactType.SOURCE_MAP]: t('Source Map'),
  42. [DebugIdBundleArtifactType.INDEXED_RAM_BUNDLE]: t('Indexed RAM Bundle'),
  43. };
  44. function ArtifactsTableRow({
  45. name,
  46. downloadRole,
  47. downloadUrl,
  48. size,
  49. type,
  50. orgSlug,
  51. artifactColumnDetails,
  52. }: {
  53. artifactColumnDetails: React.ReactNode;
  54. downloadRole: string;
  55. downloadUrl: string;
  56. name: string;
  57. orgSlug: string;
  58. size: number;
  59. type?: string;
  60. }) {
  61. return (
  62. <Fragment>
  63. <ArtifactColumn>
  64. <Name>{name || `(${t('empty')})`}</Name>
  65. {artifactColumnDetails}
  66. </ArtifactColumn>
  67. {type && <TypeColumn>{type}</TypeColumn>}
  68. <SizeColumn>
  69. <FileSize bytes={size} />
  70. </SizeColumn>
  71. <ActionsColumn>
  72. <Role role={downloadRole}>
  73. {({hasRole}) => {
  74. return (
  75. <Tooltip
  76. title={tct(
  77. 'Artifacts can only be downloaded by users with organization [downloadRole] role[orHigher]. This can be changed in [settingsLink:Debug Files Access] settings.',
  78. {
  79. downloadRole,
  80. orHigher: downloadRole !== 'owner' ? ` ${t('or higher')}` : '',
  81. settingsLink: <Link to={`/settings/${orgSlug}/#debugFilesRole`} />,
  82. }
  83. )}
  84. disabled={hasRole}
  85. isHoverable
  86. >
  87. <LinkButton
  88. size="sm"
  89. icon={<IconDownload size="sm" />}
  90. disabled={!hasRole}
  91. href={downloadUrl}
  92. title={hasRole ? t('Download Artifact') : undefined}
  93. aria-label={t('Download Artifact')}
  94. />
  95. </Tooltip>
  96. );
  97. }}
  98. </Role>
  99. </ActionsColumn>
  100. </Fragment>
  101. );
  102. }
  103. type Props = RouteComponentProps<
  104. {bundleId: string; orgId: string; projectId: string},
  105. {}
  106. > & {
  107. project: Project;
  108. };
  109. export function ProjectSourceMapsArtifacts({params, location, router, project}: Props) {
  110. const api = useApi();
  111. const organization = useOrganization();
  112. // query params
  113. const query = decodeScalar(location.query.query);
  114. const cursor = location.query.cursor ?? '';
  115. // endpoints
  116. const artifactsEndpoint = `/projects/${organization.slug}/${
  117. project.slug
  118. }/releases/${encodeURIComponent(params.bundleId)}/files/`;
  119. const debugIdBundlesArtifactsEndpoint = `/projects/${organization.slug}/${
  120. project.slug
  121. }/artifact-bundles/${encodeURIComponent(params.bundleId)}/files/`;
  122. // debug id bundles tab url
  123. const debugIdsUrl = normalizeUrl(
  124. `/settings/${organization.slug}/projects/${project.slug}/source-maps/artifact-bundles/${params.bundleId}/`
  125. );
  126. const tabDebugIdBundlesActive = location.pathname === debugIdsUrl;
  127. const {
  128. data: artifactsData,
  129. getResponseHeader: artifactsHeaders,
  130. isPending: artifactsLoading,
  131. } = useApiQuery<Artifact[]>(
  132. [
  133. artifactsEndpoint,
  134. {
  135. query: {query, cursor},
  136. },
  137. ],
  138. {
  139. staleTime: 0,
  140. keepPreviousData: true,
  141. enabled: !tabDebugIdBundlesActive,
  142. }
  143. );
  144. const {
  145. data: debugIdBundlesArtifactsData,
  146. getResponseHeader: debugIdBundlesArtifactsHeaders,
  147. isPending: debugIdBundlesArtifactsLoading,
  148. } = useApiQuery<DebugIdBundleArtifact>(
  149. [
  150. debugIdBundlesArtifactsEndpoint,
  151. {
  152. query: {query, cursor},
  153. },
  154. ],
  155. {
  156. staleTime: 0,
  157. keepPreviousData: true,
  158. enabled: tabDebugIdBundlesActive,
  159. }
  160. );
  161. const {mutate: deleteDebugIdArtifacts} = useDeleteDebugIdBundle({
  162. onSuccess: () =>
  163. router.push(
  164. `/settings/${organization.slug}/projects/${project.slug}/source-maps/artifact-bundles/`
  165. ),
  166. });
  167. const handleDeleteDebugIdBundle = useCallback(() => {
  168. if (!debugIdBundlesArtifactsData) {
  169. return;
  170. }
  171. deleteDebugIdArtifacts({
  172. projectSlug: project.slug,
  173. bundleId: debugIdBundlesArtifactsData.bundleId,
  174. });
  175. }, [debugIdBundlesArtifactsData, deleteDebugIdArtifacts, project.slug]);
  176. const handleSearch = useCallback(
  177. (newQuery: string) => {
  178. router.push({
  179. ...location,
  180. query: {...location.query, cursor: undefined, query: newQuery},
  181. });
  182. },
  183. [router, location]
  184. );
  185. return (
  186. <Fragment>
  187. <SettingsPageHeader
  188. title={tabDebugIdBundlesActive ? params.bundleId : t('Release Bundle')}
  189. action={
  190. tabDebugIdBundlesActive && (
  191. <DebugIdBundleDeleteButton size="sm" onDelete={handleDeleteDebugIdBundle} />
  192. )
  193. }
  194. subtitle={
  195. !tabDebugIdBundlesActive && (
  196. <VersionAndDetails>{params.bundleId}</VersionAndDetails>
  197. )
  198. }
  199. />
  200. {tabDebugIdBundlesActive && debugIdBundlesArtifactsData && (
  201. <DetailsPanel>
  202. <DebugIdBundleDetails debugIdBundle={debugIdBundlesArtifactsData} />
  203. </DetailsPanel>
  204. )}
  205. <SearchBarWithMarginBottom
  206. placeholder={
  207. tabDebugIdBundlesActive ? t('Filter by Path or ID') : t('Filter by Path')
  208. }
  209. onSearch={handleSearch}
  210. query={query}
  211. />
  212. <StyledPanelTable
  213. hasTypeColumn={tabDebugIdBundlesActive}
  214. headers={[
  215. t('Artifact'),
  216. ...(tabDebugIdBundlesActive
  217. ? [<TypeColumn key="type">{t('Type')}</TypeColumn>]
  218. : []),
  219. <SizeColumn key="file-size">{t('File Size')}</SizeColumn>,
  220. '',
  221. ]}
  222. emptyMessage={
  223. query
  224. ? t('No artifacts match your search query.')
  225. : tabDebugIdBundlesActive
  226. ? t('There are no artifacts in this bundle.')
  227. : t('There are no artifacts in this archive.')
  228. }
  229. isEmpty={
  230. (tabDebugIdBundlesActive
  231. ? debugIdBundlesArtifactsData?.files ?? []
  232. : artifactsData ?? []
  233. ).length === 0
  234. }
  235. isLoading={
  236. tabDebugIdBundlesActive ? debugIdBundlesArtifactsLoading : artifactsLoading
  237. }
  238. >
  239. {tabDebugIdBundlesActive
  240. ? (debugIdBundlesArtifactsData?.files ?? []).map(data => {
  241. const downloadUrl = `${api.baseUrl}/projects/${organization.slug}/${
  242. project.slug
  243. }/artifact-bundles/${encodeURIComponent(params.bundleId)}/files/${
  244. data.id
  245. }/?download=1`;
  246. return (
  247. <ArtifactsTableRow
  248. key={data.id}
  249. size={data.fileSize}
  250. name={data.filePath}
  251. type={debugIdBundleTypeLabels[data.fileType]}
  252. downloadRole={organization.debugFilesRole}
  253. downloadUrl={downloadUrl}
  254. orgSlug={organization.slug}
  255. artifactColumnDetails={
  256. <Fragment>
  257. {data.sourcemap ? (
  258. <div>
  259. <SubText>{t('Sourcemap Reference:')}</SubText> {data.sourcemap}
  260. </div>
  261. ) : null}
  262. {data.debugId ? (
  263. <div>
  264. <SubText>{t('Debug ID:')}</SubText> {data.debugId}
  265. </div>
  266. ) : null}
  267. </Fragment>
  268. }
  269. />
  270. );
  271. })
  272. : artifactsData?.map(data => {
  273. const downloadUrl = `${api.baseUrl}/projects/${organization.slug}/${
  274. project.slug
  275. }/releases/${encodeURIComponent(params.bundleId)}/files/${
  276. data.id
  277. }/?download=1`;
  278. return (
  279. <ArtifactsTableRow
  280. key={data.id}
  281. size={data.size}
  282. name={data.name}
  283. downloadRole={organization.debugFilesRole}
  284. downloadUrl={downloadUrl}
  285. orgSlug={organization.slug}
  286. artifactColumnDetails={
  287. <TimeAndDistWrapper>
  288. <TimeWrapper>
  289. <IconClock size="sm" />
  290. <TimeSince date={data.dateCreated} />
  291. </TimeWrapper>
  292. <StyledTag
  293. type={data.dist ? 'info' : undefined}
  294. tooltipText={data.dist ? undefined : t('No distribution set')}
  295. >
  296. {data.dist ?? t('none')}
  297. </StyledTag>
  298. </TimeAndDistWrapper>
  299. }
  300. />
  301. );
  302. })}
  303. </StyledPanelTable>
  304. <Pagination
  305. pageLinks={
  306. tabDebugIdBundlesActive
  307. ? debugIdBundlesArtifactsHeaders?.('Link') ?? ''
  308. : artifactsHeaders?.('Link') ?? ''
  309. }
  310. />
  311. </Fragment>
  312. );
  313. }
  314. const StyledPanelTable = styled(PanelTable)<{hasTypeColumn: boolean}>`
  315. grid-template-columns: minmax(220px, 1fr) minmax(120px, max-content) minmax(
  316. 74px,
  317. max-content
  318. );
  319. ${p =>
  320. p.hasTypeColumn &&
  321. `
  322. grid-template-columns:
  323. minmax(220px, 1fr) minmax(120px, max-content) minmax(120px, max-content)
  324. minmax(74px, max-content);
  325. `}
  326. `;
  327. const Column = styled('div')`
  328. display: flex;
  329. align-items: center;
  330. overflow: hidden;
  331. `;
  332. const ActionsColumn = styled(Column)`
  333. justify-content: flex-end;
  334. `;
  335. const SearchBarWithMarginBottom = styled(SearchBar)`
  336. margin-bottom: ${space(3)};
  337. `;
  338. const DetailsPanel = styled(Panel)`
  339. padding: ${space(1)} ${space(2)};
  340. `;
  341. const ArtifactColumn = styled('div')`
  342. overflow-wrap: break-word;
  343. word-break: break-all;
  344. line-height: 140%;
  345. display: flex;
  346. flex-direction: column;
  347. justify-content: center;
  348. `;
  349. const Name = styled('div')`
  350. display: flex;
  351. justify-content: flex-start;
  352. align-items: center;
  353. `;
  354. const TypeColumn = styled('div')`
  355. display: flex;
  356. justify-content: flex-end;
  357. text-align: right;
  358. align-items: center;
  359. color: ${p => p.theme.subText};
  360. `;
  361. const SizeColumn = styled('div')`
  362. display: flex;
  363. justify-content: flex-end;
  364. text-align: right;
  365. align-items: center;
  366. color: ${p => p.theme.subText};
  367. `;
  368. const TimeAndDistWrapper = styled('div')`
  369. width: 100%;
  370. display: flex;
  371. margin-top: ${space(1)};
  372. align-items: center;
  373. `;
  374. const TimeWrapper = styled('div')`
  375. display: grid;
  376. gap: ${space(0.5)};
  377. grid-template-columns: min-content 1fr;
  378. font-size: ${p => p.theme.fontSizeMedium};
  379. align-items: center;
  380. color: ${p => p.theme.subText};
  381. `;
  382. const StyledTag = styled(Tag)`
  383. margin-left: ${space(1)};
  384. `;
  385. const SubText = styled('span')`
  386. color: ${p => p.theme.subText};
  387. `;
  388. const VersionAndDetails = styled('div')`
  389. display: flex;
  390. flex-direction: column;
  391. gap: ${space(1)};
  392. word-break: break-word;
  393. `;