projectSourceMapsArtifacts.tsx 11 KB

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