index.tsx 11 KB

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