homepage.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {browserHistory, InjectedRouter} from 'react-router';
  2. import {Location} from 'history';
  3. import {Client} from 'sentry/api';
  4. import AsyncComponent from 'sentry/components/asyncComponent';
  5. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  6. import {
  7. getDatetimeFromState,
  8. normalizeDateTimeParams,
  9. normalizeDateTimeString,
  10. } from 'sentry/components/organizations/pageFilters/parse';
  11. import {getPageFilterStorage} from 'sentry/components/organizations/pageFilters/persistence';
  12. import {Organization, PageFilters, SavedQuery} from 'sentry/types';
  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 = AsyncComponent['state'] & {
  28. savedQuery?: SavedQuery | null;
  29. starfishResult?: null;
  30. };
  31. class HomepageQueryAPI extends AsyncComponent<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<AsyncComponent['getEndpoints']> {
  76. const {organization, selection} = this.props;
  77. const {projects, environments, datetime} = selection;
  78. const commonQuery = {
  79. environment: environments,
  80. project: projects.map(proj => String(proj)),
  81. };
  82. const endpoints: ReturnType<AsyncComponent['getEndpoints']> = [];
  83. if (organization.features.includes('discover-query')) {
  84. endpoints.push([
  85. 'savedQuery',
  86. `/organizations/${organization.slug}/discover/homepage/`,
  87. ]);
  88. }
  89. if (organization.features.includes('starfish-test-endpoint')) {
  90. endpoints.push([
  91. 'starfishResult',
  92. `/organizations/${organization.slug}/events-starfish/`,
  93. {query: {...commonQuery, ...normalizeDateTimeParams(datetime)}},
  94. ]);
  95. }
  96. return endpoints;
  97. }
  98. onRequestSuccess({stateKey, data}) {
  99. // No homepage query results in a 204, returning an empty string
  100. if (stateKey === 'savedQuery' && data === '') {
  101. this.setState({savedQuery: null});
  102. }
  103. }
  104. setSavedQuery = (newSavedQuery?: SavedQuery) => {
  105. this.setState({savedQuery: newSavedQuery});
  106. };
  107. renderBody(): React.ReactNode {
  108. const {savedQuery, loading} = this.state;
  109. return (
  110. <Results
  111. {...this.props}
  112. savedQuery={savedQuery ?? undefined}
  113. loading={loading}
  114. setSavedQuery={this.setSavedQuery}
  115. isHomepage
  116. />
  117. );
  118. }
  119. }
  120. function HomepageContainer(props: Props) {
  121. return (
  122. <PageFiltersContainer skipInitializeUrlParams>
  123. <HomepageQueryAPI {...props} />
  124. </PageFiltersContainer>
  125. );
  126. }
  127. export default withApi(withOrganization(withPageFilters(HomepageContainer)));