homepage.tsx 4.2 KB

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