index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. const endpoints: ReturnType<AsyncView['getEndpoints']> = [
  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. return endpoints;
  77. }
  78. getActiveSort() {
  79. const {location} = this.props;
  80. const urlSort = decodeScalar(location.query.sort, 'mydashboards');
  81. return SORT_OPTIONS.find(item => item.value === urlSort) || SORT_OPTIONS[0];
  82. }
  83. onDashboardsChange() {
  84. this.reloadData();
  85. }
  86. handleSearch(query: string) {
  87. const {location, router, organization} = this.props;
  88. trackAnalytics('dashboards_manage.search', {
  89. organization,
  90. });
  91. router.push({
  92. pathname: location.pathname,
  93. query: {...location.query, cursor: undefined, query},
  94. });
  95. }
  96. handleSortChange = (value: string) => {
  97. const {location, organization} = this.props;
  98. trackAnalytics('dashboards_manage.change_sort', {
  99. organization,
  100. sort: value,
  101. });
  102. browserHistory.push({
  103. pathname: location.pathname,
  104. query: {
  105. ...location.query,
  106. cursor: undefined,
  107. sort: value,
  108. },
  109. });
  110. };
  111. toggleTemplates = () => {
  112. const {showTemplates} = this.state;
  113. const {organization} = this.props;
  114. trackAnalytics('dashboards_manage.templates.toggle', {
  115. organization,
  116. show_templates: !showTemplates,
  117. });
  118. this.setState({showTemplates: !showTemplates}, () => {
  119. setShowTemplates(!showTemplates);
  120. });
  121. };
  122. getQuery() {
  123. const {query} = this.props.location.query;
  124. return typeof query === 'string' ? query : undefined;
  125. }
  126. renderTemplates() {
  127. return (
  128. <TemplateContainer>
  129. {DASHBOARDS_TEMPLATES.map(dashboard => (
  130. <TemplateCard
  131. title={dashboard.title}
  132. description={dashboard.description}
  133. onPreview={() => this.onPreview(dashboard.id)}
  134. onAdd={() => this.onAdd(dashboard)}
  135. key={dashboard.title}
  136. />
  137. ))}
  138. </TemplateContainer>
  139. );
  140. }
  141. renderActions() {
  142. const activeSort = this.getActiveSort();
  143. return (
  144. <StyledActions>
  145. <SearchBar
  146. defaultQuery=""
  147. query={this.getQuery()}
  148. placeholder={t('Search Dashboards')}
  149. onSearch={query => this.handleSearch(query)}
  150. />
  151. <CompactSelect
  152. triggerProps={{prefix: t('Sort By')}}
  153. value={activeSort.value}
  154. options={SORT_OPTIONS}
  155. onChange={opt => this.handleSortChange(opt.value)}
  156. position="bottom-end"
  157. />
  158. </StyledActions>
  159. );
  160. }
  161. renderNoAccess() {
  162. return (
  163. <Layout.Page>
  164. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  165. </Layout.Page>
  166. );
  167. }
  168. renderDashboards() {
  169. const {dashboards, dashboardsPageLinks} = this.state;
  170. const {organization, location, api} = this.props;
  171. return (
  172. <DashboardList
  173. api={api}
  174. dashboards={dashboards}
  175. organization={organization}
  176. pageLinks={dashboardsPageLinks}
  177. location={location}
  178. onDashboardsChange={() => this.onDashboardsChange()}
  179. />
  180. );
  181. }
  182. getTitle() {
  183. return t('Dashboards');
  184. }
  185. onCreate() {
  186. const {organization, location} = this.props;
  187. trackAnalytics('dashboards_manage.create.start', {
  188. organization,
  189. });
  190. browserHistory.push(
  191. normalizeUrl({
  192. pathname: `/organizations/${organization.slug}/dashboards/new/`,
  193. query: location.query,
  194. })
  195. );
  196. }
  197. async onAdd(dashboard: DashboardDetails) {
  198. const {organization, api} = this.props;
  199. trackAnalytics('dashboards_manage.templates.add', {
  200. organization,
  201. dashboard_id: dashboard.id,
  202. dashboard_title: dashboard.title,
  203. was_previewed: false,
  204. });
  205. const newDashboard = await createDashboard(
  206. api,
  207. organization.slug,
  208. {
  209. ...dashboard,
  210. widgets: assignDefaultLayout(dashboard.widgets, getInitialColumnDepths()),
  211. },
  212. true
  213. );
  214. addSuccessMessage(`${dashboard.title} dashboard template successfully added.`);
  215. this.loadDashboard(newDashboard.id);
  216. }
  217. loadDashboard(dashboardId: string) {
  218. const {organization, location} = this.props;
  219. browserHistory.push(
  220. normalizeUrl({
  221. pathname: `/organizations/${organization.slug}/dashboards/${dashboardId}/`,
  222. query: location.query,
  223. })
  224. );
  225. }
  226. onPreview(dashboardId: string) {
  227. const {organization, location} = this.props;
  228. trackAnalytics('dashboards_manage.templates.preview', {
  229. organization,
  230. dashboard_id: dashboardId,
  231. });
  232. browserHistory.push(
  233. normalizeUrl({
  234. pathname: `/organizations/${organization.slug}/dashboards/new/${dashboardId}/`,
  235. query: location.query,
  236. })
  237. );
  238. }
  239. renderLoading() {
  240. return (
  241. <Layout.Page withPadding>
  242. <LoadingIndicator />
  243. </Layout.Page>
  244. );
  245. }
  246. renderBody() {
  247. const {showTemplates} = this.state;
  248. const {organization} = this.props;
  249. return (
  250. <Feature
  251. organization={organization}
  252. features={['dashboards-edit']}
  253. renderDisabled={this.renderNoAccess}
  254. >
  255. <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
  256. <Layout.Page>
  257. <NoProjectMessage organization={organization}>
  258. <Layout.Header>
  259. <Layout.HeaderContent>
  260. <Layout.Title>
  261. {t('Dashboards')}
  262. <PageHeadingQuestionTooltip
  263. docsUrl="https://docs.sentry.io/product/dashboards/"
  264. title={t(
  265. 'A broad overview of your application’s health where you can navigate through error and performance data across multiple projects.'
  266. )}
  267. />
  268. </Layout.Title>
  269. </Layout.HeaderContent>
  270. <Layout.HeaderActions>
  271. <ButtonBar gap={1.5}>
  272. <TemplateSwitch>
  273. {t('Show Templates')}
  274. <Switch
  275. isActive={showTemplates}
  276. size="lg"
  277. toggle={this.toggleTemplates}
  278. />
  279. </TemplateSwitch>
  280. <Button
  281. data-test-id="dashboard-create"
  282. onClick={event => {
  283. event.preventDefault();
  284. this.onCreate();
  285. }}
  286. size="sm"
  287. priority="primary"
  288. icon={<IconAdd isCircled />}
  289. >
  290. {t('Create Dashboard')}
  291. </Button>
  292. </ButtonBar>
  293. </Layout.HeaderActions>
  294. </Layout.Header>
  295. <Layout.Body>
  296. <Layout.Main fullWidth>
  297. {showTemplates && this.renderTemplates()}
  298. {this.renderActions()}
  299. {this.renderDashboards()}
  300. </Layout.Main>
  301. </Layout.Body>
  302. </NoProjectMessage>
  303. </Layout.Page>
  304. </SentryDocumentTitle>
  305. </Feature>
  306. );
  307. }
  308. }
  309. const StyledActions = styled('div')`
  310. display: grid;
  311. grid-template-columns: auto max-content;
  312. gap: ${space(2)};
  313. margin-bottom: ${space(2)};
  314. @media (max-width: ${p => p.theme.breakpoints.small}) {
  315. grid-template-columns: auto;
  316. }
  317. `;
  318. const TemplateSwitch = styled('label')`
  319. font-weight: normal;
  320. font-size: ${p => p.theme.fontSizeLarge};
  321. display: flex;
  322. align-items: center;
  323. gap: ${space(1)};
  324. width: max-content;
  325. margin: 0;
  326. `;
  327. const TemplateContainer = styled('div')`
  328. display: grid;
  329. gap: ${space(2)};
  330. margin-bottom: ${space(0.5)};
  331. @media (min-width: ${p => p.theme.breakpoints.small}) {
  332. grid-template-columns: repeat(2, minmax(200px, 1fr));
  333. }
  334. @media (min-width: ${p => p.theme.breakpoints.large}) {
  335. grid-template-columns: repeat(4, minmax(200px, 1fr));
  336. }
  337. `;
  338. export default withApi(withOrganization(ManageDashboards));