projectSourceMapsArtifacts.tsx 12 KB

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