resultsHeader.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {stringify} from 'query-string';
  5. import {fetchHomepageQuery} from 'sentry/actionCreators/discoverHomepageQueries';
  6. import {fetchSavedQuery} from 'sentry/actionCreators/discoverSavedQueries';
  7. import type {Client} from 'sentry/api';
  8. import Feature from 'sentry/components/acl/feature';
  9. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  12. import TimeSince from 'sentry/components/timeSince';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  16. import type {Organization, SavedQuery} from 'sentry/types/organization';
  17. import EventView from 'sentry/utils/discover/eventView';
  18. import type {SavedQueryDatasets} from 'sentry/utils/discover/types';
  19. import withApi from 'sentry/utils/withApi';
  20. import {DatasetSelectorTabs} from 'sentry/views/discover/savedQuery/datasetSelectorTabs';
  21. import {getSavedQueryWithDataset} from 'sentry/views/discover/savedQuery/utils';
  22. import Banner from './banner';
  23. import DiscoverBreadcrumb from './breadcrumb';
  24. import {DEFAULT_EVENT_VIEW} from './data';
  25. import EventInputName from './eventInputName';
  26. import SavedQueryButtonGroup from './savedQuery';
  27. type Props = {
  28. api: Client;
  29. errorCode: number;
  30. eventView: EventView;
  31. location: Location;
  32. organization: Organization;
  33. router: InjectedRouter;
  34. setSavedQuery: (savedQuery?: SavedQuery) => void;
  35. yAxis: string[];
  36. isHomepage?: boolean;
  37. splitDecision?: SavedQueryDatasets;
  38. };
  39. type State = {
  40. homepageQuery: SavedQuery | undefined;
  41. loading: boolean;
  42. savedQuery: SavedQuery | undefined;
  43. };
  44. class ResultsHeader extends Component<Props, State> {
  45. state: State = {
  46. homepageQuery: undefined,
  47. savedQuery: undefined,
  48. loading: true,
  49. };
  50. componentDidMount() {
  51. const {eventView, isHomepage} = this.props;
  52. const {loading} = this.state;
  53. if (!isHomepage && eventView.id) {
  54. this.fetchData();
  55. } else if (eventView.id === undefined && loading) {
  56. // If this is a new query, there's nothing to load
  57. this.setState({loading: false});
  58. }
  59. if (isHomepage) {
  60. this.fetchHomepageQueryData();
  61. }
  62. }
  63. componentDidUpdate(prevProps: Props) {
  64. if (
  65. prevProps.eventView &&
  66. this.props.eventView &&
  67. prevProps.eventView.id !== this.props.eventView.id
  68. ) {
  69. this.fetchData();
  70. }
  71. }
  72. fetchData() {
  73. const {api, eventView, organization, isHomepage} = this.props;
  74. if (!isHomepage && typeof eventView.id === 'string') {
  75. this.setState({loading: true});
  76. fetchSavedQuery(api, organization.slug, eventView.id).then(savedQuery => {
  77. this.setState({
  78. savedQuery: organization.features.includes(
  79. 'performance-discover-dataset-selector'
  80. )
  81. ? (getSavedQueryWithDataset(savedQuery) as SavedQuery)
  82. : savedQuery,
  83. loading: false,
  84. });
  85. });
  86. }
  87. }
  88. fetchHomepageQueryData() {
  89. const {api, organization} = this.props;
  90. this.setState({loading: true});
  91. fetchHomepageQuery(api, organization.slug).then(homepageQuery => {
  92. this.setState({
  93. homepageQuery: organization.features.includes(
  94. 'performance-discover-dataset-selector'
  95. )
  96. ? (getSavedQueryWithDataset(homepageQuery) as SavedQuery)
  97. : homepageQuery,
  98. loading: false,
  99. });
  100. });
  101. }
  102. renderAuthor() {
  103. const {eventView, isHomepage} = this.props;
  104. const {savedQuery} = this.state;
  105. // No saved query in use.
  106. if (!eventView.id || isHomepage) {
  107. return null;
  108. }
  109. let createdBy = ' \u2014 ';
  110. let lastEdit: React.ReactNode = ' \u2014 ';
  111. if (savedQuery !== undefined) {
  112. createdBy = savedQuery.createdBy?.email || '\u2014';
  113. lastEdit = <TimeSince date={savedQuery.dateUpdated} />;
  114. }
  115. return (
  116. <Subtitle>
  117. {t('Created by:')} {createdBy} | {t('Last edited:')} {lastEdit}
  118. </Subtitle>
  119. );
  120. }
  121. renderBanner() {
  122. const {location, organization} = this.props;
  123. const eventView = EventView.fromNewQueryWithLocation(DEFAULT_EVENT_VIEW, location);
  124. const to = eventView.getResultsViewUrlTarget(organization.slug);
  125. const resultsUrl = `${to.pathname}?${stringify(to.query)}`;
  126. return (
  127. <BannerWrapper>
  128. <Banner
  129. organization={organization}
  130. resultsUrl={resultsUrl}
  131. showBuildNewQueryButton={false}
  132. />
  133. </BannerWrapper>
  134. );
  135. }
  136. render() {
  137. const {
  138. organization,
  139. location,
  140. errorCode,
  141. eventView,
  142. yAxis,
  143. router,
  144. setSavedQuery,
  145. isHomepage,
  146. splitDecision,
  147. } = this.props;
  148. const {savedQuery, loading, homepageQuery} = this.state;
  149. const hasDiscoverQueryFeature = organization.features.includes('discover-query');
  150. return (
  151. <Layout.Header>
  152. <Layout.HeaderContent>
  153. {isHomepage ? (
  154. <GuideAnchor target="discover_landing_header">
  155. <Layout.Title>
  156. {t('Discover')}
  157. <PageHeadingQuestionTooltip
  158. docsUrl="https://docs.sentry.io/product/discover-queries/"
  159. title={t(
  160. 'Create queries to get insights into the health of your system.'
  161. )}
  162. />
  163. </Layout.Title>
  164. </GuideAnchor>
  165. ) : hasDiscoverQueryFeature ? (
  166. <Fragment>
  167. <DiscoverBreadcrumb
  168. eventView={eventView}
  169. organization={organization}
  170. location={location}
  171. isHomepage={isHomepage}
  172. />
  173. <EventInputName
  174. savedQuery={savedQuery}
  175. organization={organization}
  176. eventView={eventView}
  177. isHomepage={isHomepage}
  178. />
  179. </Fragment>
  180. ) : (
  181. // Only has discover-basic
  182. <Fragment>
  183. <Layout.Title>
  184. {t('Discover')}
  185. <PageHeadingQuestionTooltip
  186. docsUrl="https://docs.sentry.io/product/discover-queries/"
  187. title={t(
  188. 'Create queries to get insights into the health of your system.'
  189. )}
  190. />
  191. </Layout.Title>
  192. </Fragment>
  193. )}
  194. {this.renderAuthor()}
  195. </Layout.HeaderContent>
  196. <Layout.HeaderActions>
  197. <SavedQueryButtonGroup
  198. setSavedQuery={setSavedQuery}
  199. location={location}
  200. organization={organization}
  201. eventView={eventView}
  202. savedQuery={savedQuery}
  203. queryDataLoading={loading}
  204. disabled={errorCode >= 400 && errorCode < 500}
  205. updateCallback={() => this.fetchData()}
  206. yAxis={yAxis}
  207. router={router}
  208. isHomepage={isHomepage}
  209. setHomepageQuery={updatedHomepageQuery => {
  210. this.setState({
  211. homepageQuery: organization.features.includes(
  212. 'performance-discover-dataset-selector'
  213. )
  214. ? (getSavedQueryWithDataset(updatedHomepageQuery) as SavedQuery)
  215. : updatedHomepageQuery,
  216. });
  217. if (isHomepage) {
  218. setSavedQuery(updatedHomepageQuery);
  219. }
  220. }}
  221. homepageQuery={homepageQuery}
  222. />
  223. </Layout.HeaderActions>
  224. {isHomepage && this.renderBanner()}
  225. <Feature
  226. organization={organization}
  227. features="performance-discover-dataset-selector"
  228. >
  229. {({hasFeature}) =>
  230. hasFeature && (
  231. <DatasetSelectorWrapper>
  232. <DatasetSelectorTabs
  233. eventView={eventView}
  234. isHomepage={isHomepage}
  235. savedQuery={savedQuery}
  236. splitDecision={splitDecision}
  237. />
  238. </DatasetSelectorWrapper>
  239. )
  240. }
  241. </Feature>
  242. </Layout.Header>
  243. );
  244. }
  245. }
  246. const Subtitle = styled('h4')`
  247. font-size: ${p => p.theme.fontSizeLarge};
  248. font-weight: ${p => p.theme.fontWeightNormal};
  249. color: ${p => p.theme.gray300};
  250. margin: ${space(0.5)} 0 0 0;
  251. `;
  252. const BannerWrapper = styled('div')`
  253. grid-column: 1 / -1;
  254. `;
  255. // Force the dataset selector to have the entire width of the grid
  256. // so it doesn't go into the overflow menu state when the window is small
  257. const DatasetSelectorWrapper = styled('div')`
  258. grid-column: 1 / -1;
  259. `;
  260. export default withApi(ResultsHeader);