projectSourceMapsArtifacts.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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';
  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 {Project} from 'sentry/types';
  18. import {useQuery} 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. type Props = RouteComponentProps<{bundleId: string}, {}> & {
  23. project: Project;
  24. tab: 'release' | 'debug-id';
  25. };
  26. export function ProjectSourceMapsArtifacts({
  27. params,
  28. location,
  29. router,
  30. project,
  31. tab,
  32. }: Props) {
  33. const api = useApi();
  34. const organization = useOrganization();
  35. const tabDebugIdBundlesActive = tab === 'debug-id';
  36. const query = decodeScalar(location.query.query);
  37. const cursor = location.query.cursor ?? '';
  38. const downloadRole = organization.debugFilesRole;
  39. const artifactsEndpoint = `/projects/${organization.slug}/${
  40. project.slug
  41. }/releases/${encodeURIComponent(params.bundleId)}/files/`;
  42. const debugIdBundlesEndpoint = ``;
  43. const {data: artifactsData, isLoading: artifactsLoading} = useQuery(
  44. [
  45. artifactsEndpoint,
  46. {
  47. query: {query, cursor},
  48. },
  49. ],
  50. () => {
  51. return api.requestPromise(artifactsEndpoint, {
  52. query: {query, cursor},
  53. includeAllArgs: true,
  54. });
  55. },
  56. {
  57. staleTime: 0,
  58. keepPreviousData: true,
  59. enabled: !tabDebugIdBundlesActive,
  60. }
  61. );
  62. const {data: debugIdBundlesData, isLoading: debugIdBundlesLoading} = useQuery(
  63. [
  64. debugIdBundlesEndpoint,
  65. {
  66. query: {query, cursor},
  67. },
  68. ],
  69. () => {
  70. return api.requestPromise(debugIdBundlesEndpoint, {
  71. query: {query, cursor},
  72. includeAllArgs: true,
  73. });
  74. },
  75. {
  76. staleTime: 0,
  77. keepPreviousData: true,
  78. enabled: tabDebugIdBundlesActive,
  79. }
  80. );
  81. const data = tabDebugIdBundlesActive
  82. ? debugIdBundlesData?.[0] ?? []
  83. : artifactsData?.[0] ?? [];
  84. const pageLinks = tabDebugIdBundlesActive
  85. ? debugIdBundlesData?.[2]?.getResponseHeader('Link') ?? ''
  86. : artifactsData?.[2]?.getResponseHeader('Link') ?? '';
  87. const loading = tabDebugIdBundlesActive ? debugIdBundlesLoading : artifactsLoading;
  88. const handleSearch = useCallback(
  89. (newQuery: string) => {
  90. router.push({
  91. ...location,
  92. query: {...location.query, cursor: undefined, query: newQuery},
  93. });
  94. },
  95. [router, location]
  96. );
  97. return (
  98. <Fragment>
  99. <SearchBarWithMarginBottom
  100. placeholder={t('Search')}
  101. onSearch={handleSearch}
  102. query={query}
  103. />
  104. <StyledPanelTable
  105. headers={[
  106. t('Artifact'),
  107. <SizeColumn key="file-size">{t('File Size')}</SizeColumn>,
  108. '',
  109. ]}
  110. emptyMessage={
  111. query
  112. ? t('No artifacts match your search query.')
  113. : t('There are no artifacts in this archive.')
  114. }
  115. isEmpty={data.length === 0}
  116. isLoading={loading}
  117. >
  118. {data.map(({id, size, name, dist, dateCreated}) => {
  119. const downloadUrl = `${api.baseUrl}/projects/${organization.slug}/${
  120. project.slug
  121. }/releases/${encodeURIComponent(name)}/files/${id}/?download=1`;
  122. return (
  123. <Fragment key={id}>
  124. <NameColumn>
  125. <Name>{name || `(${t('empty')})`}</Name>
  126. <TimeAndDistWrapper>
  127. <TimeWrapper>
  128. <IconClock size="sm" />
  129. <TimeSince date={dateCreated} />
  130. </TimeWrapper>
  131. <StyledTag
  132. type={dist ? 'info' : undefined}
  133. tooltipText={dist ? undefined : t('No distribution set')}
  134. >
  135. {dist ?? t('none')}
  136. </StyledTag>
  137. </TimeAndDistWrapper>
  138. </NameColumn>
  139. <SizeColumn>
  140. <FileSize bytes={size} />
  141. </SizeColumn>
  142. <ActionsColumn>
  143. <Role role={downloadRole}>
  144. {({hasRole}) => (
  145. <Tooltip
  146. title={tct(
  147. 'Artifacts can only be downloaded by users with organization [downloadRole] role[orHigher]. This can be changed in [settingsLink:Debug Files Access] settings.',
  148. {
  149. downloadRole,
  150. orHigher: downloadRole !== 'owner' ? ` ${t('or higher')}` : '',
  151. settingsLink: (
  152. <Link to={`/settings/${organization.slug}/#debugFilesRole`} />
  153. ),
  154. }
  155. )}
  156. disabled={hasRole}
  157. isHoverable
  158. >
  159. <Button
  160. size="sm"
  161. icon={<IconDownload size="sm" />}
  162. disabled={!hasRole}
  163. href={downloadUrl}
  164. title={hasRole ? t('Download Artifact') : undefined}
  165. aria-label={t('Download Artifact')}
  166. />
  167. </Tooltip>
  168. )}
  169. </Role>
  170. </ActionsColumn>
  171. </Fragment>
  172. );
  173. })}
  174. </StyledPanelTable>
  175. <Pagination pageLinks={pageLinks} />
  176. </Fragment>
  177. );
  178. }
  179. const StyledPanelTable = styled(PanelTable)`
  180. grid-template-columns: minmax(220px, 1fr) minmax(120px, max-content) minmax(
  181. 74px,
  182. max-content
  183. );
  184. `;
  185. const Column = styled('div')`
  186. display: flex;
  187. align-items: center;
  188. overflow: hidden;
  189. `;
  190. const ActionsColumn = styled(Column)`
  191. justify-content: flex-end;
  192. `;
  193. const SearchBarWithMarginBottom = styled(SearchBar)`
  194. margin-bottom: ${space(3)};
  195. `;
  196. const SizeColumn = styled('div')`
  197. display: flex;
  198. justify-content: flex-end;
  199. text-align: right;
  200. align-items: center;
  201. `;
  202. const Name = styled('div')`
  203. padding-right: ${space(4)};
  204. overflow-wrap: break-word;
  205. word-break: break-all;
  206. `;
  207. const TimeAndDistWrapper = styled('div')`
  208. width: 100%;
  209. display: flex;
  210. margin-top: ${space(1)};
  211. align-items: center;
  212. `;
  213. const TimeWrapper = styled('div')`
  214. display: grid;
  215. gap: ${space(0.5)};
  216. grid-template-columns: min-content 1fr;
  217. font-size: ${p => p.theme.fontSizeMedium};
  218. align-items: center;
  219. color: ${p => p.theme.subText};
  220. `;
  221. const StyledTag = styled(Tag)`
  222. margin-left: ${space(1)};
  223. `;
  224. const NameColumn = styled('div')`
  225. display: flex;
  226. flex-direction: column;
  227. align-items: flex-start;
  228. justify-content: center;
  229. `;