index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {browserHistory, InjectedRouter} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import pick from 'lodash/pick';
  4. import {Client} from 'app/api';
  5. import Feature from 'app/components/acl/feature';
  6. import Alert from 'app/components/alert';
  7. import Button from 'app/components/button';
  8. import DropdownControl, {DropdownItem} from 'app/components/dropdownControl';
  9. import LightWeightNoProjectMessage from 'app/components/lightWeightNoProjectMessage';
  10. import SearchBar from 'app/components/searchBar';
  11. import SentryDocumentTitle from 'app/components/sentryDocumentTitle';
  12. import {IconAdd} from 'app/icons';
  13. import {t} from 'app/locale';
  14. import {PageContent} from 'app/styles/organization';
  15. import space from 'app/styles/space';
  16. import {Organization, SelectValue} from 'app/types';
  17. import {trackAnalyticsEvent} from 'app/utils/analytics';
  18. import {decodeScalar} from 'app/utils/queryString';
  19. import withApi from 'app/utils/withApi';
  20. import withOrganization from 'app/utils/withOrganization';
  21. import AsyncView from 'app/views/asyncView';
  22. import {DashboardListItem} from '../types';
  23. import DashboardList from './dashboardList';
  24. const SORT_OPTIONS: SelectValue<string>[] = [
  25. {label: t('My Dashboards'), value: 'mydashboards'},
  26. {label: t('Dashboard Name (A-Z)'), value: 'title'},
  27. {label: t('Date Created (Newest)'), value: '-dateCreated'},
  28. {label: t('Date Created (Oldest)'), value: 'dateCreated'},
  29. ];
  30. type Props = {
  31. api: Client;
  32. organization: Organization;
  33. location: Location;
  34. router: InjectedRouter;
  35. } & AsyncView['props'];
  36. type State = {
  37. dashboards: DashboardListItem[] | null;
  38. dashboardsPageLinks: string;
  39. } & AsyncView['state'];
  40. class ManageDashboards extends AsyncView<Props, State> {
  41. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  42. const {organization, location} = this.props;
  43. return [
  44. [
  45. 'dashboards',
  46. `/organizations/${organization.slug}/dashboards/`,
  47. {
  48. query: {
  49. ...pick(location.query, ['cursor', 'query']),
  50. sort: this.getActiveSort().value,
  51. per_page: '9',
  52. },
  53. },
  54. ],
  55. ];
  56. }
  57. getActiveSort() {
  58. const {location} = this.props;
  59. const urlSort = decodeScalar(location.query.sort, 'mydashboards');
  60. return SORT_OPTIONS.find(item => item.value === urlSort) || SORT_OPTIONS[0];
  61. }
  62. onDashboardsChange() {
  63. this.reloadData();
  64. }
  65. handleSearch(query: string) {
  66. const {location, router} = this.props;
  67. trackAnalyticsEvent({
  68. eventKey: 'dashboards_manage.search',
  69. eventName: 'Dashboards Manager: Search',
  70. organization_id: parseInt(this.props.organization.id, 10),
  71. });
  72. router.push({
  73. pathname: location.pathname,
  74. query: {...location.query, cursor: undefined, query},
  75. });
  76. }
  77. handleSortChange = (value: string) => {
  78. const {location} = this.props;
  79. trackAnalyticsEvent({
  80. eventKey: 'dashboards_manage.change_sort',
  81. eventName: 'Dashboards Manager: Sort By Changed',
  82. organization_id: parseInt(this.props.organization.id, 10),
  83. sort: value,
  84. });
  85. browserHistory.push({
  86. pathname: location.pathname,
  87. query: {
  88. ...location.query,
  89. cursor: undefined,
  90. sort: value,
  91. },
  92. });
  93. };
  94. getQuery() {
  95. const {query} = this.props.location.query;
  96. return typeof query === 'string' ? query : undefined;
  97. }
  98. renderActions() {
  99. const activeSort = this.getActiveSort();
  100. return (
  101. <StyledActions>
  102. <SearchBar
  103. defaultQuery=""
  104. query={this.getQuery()}
  105. placeholder={t('Search Dashboards')}
  106. onSearch={query => this.handleSearch(query)}
  107. />
  108. <DropdownControl buttonProps={{prefix: t('Sort By')}} label={activeSort.label}>
  109. {SORT_OPTIONS.map(({label, value}) => (
  110. <DropdownItem
  111. key={value}
  112. onSelect={this.handleSortChange}
  113. eventKey={value}
  114. isActive={value === activeSort.value}
  115. >
  116. {label}
  117. </DropdownItem>
  118. ))}
  119. </DropdownControl>
  120. </StyledActions>
  121. );
  122. }
  123. renderNoAccess() {
  124. return (
  125. <PageContent>
  126. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  127. </PageContent>
  128. );
  129. }
  130. renderDashboards() {
  131. const {dashboards, dashboardsPageLinks} = this.state;
  132. const {organization, location, api} = this.props;
  133. return (
  134. <DashboardList
  135. api={api}
  136. dashboards={dashboards}
  137. organization={organization}
  138. pageLinks={dashboardsPageLinks}
  139. location={location}
  140. onDashboardsChange={() => this.onDashboardsChange()}
  141. />
  142. );
  143. }
  144. getTitle() {
  145. return t('Dashboards');
  146. }
  147. onCreate() {
  148. const {organization, location} = this.props;
  149. trackAnalyticsEvent({
  150. eventKey: 'dashboards_manage.create.start',
  151. eventName: 'Dashboards Manager: Dashboard Create Started',
  152. organization_id: parseInt(organization.id, 10),
  153. });
  154. browserHistory.push({
  155. pathname: `/organizations/${organization.slug}/dashboards/new/`,
  156. query: location.query,
  157. });
  158. }
  159. renderBody() {
  160. const {organization} = this.props;
  161. return (
  162. <Feature
  163. organization={organization}
  164. features={['dashboards-edit']}
  165. renderDisabled={this.renderNoAccess}
  166. >
  167. <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
  168. <StyledPageContent>
  169. <LightWeightNoProjectMessage organization={organization}>
  170. <PageContent>
  171. <StyledPageHeader>
  172. {t('Dashboards')}
  173. <Button
  174. data-test-id="dashboard-create"
  175. onClick={event => {
  176. event.preventDefault();
  177. this.onCreate();
  178. }}
  179. priority="primary"
  180. icon={<IconAdd size="xs" isCircled />}
  181. >
  182. {t('Create Dashboard')}
  183. </Button>
  184. </StyledPageHeader>
  185. {this.renderActions()}
  186. {this.renderDashboards()}
  187. </PageContent>
  188. </LightWeightNoProjectMessage>
  189. </StyledPageContent>
  190. </SentryDocumentTitle>
  191. </Feature>
  192. );
  193. }
  194. }
  195. const StyledPageContent = styled(PageContent)`
  196. padding: 0;
  197. `;
  198. const StyledPageHeader = styled('div')`
  199. display: flex;
  200. align-items: flex-end;
  201. font-size: ${p => p.theme.headerFontSize};
  202. color: ${p => p.theme.textColor};
  203. justify-content: space-between;
  204. margin-bottom: ${space(2)};
  205. `;
  206. const StyledActions = styled('div')`
  207. display: grid;
  208. grid-template-columns: auto max-content;
  209. grid-gap: ${space(2)};
  210. margin-bottom: ${space(2)};
  211. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  212. grid-template-columns: auto;
  213. }
  214. `;
  215. export default withApi(withOrganization(ManageDashboards));