sourceMapsDetails.tsx 12 KB

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