index.tsx 12 KB

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