index.tsx 12 KB

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