index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import {browserHistory, InjectedRouter} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import pick from 'lodash/pick';
  5. import {createDashboard} from 'sentry/actionCreators/dashboards';
  6. import {addSuccessMessage} from 'sentry/actionCreators/indicator';
  7. import {openImportDashboardFromFileModal} from 'sentry/actionCreators/modal';
  8. import {Client} from 'sentry/api';
  9. import Feature from 'sentry/components/acl/feature';
  10. import {Alert} from 'sentry/components/alert';
  11. import {Button} from 'sentry/components/button';
  12. import ButtonBar from 'sentry/components/buttonBar';
  13. import {CompactSelect} from 'sentry/components/compactSelect';
  14. import * as Layout from 'sentry/components/layouts/thirds';
  15. import LoadingIndicator from 'sentry/components/loadingIndicator';
  16. import NoProjectMessage from 'sentry/components/noProjectMessage';
  17. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  18. import SearchBar from 'sentry/components/searchBar';
  19. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  20. import Switch from 'sentry/components/switchButton';
  21. import {IconAdd} from 'sentry/icons';
  22. import {t} from 'sentry/locale';
  23. import {space} from 'sentry/styles/space';
  24. import {Organization, SelectValue} from 'sentry/types';
  25. import {trackAnalytics} from 'sentry/utils/analytics';
  26. import {decodeScalar} from 'sentry/utils/queryString';
  27. import withApi from 'sentry/utils/withApi';
  28. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  29. import withOrganization from 'sentry/utils/withOrganization';
  30. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  31. import {DASHBOARDS_TEMPLATES} from '../data';
  32. import {assignDefaultLayout, getInitialColumnDepths} from '../layoutUtils';
  33. import {DashboardDetails, DashboardListItem} from '../types';
  34. import DashboardList from './dashboardList';
  35. import TemplateCard from './templateCard';
  36. import {setShowTemplates, shouldShowTemplates} from './utils';
  37. const SORT_OPTIONS: SelectValue<string>[] = [
  38. {label: t('My Dashboards'), value: 'mydashboards'},
  39. {label: t('Dashboard Name (A-Z)'), value: 'title'},
  40. {label: t('Date Created (Newest)'), value: '-dateCreated'},
  41. {label: t('Date Created (Oldest)'), value: 'dateCreated'},
  42. {label: t('Most Popular'), value: 'mostPopular'},
  43. {label: t('Recently Viewed'), value: 'recentlyViewed'},
  44. ];
  45. type Props = {
  46. api: Client;
  47. location: Location;
  48. organization: Organization;
  49. router: InjectedRouter;
  50. } & DeprecatedAsyncView['props'];
  51. type State = {
  52. dashboards: DashboardListItem[] | null;
  53. dashboardsPageLinks: string;
  54. showTemplates: boolean;
  55. } & DeprecatedAsyncView['state'];
  56. class ManageDashboards extends DeprecatedAsyncView<Props, State> {
  57. getDefaultState() {
  58. return {
  59. ...super.getDefaultState(),
  60. showTemplates: shouldShowTemplates(),
  61. };
  62. }
  63. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  64. const {organization, location} = this.props;
  65. const endpoints: ReturnType<DeprecatedAsyncView['getEndpoints']> = [
  66. [
  67. 'dashboards',
  68. `/organizations/${organization.slug}/dashboards/`,
  69. {
  70. query: {
  71. ...pick(location.query, ['cursor', 'query']),
  72. sort: this.getActiveSort().value,
  73. per_page: '9',
  74. },
  75. },
  76. ],
  77. ];
  78. return endpoints;
  79. }
  80. getActiveSort() {
  81. const {location} = this.props;
  82. const urlSort = decodeScalar(location.query.sort, 'mydashboards');
  83. return SORT_OPTIONS.find(item => item.value === urlSort) || SORT_OPTIONS[0];
  84. }
  85. onDashboardsChange() {
  86. this.reloadData();
  87. }
  88. handleSearch(query: string) {
  89. const {location, router, organization} = this.props;
  90. trackAnalytics('dashboards_manage.search', {
  91. organization,
  92. });
  93. router.push({
  94. pathname: location.pathname,
  95. query: {...location.query, cursor: undefined, query},
  96. });
  97. }
  98. handleSortChange = (value: string) => {
  99. const {location, organization} = this.props;
  100. trackAnalytics('dashboards_manage.change_sort', {
  101. organization,
  102. sort: value,
  103. });
  104. browserHistory.push({
  105. pathname: location.pathname,
  106. query: {
  107. ...location.query,
  108. cursor: undefined,
  109. sort: value,
  110. },
  111. });
  112. };
  113. toggleTemplates = () => {
  114. const {showTemplates} = this.state;
  115. const {organization} = this.props;
  116. trackAnalytics('dashboards_manage.templates.toggle', {
  117. organization,
  118. show_templates: !showTemplates,
  119. });
  120. this.setState({showTemplates: !showTemplates}, () => {
  121. setShowTemplates(!showTemplates);
  122. });
  123. };
  124. getQuery() {
  125. const {query} = this.props.location.query;
  126. return typeof query === 'string' ? query : undefined;
  127. }
  128. renderTemplates() {
  129. return (
  130. <TemplateContainer>
  131. {DASHBOARDS_TEMPLATES.map(dashboard => (
  132. <TemplateCard
  133. title={dashboard.title}
  134. description={dashboard.description}
  135. onPreview={() => this.onPreview(dashboard.id)}
  136. onAdd={() => this.onAdd(dashboard)}
  137. key={dashboard.title}
  138. />
  139. ))}
  140. </TemplateContainer>
  141. );
  142. }
  143. renderActions() {
  144. const activeSort = this.getActiveSort();
  145. return (
  146. <StyledActions>
  147. <SearchBar
  148. defaultQuery=""
  149. query={this.getQuery()}
  150. placeholder={t('Search Dashboards')}
  151. onSearch={query => this.handleSearch(query)}
  152. />
  153. <CompactSelect
  154. triggerProps={{prefix: t('Sort By')}}
  155. value={activeSort.value}
  156. options={SORT_OPTIONS}
  157. onChange={opt => this.handleSortChange(opt.value)}
  158. position="bottom-end"
  159. />
  160. </StyledActions>
  161. );
  162. }
  163. renderNoAccess() {
  164. return (
  165. <Layout.Page>
  166. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  167. </Layout.Page>
  168. );
  169. }
  170. renderDashboards() {
  171. const {dashboards, dashboardsPageLinks} = this.state;
  172. const {organization, location, api} = this.props;
  173. return (
  174. <DashboardList
  175. api={api}
  176. dashboards={dashboards}
  177. organization={organization}
  178. pageLinks={dashboardsPageLinks}
  179. location={location}
  180. onDashboardsChange={() => this.onDashboardsChange()}
  181. />
  182. );
  183. }
  184. getTitle() {
  185. return t('Dashboards');
  186. }
  187. onCreate() {
  188. const {organization, location} = this.props;
  189. trackAnalytics('dashboards_manage.create.start', {
  190. organization,
  191. });
  192. browserHistory.push(
  193. normalizeUrl({
  194. pathname: `/organizations/${organization.slug}/dashboards/new/`,
  195. query: location.query,
  196. })
  197. );
  198. }
  199. async onAdd(dashboard: DashboardDetails) {
  200. const {organization, api} = this.props;
  201. trackAnalytics('dashboards_manage.templates.add', {
  202. organization,
  203. dashboard_id: dashboard.id,
  204. dashboard_title: dashboard.title,
  205. was_previewed: false,
  206. });
  207. const newDashboard = await createDashboard(
  208. api,
  209. organization.slug,
  210. {
  211. ...dashboard,
  212. widgets: assignDefaultLayout(dashboard.widgets, getInitialColumnDepths()),
  213. },
  214. true
  215. );
  216. addSuccessMessage(`${dashboard.title} dashboard template successfully added.`);
  217. this.loadDashboard(newDashboard.id);
  218. }
  219. loadDashboard(dashboardId: string) {
  220. const {organization, location} = this.props;
  221. browserHistory.push(
  222. normalizeUrl({
  223. pathname: `/organizations/${organization.slug}/dashboards/${dashboardId}/`,
  224. query: location.query,
  225. })
  226. );
  227. }
  228. onPreview(dashboardId: string) {
  229. const {organization, location} = this.props;
  230. trackAnalytics('dashboards_manage.templates.preview', {
  231. organization,
  232. dashboard_id: dashboardId,
  233. });
  234. browserHistory.push(
  235. normalizeUrl({
  236. pathname: `/organizations/${organization.slug}/dashboards/new/${dashboardId}/`,
  237. query: location.query,
  238. })
  239. );
  240. }
  241. renderLoading() {
  242. return (
  243. <Layout.Page withPadding>
  244. <LoadingIndicator />
  245. </Layout.Page>
  246. );
  247. }
  248. renderBody() {
  249. const {showTemplates} = this.state;
  250. const {organization, api, location} = this.props;
  251. return (
  252. <Feature
  253. organization={organization}
  254. features={['dashboards-edit']}
  255. renderDisabled={this.renderNoAccess}
  256. >
  257. <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
  258. <Layout.Page>
  259. <NoProjectMessage organization={organization}>
  260. <Layout.Header>
  261. <Layout.HeaderContent>
  262. <Layout.Title>
  263. {t('Dashboards')}
  264. <PageHeadingQuestionTooltip
  265. docsUrl="https://docs.sentry.io/product/dashboards/"
  266. title={t(
  267. 'A broad overview of your application’s health where you can navigate through error and performance data across multiple projects.'
  268. )}
  269. />
  270. </Layout.Title>
  271. </Layout.HeaderContent>
  272. <Layout.HeaderActions>
  273. <ButtonBar gap={1.5}>
  274. <TemplateSwitch>
  275. {t('Show Templates')}
  276. <Switch
  277. isActive={showTemplates}
  278. size="lg"
  279. toggle={this.toggleTemplates}
  280. />
  281. </TemplateSwitch>
  282. <Button
  283. data-test-id="dashboard-create"
  284. onClick={event => {
  285. event.preventDefault();
  286. this.onCreate();
  287. }}
  288. size="sm"
  289. priority="primary"
  290. icon={<IconAdd isCircled />}
  291. >
  292. {t('Create Dashboard')}
  293. </Button>
  294. <Feature features={['dashboards-import']}>
  295. <Button
  296. onClick={() => {
  297. openImportDashboardFromFileModal({
  298. organization,
  299. api,
  300. location,
  301. });
  302. }}
  303. size="sm"
  304. priority="primary"
  305. icon={<IconAdd isCircled />}
  306. >
  307. {t('Import Dashboard from JSON')}
  308. </Button>
  309. </Feature>
  310. </ButtonBar>
  311. </Layout.HeaderActions>
  312. </Layout.Header>
  313. <Layout.Body>
  314. <Layout.Main fullWidth>
  315. {showTemplates && this.renderTemplates()}
  316. {this.renderActions()}
  317. {this.renderDashboards()}
  318. </Layout.Main>
  319. </Layout.Body>
  320. </NoProjectMessage>
  321. </Layout.Page>
  322. </SentryDocumentTitle>
  323. </Feature>
  324. );
  325. }
  326. }
  327. const StyledActions = styled('div')`
  328. display: grid;
  329. grid-template-columns: auto max-content;
  330. gap: ${space(2)};
  331. margin-bottom: ${space(2)};
  332. @media (max-width: ${p => p.theme.breakpoints.small}) {
  333. grid-template-columns: auto;
  334. }
  335. `;
  336. const TemplateSwitch = styled('label')`
  337. font-weight: normal;
  338. font-size: ${p => p.theme.fontSizeLarge};
  339. display: flex;
  340. align-items: center;
  341. gap: ${space(1)};
  342. width: max-content;
  343. margin: 0;
  344. `;
  345. const TemplateContainer = styled('div')`
  346. display: grid;
  347. gap: ${space(2)};
  348. margin-bottom: ${space(0.5)};
  349. @media (min-width: ${p => p.theme.breakpoints.small}) {
  350. grid-template-columns: repeat(2, minmax(200px, 1fr));
  351. }
  352. @media (min-width: ${p => p.theme.breakpoints.large}) {
  353. grid-template-columns: repeat(4, minmax(200px, 1fr));
  354. }
  355. `;
  356. export default withApi(withOrganization(ManageDashboards));