index.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {
  5. addErrorMessage,
  6. addLoadingMessage,
  7. addSuccessMessage,
  8. } from 'sentry/actionCreators/indicator';
  9. import Access from 'sentry/components/acl/access';
  10. import {Button} from 'sentry/components/button';
  11. import ButtonBar from 'sentry/components/buttonBar';
  12. import Confirm from 'sentry/components/confirm';
  13. import Pagination from 'sentry/components/pagination';
  14. import {PanelTable} from 'sentry/components/panels';
  15. import SearchBar from 'sentry/components/searchBar';
  16. import TextOverflow from 'sentry/components/textOverflow';
  17. import {Tooltip} from 'sentry/components/tooltip';
  18. import Version from 'sentry/components/version';
  19. import {IconDelete} from 'sentry/icons';
  20. import {t} from 'sentry/locale';
  21. import {space} from 'sentry/styles/space';
  22. import {Artifact, Organization, Project} from 'sentry/types';
  23. import {formatVersion} from 'sentry/utils/formatters';
  24. import {decodeScalar} from 'sentry/utils/queryString';
  25. import routeTitleGen from 'sentry/utils/routeTitle';
  26. import AsyncView from 'sentry/views/asyncView';
  27. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  28. import SourceMapsArtifactRow from './sourceMapsArtifactRow';
  29. type RouteParams = {name: string; projectId: string};
  30. type Props = RouteComponentProps<RouteParams, {}> & {
  31. organization: Organization;
  32. project: Project;
  33. };
  34. type State = AsyncView['state'] & {
  35. artifacts: Artifact[];
  36. };
  37. class ProjectSourceMapsDetail extends AsyncView<Props, State> {
  38. getTitle() {
  39. const {projectId, name} = this.props.params;
  40. return routeTitleGen(t('Archive %s', formatVersion(name)), projectId, false);
  41. }
  42. getDefaultState(): State {
  43. return {
  44. ...super.getDefaultState(),
  45. artifacts: [],
  46. };
  47. }
  48. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  49. return [['artifacts', this.getArtifactsUrl(), {query: {query: this.getQuery()}}]];
  50. }
  51. getArtifactsUrl() {
  52. const {organization} = this.props;
  53. const {projectId, name} = this.props.params;
  54. return `/projects/${organization.slug}/${projectId}/releases/${encodeURIComponent(
  55. name
  56. )}/files/`;
  57. }
  58. handleSearch = (query: string) => {
  59. const {location, router} = this.props;
  60. router.push({
  61. ...location,
  62. query: {...location.query, cursor: undefined, query},
  63. });
  64. };
  65. handleArtifactDelete = async (id: string) => {
  66. addLoadingMessage(t('Removing artifact\u2026'));
  67. try {
  68. await this.api.requestPromise(`${this.getArtifactsUrl()}${id}/`, {
  69. method: 'DELETE',
  70. });
  71. this.fetchData();
  72. addSuccessMessage(t('Artifact removed.'));
  73. } catch {
  74. addErrorMessage(t('Unable to remove artifact. Please try again.'));
  75. }
  76. };
  77. handleArchiveDelete = async () => {
  78. const {organization} = this.props;
  79. const {projectId, name} = this.props.params;
  80. addLoadingMessage(t('Removing artifacts\u2026'));
  81. try {
  82. await this.api.requestPromise(
  83. `/projects/${organization.slug}/${projectId}/files/source-maps/`,
  84. {
  85. method: 'DELETE',
  86. query: {name},
  87. }
  88. );
  89. this.fetchData();
  90. addSuccessMessage(t('Artifacts removed.'));
  91. } catch {
  92. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  93. }
  94. };
  95. getQuery() {
  96. const {query} = this.props.location.query;
  97. return decodeScalar(query);
  98. }
  99. getEmptyMessage() {
  100. if (this.getQuery()) {
  101. return t('There are no artifacts that match your search.');
  102. }
  103. return t('There are no artifacts in this archive.');
  104. }
  105. renderLoading() {
  106. return this.renderBody();
  107. }
  108. renderArtifacts() {
  109. const {organization} = this.props;
  110. const {artifacts} = this.state;
  111. const artifactApiUrl = this.api.baseUrl + this.getArtifactsUrl();
  112. if (!artifacts.length) {
  113. return null;
  114. }
  115. return artifacts.map(artifact => {
  116. return (
  117. <SourceMapsArtifactRow
  118. key={artifact.id}
  119. artifact={artifact}
  120. onDelete={this.handleArtifactDelete}
  121. downloadUrl={`${artifactApiUrl}${artifact.id}/?download=1`}
  122. downloadRole={organization.debugFilesRole}
  123. orgSlug={organization.slug}
  124. />
  125. );
  126. });
  127. }
  128. renderBody() {
  129. const {loading, artifacts, artifactsPageLinks} = this.state;
  130. const {name} = this.props.params;
  131. const {project, organization} = this.props;
  132. return (
  133. <Fragment>
  134. <StyledSettingsPageHeader
  135. title={
  136. <Title>
  137. {t('Archive')}&nbsp;
  138. <TextOverflow>
  139. <Version version={name} tooltipRawVersion anchor={false} truncate />
  140. </TextOverflow>
  141. </Title>
  142. }
  143. action={
  144. <StyledButtonBar gap={1}>
  145. <ReleaseButton
  146. to={`/organizations/${organization.slug}/releases/${encodeURIComponent(
  147. name
  148. )}/?project=${project.id}`}
  149. >
  150. {t('Go to Release')}
  151. </ReleaseButton>
  152. <Access access={['project:releases']}>
  153. {({hasAccess}) => (
  154. <Tooltip
  155. disabled={hasAccess}
  156. title={t('You do not have permission to delete artifacts.')}
  157. >
  158. <Confirm
  159. message={t(
  160. 'Are you sure you want to remove all artifacts in this archive?'
  161. )}
  162. onConfirm={this.handleArchiveDelete}
  163. disabled={!hasAccess}
  164. >
  165. <Button
  166. icon={<IconDelete size="sm" />}
  167. title={t('Remove All Artifacts')}
  168. aria-label={t('Remove All Artifacts')}
  169. disabled={!hasAccess}
  170. />
  171. </Confirm>
  172. </Tooltip>
  173. )}
  174. </Access>
  175. <SearchBar
  176. placeholder={t('Filter artifacts')}
  177. onSearch={this.handleSearch}
  178. query={this.getQuery()}
  179. />
  180. </StyledButtonBar>
  181. }
  182. />
  183. <StyledPanelTable
  184. headers={[
  185. t('Artifact'),
  186. <SizeColumn key="size">{t('File Size')}</SizeColumn>,
  187. '',
  188. ]}
  189. emptyMessage={this.getEmptyMessage()}
  190. isEmpty={artifacts.length === 0}
  191. isLoading={loading}
  192. >
  193. {this.renderArtifacts()}
  194. </StyledPanelTable>
  195. <Pagination pageLinks={artifactsPageLinks} />
  196. </Fragment>
  197. );
  198. }
  199. }
  200. const StyledSettingsPageHeader = styled(SettingsPageHeader)`
  201. /*
  202. ugly selector to make header work on mobile
  203. we can refactor this once we start making other settings more responsive
  204. */
  205. > div {
  206. @media (max-width: ${p => p.theme.breakpoints.large}) {
  207. display: block;
  208. }
  209. > div {
  210. min-width: 0;
  211. @media (max-width: ${p => p.theme.breakpoints.large}) {
  212. margin-bottom: ${space(2)};
  213. }
  214. }
  215. }
  216. `;
  217. const Title = styled('div')`
  218. display: flex;
  219. align-items: center;
  220. `;
  221. const StyledButtonBar = styled(ButtonBar)`
  222. justify-content: flex-start;
  223. `;
  224. const StyledPanelTable = styled(PanelTable)`
  225. grid-template-columns: minmax(220px, 1fr) max-content 120px;
  226. `;
  227. const ReleaseButton = styled(Button)`
  228. white-space: nowrap;
  229. `;
  230. const SizeColumn = styled('div')`
  231. text-align: right;
  232. `;
  233. export default ProjectSourceMapsDetail;