sourceMapsDetails.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. import {Fragment, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import {useRole} from 'sentry/components/acl/useRole';
  4. import Tag from 'sentry/components/badge/tag';
  5. import {LinkButton} 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 Panel from 'sentry/components/panels/panel';
  10. import {PanelTable} from 'sentry/components/panels/panelTable';
  11. import SearchBar from 'sentry/components/searchBar';
  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 type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  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 {keepPreviousData, 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. downloadUrl,
  47. size,
  48. type,
  49. orgSlug,
  50. artifactColumnDetails,
  51. }: {
  52. artifactColumnDetails: React.ReactNode;
  53. downloadUrl: string;
  54. name: string;
  55. orgSlug: string;
  56. size: number;
  57. type?: string;
  58. }) {
  59. const {hasRole, roleRequired: downloadRole} = useRole({role: 'debugFilesRole'});
  60. return (
  61. <Fragment>
  62. <ArtifactColumn>
  63. <Name>{name || `(${t('empty')})`}</Name>
  64. {artifactColumnDetails}
  65. </ArtifactColumn>
  66. {type && <TypeColumn>{type}</TypeColumn>}
  67. <SizeColumn>
  68. <FileSize bytes={size} />
  69. </SizeColumn>
  70. <ActionsColumn>
  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. <LinkButton
  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. </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 SourceMapsDetails({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/${params.bundleId}/`
  118. );
  119. const tabDebugIdBundlesActive = location.pathname === debugIdsUrl;
  120. const {
  121. data: artifactsData,
  122. getResponseHeader: artifactsHeaders,
  123. isPending: artifactsLoading,
  124. } = useApiQuery<Artifact[]>(
  125. [
  126. artifactsEndpoint,
  127. {
  128. query: {query, cursor},
  129. },
  130. ],
  131. {
  132. staleTime: 0,
  133. placeholderData: keepPreviousData,
  134. enabled: !tabDebugIdBundlesActive,
  135. }
  136. );
  137. const {
  138. data: debugIdBundlesArtifactsData,
  139. getResponseHeader: debugIdBundlesArtifactsHeaders,
  140. isPending: debugIdBundlesArtifactsLoading,
  141. } = useApiQuery<DebugIdBundleArtifact>(
  142. [
  143. debugIdBundlesArtifactsEndpoint,
  144. {
  145. query: {query, cursor},
  146. },
  147. ],
  148. {
  149. staleTime: 0,
  150. placeholderData: keepPreviousData,
  151. enabled: tabDebugIdBundlesActive,
  152. }
  153. );
  154. const {mutate: deleteDebugIdArtifacts} = useDeleteDebugIdBundle({
  155. onSuccess: () =>
  156. router.push(`/settings/${organization.slug}/projects/${project.slug}/source-maps/`),
  157. });
  158. const handleDeleteDebugIdBundle = useCallback(() => {
  159. if (!debugIdBundlesArtifactsData) {
  160. return;
  161. }
  162. deleteDebugIdArtifacts({
  163. projectSlug: project.slug,
  164. bundleId: debugIdBundlesArtifactsData.bundleId,
  165. });
  166. }, [debugIdBundlesArtifactsData, deleteDebugIdArtifacts, project.slug]);
  167. const handleSearch = useCallback(
  168. (newQuery: string) => {
  169. router.push({
  170. ...location,
  171. query: {...location.query, cursor: undefined, query: newQuery},
  172. });
  173. },
  174. [router, location]
  175. );
  176. return (
  177. <Fragment>
  178. <SettingsPageHeader
  179. title={tabDebugIdBundlesActive ? params.bundleId : t('Release Bundle')}
  180. action={
  181. tabDebugIdBundlesActive && (
  182. <DebugIdBundleDeleteButton size="sm" onDelete={handleDeleteDebugIdBundle} />
  183. )
  184. }
  185. subtitle={
  186. !tabDebugIdBundlesActive && (
  187. <VersionAndDetails>{params.bundleId}</VersionAndDetails>
  188. )
  189. }
  190. />
  191. {tabDebugIdBundlesActive && debugIdBundlesArtifactsData && (
  192. <DetailsPanel>
  193. <DebugIdBundleDetails debugIdBundle={debugIdBundlesArtifactsData} />
  194. </DetailsPanel>
  195. )}
  196. <SearchBarWithMarginBottom
  197. placeholder={
  198. tabDebugIdBundlesActive ? t('Filter by Path or ID') : t('Filter by Path')
  199. }
  200. onSearch={handleSearch}
  201. query={query}
  202. />
  203. <StyledPanelTable
  204. hasTypeColumn={tabDebugIdBundlesActive}
  205. headers={[
  206. t('Artifact'),
  207. ...(tabDebugIdBundlesActive
  208. ? [<TypeColumn key="type">{t('Type')}</TypeColumn>]
  209. : []),
  210. <SizeColumn key="file-size">{t('File Size')}</SizeColumn>,
  211. '',
  212. ]}
  213. emptyMessage={
  214. query
  215. ? t('No artifacts match your search query.')
  216. : t('There are no artifacts in this upload.')
  217. }
  218. isEmpty={
  219. (tabDebugIdBundlesActive
  220. ? debugIdBundlesArtifactsData?.files ?? []
  221. : artifactsData ?? []
  222. ).length === 0
  223. }
  224. isLoading={
  225. tabDebugIdBundlesActive ? debugIdBundlesArtifactsLoading : artifactsLoading
  226. }
  227. >
  228. {tabDebugIdBundlesActive
  229. ? (debugIdBundlesArtifactsData?.files ?? []).map(data => {
  230. const downloadUrl = `${api.baseUrl}/projects/${organization.slug}/${
  231. project.slug
  232. }/artifact-bundles/${encodeURIComponent(params.bundleId)}/files/${
  233. data.id
  234. }/?download=1`;
  235. return (
  236. <ArtifactsTableRow
  237. key={data.id}
  238. size={data.fileSize}
  239. name={data.filePath}
  240. type={
  241. debugIdBundleTypeLabels[data.fileType as DebugIdBundleArtifactType]
  242. }
  243. downloadUrl={downloadUrl}
  244. orgSlug={organization.slug}
  245. artifactColumnDetails={
  246. <Fragment>
  247. {data.sourcemap ? (
  248. <div>
  249. <SubText>{t('Sourcemap Reference:')}</SubText> {data.sourcemap}
  250. </div>
  251. ) : null}
  252. {data.debugId ? (
  253. <div>
  254. <SubText>{t('Debug ID:')}</SubText> {data.debugId}
  255. </div>
  256. ) : null}
  257. </Fragment>
  258. }
  259. />
  260. );
  261. })
  262. : artifactsData?.map(data => {
  263. const downloadUrl = `${api.baseUrl}/projects/${organization.slug}/${
  264. project.slug
  265. }/releases/${encodeURIComponent(params.bundleId)}/files/${
  266. data.id
  267. }/?download=1`;
  268. return (
  269. <ArtifactsTableRow
  270. key={data.id}
  271. size={data.size}
  272. name={data.name}
  273. downloadUrl={downloadUrl}
  274. orgSlug={organization.slug}
  275. artifactColumnDetails={
  276. <TimeAndDistWrapper>
  277. <TimeWrapper>
  278. <IconClock size="sm" />
  279. <TimeSince date={data.dateCreated} />
  280. </TimeWrapper>
  281. <StyledTag
  282. type={data.dist ? 'info' : undefined}
  283. tooltipText={data.dist ? undefined : t('No distribution set')}
  284. >
  285. {data.dist ?? t('none')}
  286. </StyledTag>
  287. </TimeAndDistWrapper>
  288. }
  289. />
  290. );
  291. })}
  292. </StyledPanelTable>
  293. <Pagination
  294. pageLinks={
  295. tabDebugIdBundlesActive
  296. ? debugIdBundlesArtifactsHeaders?.('Link') ?? ''
  297. : artifactsHeaders?.('Link') ?? ''
  298. }
  299. />
  300. </Fragment>
  301. );
  302. }
  303. const StyledPanelTable = styled(PanelTable)<{hasTypeColumn: boolean}>`
  304. grid-template-columns: minmax(220px, 1fr) minmax(120px, max-content) minmax(
  305. 74px,
  306. max-content
  307. );
  308. ${p =>
  309. p.hasTypeColumn &&
  310. `
  311. grid-template-columns:
  312. minmax(220px, 1fr) minmax(120px, max-content) minmax(120px, max-content)
  313. minmax(74px, max-content);
  314. `}
  315. `;
  316. const Column = styled('div')`
  317. display: flex;
  318. align-items: center;
  319. overflow: hidden;
  320. `;
  321. const ActionsColumn = styled(Column)`
  322. justify-content: flex-end;
  323. `;
  324. const SearchBarWithMarginBottom = styled(SearchBar)`
  325. margin-bottom: ${space(3)};
  326. `;
  327. const DetailsPanel = styled(Panel)`
  328. padding: ${space(1)} ${space(2)};
  329. `;
  330. const ArtifactColumn = styled('div')`
  331. overflow-wrap: break-word;
  332. word-break: break-all;
  333. line-height: 140%;
  334. display: flex;
  335. flex-direction: column;
  336. justify-content: center;
  337. `;
  338. const Name = styled('div')`
  339. display: flex;
  340. justify-content: flex-start;
  341. align-items: center;
  342. `;
  343. const TypeColumn = styled('div')`
  344. display: flex;
  345. justify-content: flex-end;
  346. text-align: right;
  347. align-items: center;
  348. color: ${p => p.theme.subText};
  349. `;
  350. const SizeColumn = styled('div')`
  351. display: flex;
  352. justify-content: flex-end;
  353. text-align: right;
  354. align-items: center;
  355. color: ${p => p.theme.subText};
  356. `;
  357. const TimeAndDistWrapper = styled('div')`
  358. width: 100%;
  359. display: flex;
  360. margin-top: ${space(1)};
  361. align-items: center;
  362. `;
  363. const TimeWrapper = styled('div')`
  364. display: grid;
  365. gap: ${space(0.5)};
  366. grid-template-columns: min-content 1fr;
  367. font-size: ${p => p.theme.fontSizeMedium};
  368. align-items: center;
  369. color: ${p => p.theme.subText};
  370. `;
  371. const StyledTag = styled(Tag)`
  372. margin-left: ${space(1)};
  373. `;
  374. const SubText = styled('span')`
  375. color: ${p => p.theme.subText};
  376. `;
  377. const VersionAndDetails = styled('div')`
  378. display: flex;
  379. flex-direction: column;
  380. gap: ${space(1)};
  381. word-break: break-word;
  382. `;