homepage.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  7. import {Organization, PageFilters, SavedQuery} from 'sentry/types';
  8. import withApi from 'sentry/utils/withApi';
  9. import withOrganization from 'sentry/utils/withOrganization';
  10. import withPageFilters from 'sentry/utils/withPageFilters';
  11. import {Results} from './results';
  12. type Props = {
  13. api: Client;
  14. loading: boolean;
  15. location: Location;
  16. organization: Organization;
  17. router: InjectedRouter;
  18. selection: PageFilters;
  19. setSavedQuery: (savedQuery: SavedQuery) => void;
  20. homepageQuery?: SavedQuery;
  21. savedQuery?: SavedQuery;
  22. };
  23. type HomepageQueryState = AsyncComponent['state'] & {
  24. // Used to trigger intial redirect for the saved query
  25. hasLoaded: boolean;
  26. savedQuery?: SavedQuery | null;
  27. };
  28. class HomepageQueryAPI extends AsyncComponent<Props, HomepageQueryState> {
  29. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  30. const {organization} = this.props;
  31. const endpoints: ReturnType<AsyncComponent['getEndpoints']> = [];
  32. if (
  33. organization.features.includes('discover-query-builder-as-landing-page') &&
  34. organization.features.includes('discover-query')
  35. ) {
  36. endpoints.push([
  37. 'savedQuery',
  38. `/organizations/${organization.slug}/discover/homepage/`,
  39. ]);
  40. }
  41. return endpoints;
  42. }
  43. onRequestSuccess = ({stateKey, data}) => {
  44. const {location} = this.props;
  45. const {hasLoaded} = this.state;
  46. if (stateKey === 'savedQuery' && !hasLoaded) {
  47. this.setState({hasLoaded: true});
  48. const normalizedDateTime = normalizeDateTimeParams({
  49. start: data.start,
  50. end: data.end,
  51. statsPeriod: data.range,
  52. utc: data.utc,
  53. });
  54. browserHistory.replace({
  55. pathname: location.pathname,
  56. query: {
  57. project: data.projects ?? [],
  58. environment: data.environment ?? [],
  59. ...normalizedDateTime,
  60. ...location.query,
  61. },
  62. });
  63. }
  64. };
  65. setSavedQuery = (newSavedQuery: SavedQuery) => {
  66. this.setState({savedQuery: newSavedQuery});
  67. };
  68. renderLoading() {
  69. return this.renderBody();
  70. }
  71. renderBody(): React.ReactNode {
  72. const {savedQuery, loading} = this.state;
  73. return (
  74. <Results
  75. {...this.props}
  76. savedQuery={savedQuery ?? undefined}
  77. loading={loading}
  78. setSavedQuery={this.setSavedQuery}
  79. isHomepage
  80. />
  81. );
  82. }
  83. }
  84. function HomepageContainer(props: Props) {
  85. return (
  86. <PageFiltersContainer
  87. skipLoadLastUsed={
  88. props.organization.features.includes('global-views') && !!props.savedQuery
  89. }
  90. >
  91. <HomepageQueryAPI {...props} />
  92. </PageFiltersContainer>
  93. );
  94. }
  95. export default withApi(withOrganization(withPageFilters(HomepageContainer)));