index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 {getDashboardTemplates} 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. const {organization} = this.props;
  133. return (
  134. <TemplateContainer>
  135. {getDashboardTemplates(organization).map(dashboard => (
  136. <TemplateCard
  137. title={dashboard.title}
  138. description={dashboard.description}
  139. onPreview={() => this.onPreview(dashboard.id)}
  140. onAdd={() => this.onAdd(dashboard)}
  141. key={dashboard.title}
  142. />
  143. ))}
  144. </TemplateContainer>
  145. );
  146. }
  147. renderActions() {
  148. const activeSort = this.getActiveSort();
  149. return (
  150. <StyledActions>
  151. <SearchBar
  152. defaultQuery=""
  153. query={this.getQuery()}
  154. placeholder={t('Search Dashboards')}
  155. onSearch={query => this.handleSearch(query)}
  156. />
  157. <CompactSelect
  158. triggerProps={{prefix: t('Sort By')}}
  159. value={activeSort.value}
  160. options={SORT_OPTIONS}
  161. onChange={opt => this.handleSortChange(opt.value)}
  162. position="bottom-end"
  163. />
  164. </StyledActions>
  165. );
  166. }
  167. renderNoAccess() {
  168. return (
  169. <Layout.Page>
  170. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  171. </Layout.Page>
  172. );
  173. }
  174. renderDashboards() {
  175. const {dashboards, dashboardsPageLinks} = this.state;
  176. const {organization, location, api} = this.props;
  177. return (
  178. <DashboardList
  179. api={api}
  180. dashboards={dashboards}
  181. organization={organization}
  182. pageLinks={dashboardsPageLinks}
  183. location={location}
  184. onDashboardsChange={() => this.onDashboardsChange()}
  185. />
  186. );
  187. }
  188. getTitle() {
  189. return t('Dashboards');
  190. }
  191. onCreate() {
  192. const {organization, location} = this.props;
  193. trackAnalytics('dashboards_manage.create.start', {
  194. organization,
  195. });
  196. browserHistory.push(
  197. normalizeUrl({
  198. pathname: `/organizations/${organization.slug}/dashboards/new/`,
  199. query: location.query,
  200. })
  201. );
  202. }
  203. async onAdd(dashboard: DashboardDetails) {
  204. const {organization, api} = this.props;
  205. trackAnalytics('dashboards_manage.templates.add', {
  206. organization,
  207. dashboard_id: dashboard.id,
  208. dashboard_title: dashboard.title,
  209. was_previewed: false,
  210. });
  211. const newDashboard = await createDashboard(
  212. api,
  213. organization.slug,
  214. {
  215. ...dashboard,
  216. widgets: assignDefaultLayout(dashboard.widgets, getInitialColumnDepths()),
  217. },
  218. true
  219. );
  220. addSuccessMessage(`${dashboard.title} dashboard template successfully added.`);
  221. this.loadDashboard(newDashboard.id);
  222. }
  223. loadDashboard(dashboardId: string) {
  224. const {organization, location} = this.props;
  225. browserHistory.push(
  226. normalizeUrl({
  227. pathname: `/organizations/${organization.slug}/dashboards/${dashboardId}/`,
  228. query: location.query,
  229. })
  230. );
  231. }
  232. onPreview(dashboardId: string) {
  233. const {organization, location} = this.props;
  234. trackAnalytics('dashboards_manage.templates.preview', {
  235. organization,
  236. dashboard_id: dashboardId,
  237. });
  238. browserHistory.push(
  239. normalizeUrl({
  240. pathname: `/organizations/${organization.slug}/dashboards/new/${dashboardId}/`,
  241. query: location.query,
  242. })
  243. );
  244. }
  245. renderLoading() {
  246. return (
  247. <Layout.Page withPadding>
  248. <LoadingIndicator />
  249. </Layout.Page>
  250. );
  251. }
  252. renderBody() {
  253. const {showTemplates} = this.state;
  254. const {organization, api, location} = this.props;
  255. return (
  256. <Feature
  257. organization={organization}
  258. features="dashboards-edit"
  259. renderDisabled={this.renderNoAccess}
  260. >
  261. <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
  262. <Layout.Page>
  263. <NoProjectMessage organization={organization}>
  264. <Layout.Header>
  265. <Layout.HeaderContent>
  266. <Layout.Title>
  267. {t('Dashboards')}
  268. <PageHeadingQuestionTooltip
  269. docsUrl="https://docs.sentry.io/product/dashboards/"
  270. title={t(
  271. 'A broad overview of your application’s health where you can navigate through error and performance data across multiple projects.'
  272. )}
  273. />
  274. </Layout.Title>
  275. </Layout.HeaderContent>
  276. <Layout.HeaderActions>
  277. <ButtonBar gap={1.5}>
  278. <TemplateSwitch>
  279. {t('Show Templates')}
  280. <Switch
  281. isActive={showTemplates}
  282. size="lg"
  283. toggle={this.toggleTemplates}
  284. />
  285. </TemplateSwitch>
  286. <DashboardImportButton />
  287. <Button
  288. data-test-id="dashboard-create"
  289. onClick={event => {
  290. event.preventDefault();
  291. this.onCreate();
  292. }}
  293. size="sm"
  294. priority="primary"
  295. icon={<IconAdd isCircled />}
  296. >
  297. {t('Create Dashboard')}
  298. </Button>
  299. <Feature features="dashboards-import">
  300. <Button
  301. onClick={() => {
  302. openImportDashboardFromFileModal({
  303. organization,
  304. api,
  305. location,
  306. });
  307. }}
  308. size="sm"
  309. priority="primary"
  310. icon={<IconAdd isCircled />}
  311. >
  312. {t('Import Dashboard from JSON')}
  313. </Button>
  314. </Feature>
  315. </ButtonBar>
  316. </Layout.HeaderActions>
  317. </Layout.Header>
  318. <Layout.Body>
  319. <Layout.Main fullWidth>
  320. {showTemplates && this.renderTemplates()}
  321. {this.renderActions()}
  322. {this.renderDashboards()}
  323. </Layout.Main>
  324. </Layout.Body>
  325. </NoProjectMessage>
  326. </Layout.Page>
  327. </SentryDocumentTitle>
  328. </Feature>
  329. );
  330. }
  331. }
  332. const StyledActions = styled('div')`
  333. display: grid;
  334. grid-template-columns: auto max-content;
  335. gap: ${space(2)};
  336. margin-bottom: ${space(2)};
  337. @media (max-width: ${p => p.theme.breakpoints.small}) {
  338. grid-template-columns: auto;
  339. }
  340. `;
  341. const TemplateSwitch = styled('label')`
  342. font-weight: ${p => p.theme.fontWeightNormal};
  343. font-size: ${p => p.theme.fontSizeLarge};
  344. display: flex;
  345. align-items: center;
  346. gap: ${space(1)};
  347. width: max-content;
  348. margin: 0;
  349. `;
  350. const TemplateContainer = styled('div')`
  351. display: grid;
  352. gap: ${space(2)};
  353. margin-bottom: ${space(0.5)};
  354. @media (min-width: ${p => p.theme.breakpoints.small}) {
  355. grid-template-columns: repeat(2, minmax(200px, 1fr));
  356. }
  357. @media (min-width: ${p => p.theme.breakpoints.large}) {
  358. grid-template-columns: repeat(4, minmax(200px, 1fr));
  359. }
  360. `;
  361. export default withApi(withOrganization(ManageDashboards));