resultsHeader.tsx 7.2 KB

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