homepage.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. });
  74. }
  75. }
  76. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  77. const {organization} = this.props;
  78. const endpoints: ReturnType<DeprecatedAsyncComponent['getEndpoints']> = [];
  79. if (organization.features.includes('discover-query')) {
  80. endpoints.push([
  81. 'savedQuery',
  82. `/organizations/${organization.slug}/discover/homepage/`,
  83. ]);
  84. }
  85. return endpoints;
  86. }
  87. onRequestSuccess({stateKey, data}) {
  88. // No homepage query results in a 204, returning an empty string
  89. if (stateKey === 'savedQuery' && data === '') {
  90. this.setState({savedQuery: null});
  91. }
  92. }
  93. setSavedQuery = (newSavedQuery?: SavedQuery) => {
  94. this.setState({savedQuery: newSavedQuery});
  95. };
  96. renderBody(): React.ReactNode {
  97. const {savedQuery, loading} = this.state;
  98. const {organization} = this.props;
  99. let savedQueryWithDataset = savedQuery;
  100. if (
  101. organization.features.includes('performance-discover-dataset-selector') &&
  102. savedQuery
  103. ) {
  104. savedQueryWithDataset = getSavedQueryWithDataset(savedQuery);
  105. }
  106. return (
  107. <Results
  108. {...this.props}
  109. savedQuery={savedQueryWithDataset ?? undefined}
  110. loading={loading}
  111. setSavedQuery={this.setSavedQuery}
  112. isHomepage
  113. />
  114. );
  115. }
  116. }
  117. function HomepageContainer(props: Props) {
  118. return (
  119. <PageFiltersContainer skipInitializeUrlParams>
  120. <HomepageQueryAPI {...props} />
  121. </PageFiltersContainer>
  122. );
  123. }
  124. export default withApi(withOrganization(withPageFilters(HomepageContainer)));