projectSourceMapsArtifacts.tsx 11 KB

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