sourceMapsDetails.tsx 12 KB

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