resultsHeader.tsx 6.3 KB

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