resultsHeader.tsx 6.6 KB

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