homepage.tsx 4.3 KB

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