resultsHeader.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {Component} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {fetchHomepageQuery} from 'sentry/actionCreators/discoverHomepageQueries';
  6. import {fetchSavedQuery} from 'sentry/actionCreators/discoverSavedQueries';
  7. import {Client} from 'sentry/api';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import TimeSince from 'sentry/components/timeSince';
  10. import {t} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {Organization, SavedQuery} from 'sentry/types';
  13. import EventView from 'sentry/utils/discover/eventView';
  14. import withApi from 'sentry/utils/withApi';
  15. import DiscoverBreadcrumb from './breadcrumb';
  16. import EventInputName from './eventInputName';
  17. import SavedQueryButtonGroup from './savedQuery';
  18. type Props = {
  19. api: Client;
  20. errorCode: number;
  21. eventView: EventView;
  22. location: Location;
  23. organization: Organization;
  24. router: InjectedRouter;
  25. setSavedQuery: (savedQuery: SavedQuery) => void;
  26. yAxis: string[];
  27. isHomepage?: boolean;
  28. };
  29. type State = {
  30. homepageQuery: SavedQuery | undefined;
  31. loading: boolean;
  32. savedQuery: SavedQuery | undefined;
  33. };
  34. class ResultsHeader extends Component<Props, State> {
  35. state: State = {
  36. homepageQuery: undefined,
  37. savedQuery: undefined,
  38. loading: true,
  39. };
  40. componentDidMount() {
  41. const {eventView, isHomepage} = this.props;
  42. const {loading} = this.state;
  43. if (!isHomepage && eventView.id) {
  44. this.fetchData();
  45. } else if (eventView.id === undefined && loading) {
  46. // If this is a new query, there's nothing to load
  47. this.setState({loading: false});
  48. }
  49. if (isHomepage) {
  50. this.fetchHomepageQueryData();
  51. }
  52. }
  53. componentDidUpdate(prevProps: Props) {
  54. if (
  55. prevProps.eventView &&
  56. this.props.eventView &&
  57. prevProps.eventView.id !== this.props.eventView.id
  58. ) {
  59. this.fetchData();
  60. }
  61. }
  62. fetchData() {
  63. const {api, eventView, organization, isHomepage} = this.props;
  64. if (!isHomepage && typeof eventView.id === 'string') {
  65. this.setState({loading: true});
  66. fetchSavedQuery(api, organization.slug, eventView.id).then(savedQuery => {
  67. this.setState({savedQuery, loading: false});
  68. });
  69. }
  70. }
  71. fetchHomepageQueryData() {
  72. const {api, organization} = this.props;
  73. this.setState({loading: true});
  74. fetchHomepageQuery(api, organization.slug).then(homepageQuery => {
  75. this.setState({homepageQuery, loading: false});
  76. });
  77. }
  78. renderAuthor() {
  79. const {eventView, isHomepage} = this.props;
  80. const {savedQuery} = this.state;
  81. // No saved query in use.
  82. if (!eventView.id || isHomepage) {
  83. return null;
  84. }
  85. let createdBy = ' \u2014 ';
  86. let lastEdit: React.ReactNode = ' \u2014 ';
  87. if (savedQuery !== undefined) {
  88. createdBy = savedQuery.createdBy?.email || '\u2014';
  89. lastEdit = <TimeSince date={savedQuery.dateUpdated} />;
  90. }
  91. return (
  92. <Subtitle>
  93. {t('Created by:')} {createdBy} | {t('Last edited:')} {lastEdit}
  94. </Subtitle>
  95. );
  96. }
  97. render() {
  98. const {
  99. organization,
  100. location,
  101. errorCode,
  102. eventView,
  103. yAxis,
  104. router,
  105. setSavedQuery,
  106. isHomepage,
  107. } = this.props;
  108. const {savedQuery, loading, homepageQuery} = this.state;
  109. return (
  110. <Layout.Header>
  111. <StyledHeaderContent>
  112. <DiscoverBreadcrumb
  113. eventView={eventView}
  114. organization={organization}
  115. location={location}
  116. isHomepage={isHomepage}
  117. />
  118. <EventInputName
  119. savedQuery={savedQuery}
  120. organization={organization}
  121. eventView={eventView}
  122. isHomepage={isHomepage}
  123. />
  124. {this.renderAuthor()}
  125. </StyledHeaderContent>
  126. <Layout.HeaderActions>
  127. <SavedQueryButtonGroup
  128. setSavedQuery={setSavedQuery}
  129. location={location}
  130. organization={organization}
  131. eventView={eventView}
  132. savedQuery={savedQuery}
  133. queryDataLoading={loading}
  134. disabled={errorCode >= 400 && errorCode < 500}
  135. updateCallback={() => this.fetchData()}
  136. yAxis={yAxis}
  137. router={router}
  138. isHomepage={isHomepage}
  139. setHomepageQuery={updatedHomepageQuery =>
  140. this.setState({homepageQuery: updatedHomepageQuery})
  141. }
  142. homepageQuery={homepageQuery}
  143. />
  144. </Layout.HeaderActions>
  145. </Layout.Header>
  146. );
  147. }
  148. }
  149. const Subtitle = styled('h4')`
  150. font-size: ${p => p.theme.fontSizeLarge};
  151. font-weight: normal;
  152. color: ${p => p.theme.gray300};
  153. margin: ${space(0.5)} 0 0 0;
  154. `;
  155. const StyledHeaderContent = styled(Layout.HeaderContent)`
  156. overflow: unset;
  157. `;
  158. export default withApi(ResultsHeader);