index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import type {InjectedRouter} from 'react-router';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import pick from 'lodash/pick';
  6. import {createDashboard} from 'sentry/actionCreators/dashboards';
  7. import {addSuccessMessage} from 'sentry/actionCreators/indicator';
  8. import {openImportDashboardFromFileModal} from 'sentry/actionCreators/modal';
  9. import type {Client} from 'sentry/api';
  10. import Feature from 'sentry/components/acl/feature';
  11. import {Alert} from 'sentry/components/alert';
  12. import {Button} from 'sentry/components/button';
  13. import ButtonBar from 'sentry/components/buttonBar';
  14. import {CompactSelect} from 'sentry/components/compactSelect';
  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, IconDownload} from 'sentry/icons';
  23. import {t} from 'sentry/locale';
  24. import {space} from 'sentry/styles/space';
  25. import type {Organization, SelectValue} from 'sentry/types';
  26. import {trackAnalytics} from 'sentry/utils/analytics';
  27. import {hasDashboardImportFeature} from 'sentry/utils/metrics/features';
  28. import {decodeScalar} from 'sentry/utils/queryString';
  29. import withApi from 'sentry/utils/withApi';
  30. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  31. import withOrganization from 'sentry/utils/withOrganization';
  32. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  33. import {openDashboardImport} from '../../metrics/dashboardImportModal';
  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. {hasDashboardImportFeature(organization) && (
  286. <Button
  287. onClick={() => {
  288. openDashboardImport(organization);
  289. }}
  290. size="sm"
  291. icon={<IconDownload />}
  292. >
  293. {t('Import Dashboard')}
  294. </Button>
  295. )}
  296. <Button
  297. data-test-id="dashboard-create"
  298. onClick={event => {
  299. event.preventDefault();
  300. this.onCreate();
  301. }}
  302. size="sm"
  303. priority="primary"
  304. icon={<IconAdd isCircled />}
  305. >
  306. {t('Create Dashboard')}
  307. </Button>
  308. <Feature features="dashboards-import">
  309. <Button
  310. onClick={() => {
  311. openImportDashboardFromFileModal({
  312. organization,
  313. api,
  314. location,
  315. });
  316. }}
  317. size="sm"
  318. priority="primary"
  319. icon={<IconAdd isCircled />}
  320. >
  321. {t('Import Dashboard from JSON')}
  322. </Button>
  323. </Feature>
  324. </ButtonBar>
  325. </Layout.HeaderActions>
  326. </Layout.Header>
  327. <Layout.Body>
  328. <Layout.Main fullWidth>
  329. {showTemplates && this.renderTemplates()}
  330. {this.renderActions()}
  331. {this.renderDashboards()}
  332. </Layout.Main>
  333. </Layout.Body>
  334. </NoProjectMessage>
  335. </Layout.Page>
  336. </SentryDocumentTitle>
  337. </Feature>
  338. );
  339. }
  340. }
  341. const StyledActions = styled('div')`
  342. display: grid;
  343. grid-template-columns: auto max-content;
  344. gap: ${space(2)};
  345. margin-bottom: ${space(2)};
  346. @media (max-width: ${p => p.theme.breakpoints.small}) {
  347. grid-template-columns: auto;
  348. }
  349. `;
  350. const TemplateSwitch = styled('label')`
  351. font-weight: normal;
  352. font-size: ${p => p.theme.fontSizeLarge};
  353. display: flex;
  354. align-items: center;
  355. gap: ${space(1)};
  356. width: max-content;
  357. margin: 0;
  358. `;
  359. const TemplateContainer = styled('div')`
  360. display: grid;
  361. gap: ${space(2)};
  362. margin-bottom: ${space(0.5)};
  363. @media (min-width: ${p => p.theme.breakpoints.small}) {
  364. grid-template-columns: repeat(2, minmax(200px, 1fr));
  365. }
  366. @media (min-width: ${p => p.theme.breakpoints.large}) {
  367. grid-template-columns: repeat(4, minmax(200px, 1fr));
  368. }
  369. `;
  370. export default withApi(withOrganization(ManageDashboards));