index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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; orgId: 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 {orgId, projectId, name} = this.props.params;
  53. return `/projects/${orgId}/${projectId}/releases/${encodeURIComponent(name)}/files/`;
  54. }
  55. handleSearch = (query: string) => {
  56. const {location, router} = this.props;
  57. router.push({
  58. ...location,
  59. query: {...location.query, cursor: undefined, query},
  60. });
  61. };
  62. handleArtifactDelete = async (id: string) => {
  63. addLoadingMessage(t('Removing artifact\u2026'));
  64. try {
  65. await this.api.requestPromise(`${this.getArtifactsUrl()}${id}/`, {
  66. method: 'DELETE',
  67. });
  68. this.fetchData();
  69. addSuccessMessage(t('Artifact removed.'));
  70. } catch {
  71. addErrorMessage(t('Unable to remove artifact. Please try again.'));
  72. }
  73. };
  74. handleArchiveDelete = async () => {
  75. const {orgId, projectId, name} = this.props.params;
  76. addLoadingMessage(t('Removing artifacts\u2026'));
  77. try {
  78. await this.api.requestPromise(
  79. `/projects/${orgId}/${projectId}/files/source-maps/`,
  80. {
  81. method: 'DELETE',
  82. query: {name},
  83. }
  84. );
  85. this.fetchData();
  86. addSuccessMessage(t('Artifacts removed.'));
  87. } catch {
  88. addErrorMessage(t('Unable to remove artifacts. Please try again.'));
  89. }
  90. };
  91. getQuery() {
  92. const {query} = this.props.location.query;
  93. return decodeScalar(query);
  94. }
  95. getEmptyMessage() {
  96. if (this.getQuery()) {
  97. return t('There are no artifacts that match your search.');
  98. }
  99. return t('There are no artifacts in this archive.');
  100. }
  101. renderLoading() {
  102. return this.renderBody();
  103. }
  104. renderArtifacts() {
  105. const {organization} = this.props;
  106. const {artifacts} = this.state;
  107. const artifactApiUrl = this.api.baseUrl + this.getArtifactsUrl();
  108. if (!artifacts.length) {
  109. return null;
  110. }
  111. return artifacts.map(artifact => {
  112. return (
  113. <SourceMapsArtifactRow
  114. key={artifact.id}
  115. artifact={artifact}
  116. onDelete={this.handleArtifactDelete}
  117. downloadUrl={`${artifactApiUrl}${artifact.id}/?download=1`}
  118. downloadRole={organization.debugFilesRole}
  119. orgSlug={organization.slug}
  120. />
  121. );
  122. });
  123. }
  124. renderBody() {
  125. const {loading, artifacts, artifactsPageLinks} = this.state;
  126. const {name, orgId} = this.props.params;
  127. const {project} = this.props;
  128. return (
  129. <Fragment>
  130. <StyledSettingsPageHeader
  131. title={
  132. <Title>
  133. {t('Archive')}&nbsp;
  134. <TextOverflow>
  135. <Version version={name} tooltipRawVersion anchor={false} truncate />
  136. </TextOverflow>
  137. </Title>
  138. }
  139. action={
  140. <StyledButtonBar gap={1}>
  141. <ReleaseButton
  142. to={`/organizations/${orgId}/releases/${encodeURIComponent(
  143. name
  144. )}/?project=${project.id}`}
  145. >
  146. {t('Go to Release')}
  147. </ReleaseButton>
  148. <Access access={['project:releases']}>
  149. {({hasAccess}) => (
  150. <Tooltip
  151. disabled={hasAccess}
  152. title={t('You do not have permission to delete artifacts.')}
  153. >
  154. <Confirm
  155. message={t(
  156. 'Are you sure you want to remove all artifacts in this archive?'
  157. )}
  158. onConfirm={this.handleArchiveDelete}
  159. disabled={!hasAccess}
  160. >
  161. <Button
  162. icon={<IconDelete size="sm" />}
  163. title={t('Remove All Artifacts')}
  164. aria-label={t('Remove All Artifacts')}
  165. disabled={!hasAccess}
  166. />
  167. </Confirm>
  168. </Tooltip>
  169. )}
  170. </Access>
  171. <SearchBar
  172. placeholder={t('Filter artifacts')}
  173. onSearch={this.handleSearch}
  174. query={this.getQuery()}
  175. />
  176. </StyledButtonBar>
  177. }
  178. />
  179. <StyledPanelTable
  180. headers={[
  181. t('Artifact'),
  182. <SizeColumn key="size">{t('File Size')}</SizeColumn>,
  183. '',
  184. ]}
  185. emptyMessage={this.getEmptyMessage()}
  186. isEmpty={artifacts.length === 0}
  187. isLoading={loading}
  188. >
  189. {this.renderArtifacts()}
  190. </StyledPanelTable>
  191. <Pagination pageLinks={artifactsPageLinks} />
  192. </Fragment>
  193. );
  194. }
  195. }
  196. const StyledSettingsPageHeader = styled(SettingsPageHeader)`
  197. /*
  198. ugly selector to make header work on mobile
  199. we can refactor this once we start making other settings more responsive
  200. */
  201. > div {
  202. @media (max-width: ${p => p.theme.breakpoints.large}) {
  203. display: block;
  204. }
  205. > div {
  206. min-width: 0;
  207. @media (max-width: ${p => p.theme.breakpoints.large}) {
  208. margin-bottom: ${space(2)};
  209. }
  210. }
  211. }
  212. `;
  213. const Title = styled('div')`
  214. display: flex;
  215. align-items: center;
  216. `;
  217. const StyledButtonBar = styled(ButtonBar)`
  218. justify-content: flex-start;
  219. `;
  220. const StyledPanelTable = styled(PanelTable)`
  221. grid-template-columns: minmax(220px, 1fr) max-content 120px;
  222. `;
  223. const ReleaseButton = styled(Button)`
  224. white-space: nowrap;
  225. `;
  226. const SizeColumn = styled('div')`
  227. text-align: right;
  228. `;
  229. export default ProjectSourceMapsDetail;