homepage.tsx 5.2 KB

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