index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React from 'react';
  2. import * as ReactRouter 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 LightWeightNoProjectMessage from 'app/components/lightWeightNoProjectMessage';
  10. import PageHeading from 'app/components/pageHeading';
  11. import SearchBar from 'app/components/searchBar';
  12. import {t} from 'app/locale';
  13. import {PageContent, PageHeader} from 'app/styles/organization';
  14. import space from 'app/styles/space';
  15. import {Organization} from 'app/types';
  16. import withApi from 'app/utils/withApi';
  17. import withOrganization from 'app/utils/withOrganization';
  18. import AsyncView from 'app/views/asyncView';
  19. import {DashboardListItem} from '../types';
  20. import DashboardList from './dashboardList';
  21. type Props = {
  22. api: Client;
  23. organization: Organization;
  24. location: Location;
  25. router: ReactRouter.InjectedRouter;
  26. } & AsyncView['props'];
  27. type State = {
  28. dashboards: DashboardListItem[] | null;
  29. dashboardsPageLinks: string;
  30. } & AsyncView['state'];
  31. class ManageDashboards extends AsyncView<Props, State> {
  32. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  33. const {organization, location} = this.props;
  34. return [
  35. [
  36. 'dashboards',
  37. `/organizations/${organization.slug}/dashboards/`,
  38. {
  39. query: {
  40. ...pick(location.query, ['cursor', 'query']),
  41. per_page: '9',
  42. },
  43. },
  44. ],
  45. ];
  46. }
  47. onDashboardsChange() {
  48. this.reloadData();
  49. }
  50. handleSearch(query: string) {
  51. const {location, router} = this.props;
  52. router.push({
  53. pathname: location.pathname,
  54. query: {...location.query, cursor: undefined, query},
  55. });
  56. }
  57. getQuery() {
  58. const {query} = this.props.location.query;
  59. return typeof query === 'string' ? query : undefined;
  60. }
  61. renderActions() {
  62. return (
  63. <StyledActions>
  64. <StyledSearchBar
  65. defaultQuery=""
  66. query={this.getQuery()}
  67. placeholder={t('Search Dashboards')}
  68. onSearch={query => this.handleSearch(query)}
  69. />
  70. </StyledActions>
  71. );
  72. }
  73. renderNoAccess() {
  74. return (
  75. <PageContent>
  76. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  77. </PageContent>
  78. );
  79. }
  80. renderDashboards() {
  81. const {dashboards, dashboardsPageLinks} = this.state;
  82. const {organization, location, api} = this.props;
  83. return (
  84. <DashboardList
  85. api={api}
  86. dashboards={dashboards}
  87. organization={organization}
  88. pageLinks={dashboardsPageLinks}
  89. location={location}
  90. onDashboardsChange={() => this.onDashboardsChange()}
  91. />
  92. );
  93. }
  94. getTitle() {
  95. return t('Manage Dashboards');
  96. }
  97. renderBody() {
  98. const {organization} = this.props;
  99. return (
  100. <Feature
  101. organization={organization}
  102. features={['dashboards-manage']}
  103. renderDisabled={this.renderNoAccess}
  104. >
  105. <LightWeightNoProjectMessage organization={organization}>
  106. <PageContent>
  107. <Breadcrumbs
  108. crumbs={[
  109. {
  110. label: 'Dashboards',
  111. to: `/organizations/${organization.slug}/dashboards/`,
  112. },
  113. {
  114. label: 'Manage Dashboards',
  115. },
  116. ]}
  117. />
  118. <PageHeader>
  119. <PageHeading>{t('Manage Dashboards')}</PageHeading>
  120. </PageHeader>
  121. {this.renderActions()}
  122. {this.renderDashboards()}
  123. </PageContent>
  124. </LightWeightNoProjectMessage>
  125. </Feature>
  126. );
  127. }
  128. }
  129. const StyledSearchBar = styled(SearchBar)`
  130. flex-grow: 1;
  131. `;
  132. const StyledActions = styled('div')`
  133. display: grid;
  134. grid-template-columns: auto max-content min-content;
  135. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  136. grid-template-columns: auto;
  137. }
  138. align-items: center;
  139. margin-bottom: ${space(3)};
  140. `;
  141. export default withApi(withOrganization(ManageDashboards));