index.tsx 11 KB

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