import {browserHistory, InjectedRouter} from 'react-router'; import {Location} from 'history'; import {Client} from 'sentry/api'; import AsyncComponent from 'sentry/components/asyncComponent'; import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container'; import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse'; import {Organization, PageFilters, SavedQuery} from 'sentry/types'; import withApi from 'sentry/utils/withApi'; import withOrganization from 'sentry/utils/withOrganization'; import withPageFilters from 'sentry/utils/withPageFilters'; import {Results} from './results'; type Props = { api: Client; loading: boolean; location: Location; organization: Organization; router: InjectedRouter; selection: PageFilters; setSavedQuery: (savedQuery: SavedQuery) => void; homepageQuery?: SavedQuery; savedQuery?: SavedQuery; }; type HomepageQueryState = AsyncComponent['state'] & { // Used to trigger intial redirect for the saved query hasLoaded: boolean; savedQuery?: SavedQuery | null; }; class HomepageQueryAPI extends AsyncComponent { getEndpoints(): ReturnType { const {organization} = this.props; const endpoints: ReturnType = []; if ( organization.features.includes('discover-query-builder-as-landing-page') && organization.features.includes('discover-query') ) { endpoints.push([ 'savedQuery', `/organizations/${organization.slug}/discover/homepage/`, ]); } return endpoints; } onRequestSuccess = ({stateKey, data}) => { const {location} = this.props; const {hasLoaded} = this.state; if (stateKey === 'savedQuery' && !hasLoaded) { this.setState({hasLoaded: true}); const normalizedDateTime = normalizeDateTimeParams({ start: data.start, end: data.end, statsPeriod: data.range, utc: data.utc, }); browserHistory.replace({ pathname: location.pathname, query: { project: data.projects ?? [], environment: data.environment ?? [], ...normalizedDateTime, ...location.query, }, }); } }; setSavedQuery = (newSavedQuery: SavedQuery) => { this.setState({savedQuery: newSavedQuery}); }; renderLoading() { return this.renderBody(); } renderBody(): React.ReactNode { const {savedQuery, loading} = this.state; return ( ); } } function HomepageContainer(props: Props) { return ( ); } export default withApi(withOrganization(withPageFilters(HomepageContainer)));