sourceMapsDetails.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import {Fragment, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import {useRole} from 'sentry/components/acl/useRole';
  4. import {LinkButton} from 'sentry/components/button';
  5. import {Tag} from 'sentry/components/core/badge/tag';
  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. <Tooltip
  267. title={data.dist ? undefined : t('No distribution set')}
  268. skipWrapper
  269. >
  270. <StyledTag type={data.dist ? 'info' : undefined}>
  271. {data.dist ?? t('none')}
  272. </StyledTag>
  273. </Tooltip>
  274. </TimeAndDistWrapper>
  275. }
  276. />
  277. );
  278. })}
  279. </StyledPanelTable>
  280. <Pagination
  281. pageLinks={
  282. isDebugIdBundle
  283. ? (debugIdBundlesArtifactsHeaders?.('Link') ?? '')
  284. : (artifactsHeaders?.('Link') ?? '')
  285. }
  286. />
  287. </Fragment>
  288. );
  289. }
  290. const StyledPanelTable = styled(PanelTable)<{hasTypeColumn: boolean}>`
  291. grid-template-columns: minmax(220px, 1fr) minmax(120px, max-content) minmax(
  292. 74px,
  293. max-content
  294. );
  295. ${p =>
  296. p.hasTypeColumn &&
  297. `
  298. grid-template-columns:
  299. minmax(220px, 1fr) minmax(120px, max-content) minmax(120px, max-content)
  300. minmax(74px, 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 DetailsPanel = styled(Panel)`
  315. padding: ${space(1)} ${space(2)};
  316. `;
  317. const ArtifactColumn = styled('div')`
  318. overflow-wrap: break-word;
  319. word-break: break-all;
  320. line-height: 140%;
  321. display: flex;
  322. flex-direction: column;
  323. justify-content: center;
  324. `;
  325. const Name = styled('div')`
  326. display: flex;
  327. justify-content: flex-start;
  328. align-items: center;
  329. `;
  330. const TypeColumn = styled('div')`
  331. display: flex;
  332. justify-content: flex-end;
  333. text-align: right;
  334. align-items: center;
  335. color: ${p => p.theme.subText};
  336. `;
  337. const SizeColumn = styled('div')`
  338. display: flex;
  339. justify-content: flex-end;
  340. text-align: right;
  341. align-items: center;
  342. color: ${p => p.theme.subText};
  343. `;
  344. const TimeAndDistWrapper = styled('div')`
  345. width: 100%;
  346. display: flex;
  347. margin-top: ${space(1)};
  348. align-items: center;
  349. `;
  350. const TimeWrapper = styled('div')`
  351. display: grid;
  352. gap: ${space(0.5)};
  353. grid-template-columns: min-content 1fr;
  354. font-size: ${p => p.theme.fontSizeMedium};
  355. align-items: center;
  356. color: ${p => p.theme.subText};
  357. `;
  358. const StyledTag = styled(Tag)`
  359. margin-left: ${space(1)};
  360. `;
  361. const SubText = styled('span')`
  362. color: ${p => p.theme.subText};
  363. `;
  364. const VersionAndDetails = styled('div')`
  365. display: flex;
  366. flex-direction: column;
  367. gap: ${space(1)};
  368. word-break: break-word;
  369. `;