resultsHeader.tsx 3.6 KB

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