homepage.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import type {InjectedRouter} from 'react-router';
  2. import type {Location} from 'history';
  3. import type {Client} from 'sentry/api';
  4. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  5. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  6. import {
  7. getDatetimeFromState,
  8. normalizeDateTimeString,
  9. } from 'sentry/components/organizations/pageFilters/parse';
  10. import {getPageFilterStorage} from 'sentry/components/organizations/pageFilters/persistence';
  11. import type {Organization, PageFilters, SavedQuery} from 'sentry/types';
  12. import {browserHistory} from 'sentry/utils/browserHistory';
  13. import EventView from 'sentry/utils/discover/eventView';
  14. import withApi from 'sentry/utils/withApi';
  15. import withOrganization from 'sentry/utils/withOrganization';
  16. import withPageFilters from 'sentry/utils/withPageFilters';
  17. import {getSavedQueryWithDataset} from 'sentry/views/discover/savedQuery/utils';
  18. import {Results} from './results';
  19. type Props = {
  20. api: Client;
  21. loading: boolean;
  22. location: Location;
  23. organization: Organization;
  24. router: InjectedRouter;
  25. selection: PageFilters;
  26. setSavedQuery: (savedQuery: SavedQuery) => void;
  27. };
  28. type HomepageQueryState = DeprecatedAsyncComponent['state'] & {
  29. savedQuery?: SavedQuery | null;
  30. starfishResult?: null;
  31. };
  32. class HomepageQueryAPI extends DeprecatedAsyncComponent<Props, HomepageQueryState> {
  33. shouldReload = true;
  34. componentDidUpdate(_, prevState) {
  35. const hasFetchedSavedQuery = !prevState.savedQuery && this.state.savedQuery;
  36. const hasInitiallyLoaded = prevState.loading && !this.state.loading;
  37. const sidebarClicked = this.state.savedQuery && this.props.location.search === '';
  38. const hasValidEventViewInURL = EventView.fromLocation(this.props.location).isValid();
  39. if (
  40. this.state.savedQuery &&
  41. ((hasInitiallyLoaded && hasFetchedSavedQuery && !hasValidEventViewInURL) ||
  42. sidebarClicked)
  43. ) {
  44. const eventView = EventView.fromSavedQuery(this.state.savedQuery);
  45. const pageFilterState = getPageFilterStorage(this.props.organization.slug);
  46. let query = {
  47. ...eventView.generateQueryStringObject(),
  48. };
  49. // Handle locked filters explicitly because we can't expect
  50. // PageFilterContainer to properly overwrite stored filters
  51. // when pushing the homepage query to the URL
  52. if (pageFilterState?.pinnedFilters) {
  53. pageFilterState.pinnedFilters.forEach(pinnedFilter => {
  54. if (pinnedFilter === 'projects') {
  55. query.project = pageFilterState.state.project?.map(String);
  56. } else if (pinnedFilter === 'datetime') {
  57. const {period, start, end, utc} = getDatetimeFromState(pageFilterState.state);
  58. query = {
  59. ...query,
  60. statsPeriod: period ?? undefined,
  61. utc: utc?.toString(),
  62. start: normalizeDateTimeString(start),
  63. end: normalizeDateTimeString(end),
  64. };
  65. } else {
  66. query[pinnedFilter] = pageFilterState.state[pinnedFilter];
  67. }
  68. });
  69. }
  70. browserHistory.replace({
  71. ...this.props.location,
  72. query: {
  73. ...query,
  74. queryDataset: this.state.savedQuery?.queryDataset,
  75. },
  76. });
  77. }
  78. }
  79. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  80. const {organization} = this.props;
  81. const endpoints: ReturnType<DeprecatedAsyncComponent['getEndpoints']> = [];
  82. if (organization.features.includes('discover-query')) {
  83. endpoints.push([
  84. 'savedQuery',
  85. `/organizations/${organization.slug}/discover/homepage/`,
  86. ]);
  87. }
  88. return endpoints;
  89. }
  90. onRequestSuccess({stateKey, data}) {
  91. const {organization} = this.props;
  92. // No homepage query results in a 204, returning an empty string
  93. if (stateKey === 'savedQuery' && data === '') {
  94. this.setState({savedQuery: null});
  95. return;
  96. }
  97. if (stateKey === 'savedQuery') {
  98. this.setState({
  99. savedQuery: organization.features.includes(
  100. 'performance-discover-dataset-selector'
  101. )
  102. ? getSavedQueryWithDataset(data)
  103. : data,
  104. });
  105. }
  106. }
  107. setSavedQuery = (newSavedQuery?: SavedQuery) => {
  108. const {organization} = this.props;
  109. this.setState({
  110. savedQuery: organization.features.includes('performance-discover-dataset-selector')
  111. ? getSavedQueryWithDataset(newSavedQuery)
  112. : newSavedQuery,
  113. });
  114. };
  115. renderBody(): React.ReactNode {
  116. const {savedQuery, loading} = this.state;
  117. return (
  118. <Results
  119. {...this.props}
  120. savedQuery={savedQuery ?? undefined}
  121. loading={loading}
  122. setSavedQuery={this.setSavedQuery}
  123. isHomepage
  124. />
  125. );
  126. }
  127. }
  128. function HomepageContainer(props: Props) {
  129. return (
  130. <PageFiltersContainer skipInitializeUrlParams>
  131. <HomepageQueryAPI {...props} />
  132. </PageFiltersContainer>
  133. );
  134. }
  135. export default withApi(withOrganization(withPageFilters(HomepageContainer)));