projectSourceMapsArtifacts.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. 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 debugIdBundlesArtifactsEndpoint = `/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/artifact-bundles/${params.bundleId}/`
  118. );
  119. const tabDebugIdBundlesActive = location.pathname === debugIdsUrl;
  120. const {
  121. data: artifactsData,
  122. getResponseHeader: artifactsHeaders,
  123. isLoading: artifactsLoading,
  124. } = useApiQuery<Artifact[]>(
  125. [
  126. artifactsEndpoint,
  127. {
  128. query: {query, cursor},
  129. },
  130. ],
  131. {
  132. staleTime: 0,
  133. keepPreviousData: true,
  134. enabled: !tabDebugIdBundlesActive,
  135. }
  136. );
  137. const {
  138. data: debugIdBundlesArtifactsData,
  139. getResponseHeader: debugIdBundlesArtifactsHeaders,
  140. isLoading: debugIdBundlesArtifactsLoading,
  141. } = useApiQuery<DebugIdBundleArtifact>(
  142. [
  143. debugIdBundlesArtifactsEndpoint,
  144. {
  145. query: {query, cursor},
  146. },
  147. ],
  148. {
  149. staleTime: 0,
  150. keepPreviousData: true,
  151. enabled: tabDebugIdBundlesActive,
  152. }
  153. );
  154. const handleSearch = useCallback(
  155. (newQuery: string) => {
  156. router.push({
  157. ...location,
  158. query: {...location.query, cursor: undefined, query: newQuery},
  159. });
  160. },
  161. [router, location]
  162. );
  163. return (
  164. <Fragment>
  165. <SettingsPageHeader
  166. title={tabDebugIdBundlesActive ? t('Artifact Bundle') : t('Release Bundle')}
  167. subtitle={
  168. <VersionAndDetails>
  169. {params.bundleId}
  170. {tabDebugIdBundlesActive &&
  171. // TODO(Pri): Move the loading to the component once fully transitioned to associations.
  172. !debugIdBundlesArtifactsLoading &&
  173. (debugIdBundlesArtifactsData?.associations ? (
  174. <Associations associations={debugIdBundlesArtifactsData?.associations} />
  175. ) : (
  176. <DebugIdBundlesTags
  177. dist={debugIdBundlesArtifactsData?.dist}
  178. release={debugIdBundlesArtifactsData?.release}
  179. />
  180. ))}
  181. </VersionAndDetails>
  182. }
  183. />
  184. <SearchBarWithMarginBottom
  185. placeholder={
  186. tabDebugIdBundlesActive ? t('Filter by Path or ID') : t('Filter by Path')
  187. }
  188. onSearch={handleSearch}
  189. query={query}
  190. />
  191. <StyledPanelTable
  192. headers={[
  193. t('Artifact'),
  194. <SizeColumn key="file-size">{t('File Size')}</SizeColumn>,
  195. '',
  196. ]}
  197. emptyMessage={
  198. query
  199. ? t('No artifacts match your search query.')
  200. : tabDebugIdBundlesActive
  201. ? t('There are no artifacts in this bundle.')
  202. : t('There are no artifacts in this archive.')
  203. }
  204. isEmpty={
  205. (tabDebugIdBundlesActive
  206. ? debugIdBundlesArtifactsData?.files ?? []
  207. : artifactsData ?? []
  208. ).length === 0
  209. }
  210. isLoading={
  211. tabDebugIdBundlesActive ? debugIdBundlesArtifactsLoading : artifactsLoading
  212. }
  213. >
  214. {tabDebugIdBundlesActive
  215. ? (debugIdBundlesArtifactsData?.files ?? []).map(data => {
  216. const downloadUrl = `${api.baseUrl}/projects/${organization.slug}/${
  217. project.slug
  218. }/artifact-bundles/${encodeURIComponent(params.bundleId)}/files/${
  219. data.id
  220. }/?download=1`;
  221. return (
  222. <ArtifactsTableRow
  223. key={data.id}
  224. size={data.fileSize}
  225. name={data.filePath}
  226. downloadRole={organization.debugFilesRole}
  227. downloadUrl={downloadUrl}
  228. orgSlug={organization.slug}
  229. artifactColumnDetails={
  230. <DebugIdAndFileTypeWrapper>
  231. <div>{data.debugId}</div>
  232. <div>{debugIdBundleTypeLabels[data.fileType]}</div>
  233. </DebugIdAndFileTypeWrapper>
  234. }
  235. />
  236. );
  237. })
  238. : artifactsData?.map(data => {
  239. const downloadUrl = `${api.baseUrl}/projects/${organization.slug}/${
  240. project.slug
  241. }/releases/${encodeURIComponent(params.bundleId)}/files/${
  242. data.id
  243. }/?download=1`;
  244. return (
  245. <ArtifactsTableRow
  246. key={data.id}
  247. size={data.size}
  248. name={data.name}
  249. downloadRole={organization.debugFilesRole}
  250. downloadUrl={downloadUrl}
  251. orgSlug={organization.slug}
  252. artifactColumnDetails={
  253. <TimeAndDistWrapper>
  254. <TimeWrapper>
  255. <IconClock size="sm" />
  256. <TimeSince date={data.dateCreated} />
  257. </TimeWrapper>
  258. <StyledTag
  259. type={data.dist ? 'info' : undefined}
  260. tooltipText={data.dist ? undefined : t('No distribution set')}
  261. >
  262. {data.dist ?? t('none')}
  263. </StyledTag>
  264. </TimeAndDistWrapper>
  265. }
  266. />
  267. );
  268. })}
  269. </StyledPanelTable>
  270. <Pagination
  271. pageLinks={
  272. tabDebugIdBundlesActive
  273. ? debugIdBundlesArtifactsHeaders?.('Link') ?? ''
  274. : artifactsHeaders?.('Link') ?? ''
  275. }
  276. />
  277. </Fragment>
  278. );
  279. }
  280. const StyledPanelTable = styled(PanelTable)`
  281. grid-template-columns: minmax(220px, 1fr) minmax(120px, max-content) minmax(
  282. 74px,
  283. max-content
  284. );
  285. `;
  286. const Column = styled('div')`
  287. display: flex;
  288. align-items: center;
  289. overflow: hidden;
  290. `;
  291. const ActionsColumn = styled(Column)`
  292. justify-content: flex-end;
  293. `;
  294. const SearchBarWithMarginBottom = styled(SearchBar)`
  295. margin-bottom: ${space(3)};
  296. `;
  297. const ArtifactColumn = styled('div')`
  298. overflow-wrap: break-word;
  299. word-break: break-all;
  300. line-height: 140%;
  301. `;
  302. const Name = styled('div')`
  303. display: flex;
  304. justify-content: flex-start;
  305. align-items: center;
  306. `;
  307. const SizeColumn = styled('div')`
  308. display: flex;
  309. justify-content: flex-end;
  310. text-align: right;
  311. align-items: center;
  312. color: ${p => p.theme.subText};
  313. `;
  314. const TimeAndDistWrapper = styled('div')`
  315. width: 100%;
  316. display: flex;
  317. margin-top: ${space(1)};
  318. align-items: center;
  319. `;
  320. const TimeWrapper = styled('div')`
  321. display: grid;
  322. gap: ${space(0.5)};
  323. grid-template-columns: min-content 1fr;
  324. font-size: ${p => p.theme.fontSizeMedium};
  325. align-items: center;
  326. color: ${p => p.theme.subText};
  327. `;
  328. const StyledTag = styled(Tag)`
  329. margin-left: ${space(1)};
  330. `;
  331. const DebugIdAndFileTypeWrapper = styled('div')`
  332. font-size: ${p => p.theme.fontSizeSmall};
  333. color: ${p => p.theme.subText};
  334. `;
  335. const VersionAndDetails = styled('div')`
  336. display: flex;
  337. flex-direction: column;
  338. gap: ${space(1)};
  339. word-break: break-word;
  340. `;