1
+ import { useEffect } from 'react' ;
1
2
import type { Location } from 'history' ;
2
3
3
4
import type { Client } from 'sentry/api' ;
4
- import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent' ;
5
+ import LoadingError from 'sentry/components/loadingError' ;
6
+ import LoadingIndicator from 'sentry/components/loadingIndicator' ;
5
7
import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container' ;
6
8
import {
7
9
getDatetimeFromState ,
@@ -11,8 +13,12 @@ import {getPageFilterStorage} from 'sentry/components/organizations/pageFilters/
11
13
import type { PageFilters } from 'sentry/types/core' ;
12
14
import type { InjectedRouter } from 'sentry/types/legacyReactRouter' ;
13
15
import type { Organization , SavedQuery } from 'sentry/types/organization' ;
14
- import { browserHistory } from 'sentry/utils/browserHistory' ;
15
16
import EventView from 'sentry/utils/discover/eventView' ;
17
+ import { type ApiQueryKey , useApiQuery , useQueryClient } from 'sentry/utils/queryClient' ;
18
+ import { useLocation } from 'sentry/utils/useLocation' ;
19
+ import { useNavigate } from 'sentry/utils/useNavigate' ;
20
+ import useOrganization from 'sentry/utils/useOrganization' ;
21
+ import usePrevious from 'sentry/utils/usePrevious' ;
16
22
import withApi from 'sentry/utils/withApi' ;
17
23
import withOrganization from 'sentry/utils/withOrganization' ;
18
24
import withPageFilters from 'sentry/utils/withPageFilters' ;
@@ -30,27 +36,42 @@ type Props = {
30
36
setSavedQuery : ( savedQuery : SavedQuery ) => void ;
31
37
} ;
32
38
33
- type HomepageQueryState = DeprecatedAsyncComponent [ 'state' ] & {
34
- savedQuery ?: SavedQuery | null ;
35
- starfishResult ?: null ;
36
- } ;
39
+ function makeDiscoverHomepageQueryKey ( organization : Organization ) : ApiQueryKey {
40
+ return [ `/organizations/${ organization . slug } /discover/homepage/` ] ;
41
+ }
37
42
38
- class HomepageQueryAPI extends DeprecatedAsyncComponent < Props , HomepageQueryState > {
39
- shouldReload = true ;
43
+ function Homepage ( props : Props ) {
44
+ const organization = useOrganization ( ) ;
45
+ const queryClient = useQueryClient ( ) ;
46
+ const location = useLocation ( ) ;
47
+ const navigate = useNavigate ( ) ;
48
+ const { data, isLoading, isError, refetch} = useApiQuery < SavedQuery > (
49
+ makeDiscoverHomepageQueryKey ( organization ) ,
50
+ {
51
+ staleTime : 0 ,
52
+ enabled : organization . features . includes ( 'discover-query' ) ,
53
+ }
54
+ ) ;
40
55
41
- componentDidUpdate ( _ : any , prevState : any ) {
42
- const hasFetchedSavedQuery = ! prevState . savedQuery && this . state . savedQuery ;
43
- const hasInitiallyLoaded = prevState . loading && ! this . state . loading ;
44
- const sidebarClicked = this . state . savedQuery && this . props . location . search === '' ;
45
- const hasValidEventViewInURL = EventView . fromLocation ( this . props . location ) . isValid ( ) ;
56
+ const savedQuery = organization . features . includes (
57
+ 'performance-discover-dataset-selector'
58
+ )
59
+ ? getSavedQueryWithDataset ( data )
60
+ : data ;
61
+
62
+ const previousSavedQuery = usePrevious ( savedQuery ) ;
63
+
64
+ useEffect ( ( ) => {
65
+ const hasFetchedSavedQuery = ! previousSavedQuery && savedQuery ;
66
+ const sidebarClicked = savedQuery && location . search === '' ;
67
+ const hasValidEventViewInURL = EventView . fromLocation ( location ) . isValid ( ) ;
46
68
47
69
if (
48
- this . state . savedQuery &&
49
- ( ( hasInitiallyLoaded && hasFetchedSavedQuery && ! hasValidEventViewInURL ) ||
50
- sidebarClicked )
70
+ savedQuery &&
71
+ ( ( hasFetchedSavedQuery && ! hasValidEventViewInURL ) || sidebarClicked )
51
72
) {
52
- const eventView = EventView . fromSavedQuery ( this . state . savedQuery ) ;
53
- const pageFilterState = getPageFilterStorage ( this . props . organization . slug ) ;
73
+ const eventView = EventView . fromSavedQuery ( savedQuery ) ;
74
+ const pageFilterState = getPageFilterStorage ( organization . slug ) ;
54
75
let query = {
55
76
...eventView . generateQueryStringObject ( ) ,
56
77
} ;
@@ -79,75 +100,46 @@ class HomepageQueryAPI extends DeprecatedAsyncComponent<Props, HomepageQueryStat
79
100
} ) ;
80
101
}
81
102
82
- browserHistory . replace ( {
83
- ...this . props . location ,
84
- query : {
85
- ...query ,
86
- queryDataset : this . state . savedQuery ?. queryDataset ,
103
+ navigate (
104
+ {
105
+ ...location ,
106
+ query : {
107
+ ...query ,
108
+ queryDataset : savedQuery ?. queryDataset ,
109
+ } ,
87
110
} ,
88
- } ) ;
111
+ { replace : true }
112
+ ) ;
89
113
}
90
- }
114
+ } , [ savedQuery , location , previousSavedQuery , navigate , organization . slug ] ) ;
91
115
92
- getEndpoints ( ) : ReturnType < DeprecatedAsyncComponent [ 'getEndpoints' ] > {
93
- const { organization} = this . props ;
94
-
95
- const endpoints : ReturnType < DeprecatedAsyncComponent [ 'getEndpoints' ] > = [ ] ;
96
- if ( organization . features . includes ( 'discover-query' ) ) {
97
- endpoints . push ( [
98
- 'savedQuery' ,
99
- `/organizations/${ organization . slug } /discover/homepage/` ,
100
- ] ) ;
101
- }
102
- return endpoints ;
116
+ if ( isLoading ) {
117
+ return < LoadingIndicator /> ;
103
118
}
104
119
105
- onRequestSuccess ( { stateKey, data} : any ) {
106
- const { organization} = this . props ;
107
- // No homepage query results in a 204, returning an empty string
108
- if ( stateKey === 'savedQuery' && data === '' ) {
109
- this . setState ( { savedQuery : null } ) ;
110
- return ;
111
- }
112
- if ( stateKey === 'savedQuery' ) {
113
- this . setState ( {
114
- savedQuery : organization . features . includes (
115
- 'performance-discover-dataset-selector'
116
- )
117
- ? getSavedQueryWithDataset ( data )
118
- : data ,
119
- } ) ;
120
- }
120
+ if ( isError ) {
121
+ return < LoadingError onRetry = { refetch } /> ;
121
122
}
122
123
123
- setSavedQuery = ( newSavedQuery ?: SavedQuery ) => {
124
- const { organization} = this . props ;
125
- this . setState ( {
126
- savedQuery : organization . features . includes ( 'performance-discover-dataset-selector' )
127
- ? ( getSavedQueryWithDataset ( newSavedQuery ) as SavedQuery )
128
- : newSavedQuery ,
129
- } ) ;
124
+ const setSavedQuery = ( newSavedQuery ?: SavedQuery ) => {
125
+ queryClient . setQueryData ( makeDiscoverHomepageQueryKey ( organization ) , newSavedQuery ) ;
130
126
} ;
131
127
132
- renderBody ( ) : React . ReactNode {
133
- const { savedQuery, loading} = this . state ;
134
-
135
- return (
136
- < Results
137
- { ...this . props }
138
- savedQuery = { savedQuery ?? undefined }
139
- loading = { loading }
140
- setSavedQuery = { this . setSavedQuery }
141
- isHomepage
142
- />
143
- ) ;
144
- }
128
+ return (
129
+ < Results
130
+ { ...props }
131
+ savedQuery = { savedQuery }
132
+ loading = { isLoading }
133
+ setSavedQuery = { setSavedQuery }
134
+ isHomepage
135
+ />
136
+ ) ;
145
137
}
146
138
147
139
function HomepageContainer ( props : Props ) {
148
140
return (
149
141
< PageFiltersContainer skipInitializeUrlParams >
150
- < HomepageQueryAPI { ...props } />
142
+ < Homepage { ...props } />
151
143
</ PageFiltersContainer >
152
144
) ;
153
145
}
0 commit comments