index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import * as ReactRouter from 'react-router';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import pick from 'lodash/pick';
  5. import {Client} from 'app/api';
  6. import Feature from 'app/components/acl/feature';
  7. import Alert from 'app/components/alert';
  8. import Breadcrumbs from 'app/components/breadcrumbs';
  9. import DropdownControl, {DropdownItem} from 'app/components/dropdownControl';
  10. import * as Layout from 'app/components/layouts/thirds';
  11. import LightWeightNoProjectMessage from 'app/components/lightWeightNoProjectMessage';
  12. import SearchBar from 'app/components/searchBar';
  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 {decodeScalar} from 'app/utils/queryString';
  18. import withApi from 'app/utils/withApi';
  19. import withOrganization from 'app/utils/withOrganization';
  20. import AsyncView from 'app/views/asyncView';
  21. import {DashboardListItem} from '../types';
  22. import DashboardList from './dashboardList';
  23. const SORT_OPTIONS: SelectValue<string>[] = [
  24. {label: t('My Dashboards'), value: 'mydashboards'},
  25. {label: t('Dashboard Name (A-Z)'), value: 'title'},
  26. {label: t('Date Created (Newest)'), value: '-dateCreated'},
  27. {label: t('Date Created (Oldest)'), value: 'dateCreated'},
  28. ];
  29. type Props = {
  30. api: Client;
  31. organization: Organization;
  32. location: Location;
  33. router: ReactRouter.InjectedRouter;
  34. } & AsyncView['props'];
  35. type State = {
  36. dashboards: DashboardListItem[] | null;
  37. dashboardsPageLinks: string;
  38. } & AsyncView['state'];
  39. class ManageDashboards extends AsyncView<Props, State> {
  40. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  41. const {organization, location} = this.props;
  42. return [
  43. [
  44. 'dashboards',
  45. `/organizations/${organization.slug}/dashboards/`,
  46. {
  47. query: {
  48. ...pick(location.query, ['cursor', 'query']),
  49. sort: this.getActiveSort().value,
  50. per_page: '9',
  51. },
  52. },
  53. ],
  54. ];
  55. }
  56. getActiveSort() {
  57. const {location} = this.props;
  58. const urlSort = decodeScalar(location.query.sort, 'mydashboards');
  59. return SORT_OPTIONS.find(item => item.value === urlSort) || SORT_OPTIONS[0];
  60. }
  61. onDashboardsChange() {
  62. this.reloadData();
  63. }
  64. handleSearch(query: string) {
  65. const {location, router} = this.props;
  66. router.push({
  67. pathname: location.pathname,
  68. query: {...location.query, cursor: undefined, query},
  69. });
  70. }
  71. handleSortChange = (value: string) => {
  72. const {location} = this.props;
  73. browserHistory.push({
  74. pathname: location.pathname,
  75. query: {
  76. ...location.query,
  77. cursor: undefined,
  78. sort: value,
  79. },
  80. });
  81. };
  82. getQuery() {
  83. const {query} = this.props.location.query;
  84. return typeof query === 'string' ? query : undefined;
  85. }
  86. renderActions() {
  87. const activeSort = this.getActiveSort();
  88. return (
  89. <StyledActions>
  90. <StyledSearchBar
  91. defaultQuery=""
  92. query={this.getQuery()}
  93. placeholder={t('Search Dashboards')}
  94. onSearch={query => this.handleSearch(query)}
  95. />
  96. <StyledDropdownControl
  97. buttonProps={{prefix: t('Sort By')}}
  98. label={activeSort.label}
  99. >
  100. {SORT_OPTIONS.map(({label, value}) => (
  101. <DropdownItem
  102. key={value}
  103. onSelect={this.handleSortChange}
  104. eventKey={value}
  105. isActive={value === activeSort.value}
  106. >
  107. {label}
  108. </DropdownItem>
  109. ))}
  110. </StyledDropdownControl>
  111. </StyledActions>
  112. );
  113. }
  114. renderNoAccess() {
  115. return (
  116. <PageContent>
  117. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  118. </PageContent>
  119. );
  120. }
  121. renderDashboards() {
  122. const {dashboards, dashboardsPageLinks} = this.state;
  123. const {organization, location, api} = this.props;
  124. return (
  125. <DashboardList
  126. api={api}
  127. dashboards={dashboards}
  128. organization={organization}
  129. pageLinks={dashboardsPageLinks}
  130. location={location}
  131. onDashboardsChange={() => this.onDashboardsChange()}
  132. />
  133. );
  134. }
  135. getTitle() {
  136. return t('Manage Dashboards');
  137. }
  138. renderBody() {
  139. const {organization} = this.props;
  140. return (
  141. <Feature
  142. organization={organization}
  143. features={['dashboards-manage']}
  144. renderDisabled={this.renderNoAccess}
  145. >
  146. <LightWeightNoProjectMessage organization={organization}>
  147. <Layout.Header>
  148. <Layout.HeaderContent>
  149. <Breadcrumbs
  150. crumbs={[
  151. {
  152. label: 'Dashboards',
  153. to: `/organizations/${organization.slug}/dashboards/`,
  154. },
  155. {
  156. label: 'Manage Dashboards',
  157. },
  158. ]}
  159. />
  160. <Layout.Title>{t('Manage Dashboards')}</Layout.Title>
  161. </Layout.HeaderContent>
  162. </Layout.Header>
  163. <Layout.Body>
  164. <Layout.Main fullWidth>
  165. {this.renderActions()}
  166. {this.renderDashboards()}
  167. </Layout.Main>
  168. </Layout.Body>
  169. </LightWeightNoProjectMessage>
  170. </Feature>
  171. );
  172. }
  173. }
  174. const StyledSearchBar = styled(SearchBar)`
  175. flex-grow: 1;
  176. margin-right: ${space(2)};
  177. margin-bottom: ${space(2)};
  178. `;
  179. const StyledDropdownControl = styled(DropdownControl)`
  180. margin-bottom: ${space(2)};
  181. `;
  182. const StyledActions = styled('div')`
  183. display: grid;
  184. grid-template-columns: auto max-content min-content;
  185. width: 100%;
  186. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  187. grid-template-columns: auto;
  188. }
  189. margin-bottom: ${space(1)};
  190. align-items: center;
  191. `;
  192. export default withApi(withOrganization(ManageDashboards));