homepage.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 {PageFilters} from 'sentry/types/core';
  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 {
  67. query[pinnedFilter] = pageFilterState.state[pinnedFilter];
  68. }
  69. });
  70. }
  71. browserHistory.replace({
  72. ...this.props.location,
  73. query: {
  74. ...query,
  75. queryDataset: this.state.savedQuery?.queryDataset,
  76. },
  77. });
  78. }
  79. }
  80. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  81. const {organization} = this.props;
  82. const endpoints: ReturnType<DeprecatedAsyncComponent['getEndpoints']> = [];
  83. if (organization.features.includes('discover-query')) {
  84. endpoints.push([
  85. 'savedQuery',
  86. `/organizations/${organization.slug}/discover/homepage/`,
  87. ]);
  88. }
  89. return endpoints;
  90. }
  91. onRequestSuccess({stateKey, data}) {
  92. const {organization} = this.props;
  93. // No homepage query results in a 204, returning an empty string
  94. if (stateKey === 'savedQuery' && data === '') {
  95. this.setState({savedQuery: null});
  96. return;
  97. }
  98. if (stateKey === 'savedQuery') {
  99. this.setState({
  100. savedQuery: organization.features.includes(
  101. 'performance-discover-dataset-selector'
  102. )
  103. ? getSavedQueryWithDataset(data)
  104. : data,
  105. });
  106. }
  107. }
  108. setSavedQuery = (newSavedQuery?: SavedQuery) => {
  109. const {organization} = this.props;
  110. this.setState({
  111. savedQuery: organization.features.includes('performance-discover-dataset-selector')
  112. ? (getSavedQueryWithDataset(newSavedQuery) as SavedQuery)
  113. : newSavedQuery,
  114. });
  115. };
  116. renderBody(): React.ReactNode {
  117. const {savedQuery, loading} = this.state;
  118. return (
  119. <Results
  120. {...this.props}
  121. savedQuery={savedQuery ?? undefined}
  122. loading={loading}
  123. setSavedQuery={this.setSavedQuery}
  124. isHomepage
  125. />
  126. );
  127. }
  128. }
  129. function HomepageContainer(props: Props) {
  130. return (
  131. <PageFiltersContainer skipInitializeUrlParams>
  132. <HomepageQueryAPI {...props} />
  133. </PageFiltersContainer>
  134. );
  135. }
  136. export default withApi(withOrganization(withPageFilters(HomepageContainer)));