@@ -23,17 +23,18 @@ const SearchModule: FC<SearchModuleProps> = ({ index }) => {
23
23
const dispatch = useAppDispatch ( ) ;
24
24
const search = useAppSelector ( ( state ) => state . search [ index ] ) ;
25
25
const [ pendingRequest , setPendingRequest ] = useState < NodeJS . Timeout | null > ( null ) ;
26
+ const [ prevIndex , setPrevIndex ] = useState < SearchIndex | null > ( null ) ;
26
27
27
28
const searchNames = useCallback (
28
- ( query : string ) => {
29
+ ( query : string , pageNumber : number , lastQuery ?: string ) => {
29
30
// Get all results only when query changes or user reaches the fourth page or after
30
31
const nameResults = wfs ( {
31
32
query : query ,
32
33
resultType : index === 'courses' ? 'COURSE' : 'INSTRUCTOR' ,
33
34
// Load INITIAL_MAX_PAGE pages first
34
35
// when user reaches the 4th page or after, load all results
35
36
numResults :
36
- search . lastQuery !== query || search . pageNumber < FULL_RESULT_THRESHOLD
37
+ lastQuery !== query || pageNumber < FULL_RESULT_THRESHOLD
37
38
? NUM_RESULTS_PER_PAGE * INITIAL_MAX_PAGE
38
39
: undefined ,
39
40
} ) ;
@@ -52,23 +53,30 @@ const SearchModule: FC<SearchModuleProps> = ({ index }) => {
52
53
}
53
54
dispatch ( setNames ( { index, names } ) ) ;
54
55
// reset page number and hasFullResults flag if query changes
55
- if ( query !== search . lastQuery ) {
56
+ if ( query !== lastQuery ) {
56
57
dispatch ( setPageNumber ( { index, pageNumber : 0 } ) ) ;
57
58
dispatch ( setHasFullResults ( { index, hasFullResults : false } ) ) ;
58
59
dispatch ( setLastQuery ( { index, lastQuery : query } ) ) ;
59
60
}
60
61
} ,
61
- [ dispatch , search . pageNumber , search . lastQuery , index ] ,
62
+ [ dispatch , index ] ,
62
63
) ;
63
64
65
+ // Search empty string to load some results on intial visit/when switching between courses and professors tabs
66
+ // make sure this runs before everything else for best performance and avoiding bugs
67
+ if ( index !== prevIndex ) {
68
+ setPrevIndex ( index ) ;
69
+ searchNames ( '' , 0 ) ;
70
+ }
71
+
64
72
const searchResults = useCallback ( async ( ) => {
65
73
if ( search . names . length === 0 ) {
66
74
dispatch ( setResults ( { index, results : [ ] } ) ) ;
67
75
return ;
68
76
}
69
77
if ( ! search . hasFullResults && search . pageNumber >= FULL_RESULT_THRESHOLD ) {
70
78
dispatch ( setHasFullResults ( { index, hasFullResults : true } ) ) ;
71
- searchNames ( search . lastQuery ) ;
79
+ searchNames ( search . lastQuery , search . pageNumber , search . lastQuery ) ;
72
80
return ;
73
81
}
74
82
// Get the subset of names based on the page
@@ -80,12 +88,7 @@ const SearchModule: FC<SearchModuleProps> = ({ index }) => {
80
88
dispatch ( setResults ( { index, results : Object . values ( results ) } ) ) ;
81
89
} , [ dispatch , search . names , search . pageNumber , index , search . hasFullResults , search . lastQuery , searchNames ] ) ;
82
90
83
- // Refresh search results when names and page number changes (controlled by searchResults dependency array)
84
- useEffect ( ( ) => {
85
- searchResults ( ) ;
86
- } , [ index , searchResults ] ) ;
87
-
88
- // reset page number and clear results on unmount
91
+ // clear results and reset page number when component unmounts
89
92
// results will persist otherwise, e.g. current page of results from catalogue carries over to roadmap search container
90
93
useEffect ( ( ) => {
91
94
return ( ) => {
@@ -96,17 +99,17 @@ const SearchModule: FC<SearchModuleProps> = ({ index }) => {
96
99
} ;
97
100
} , [ dispatch ] ) ;
98
101
99
- // Search empty string to load some results on intial visit/when switching between courses and professors tabs
102
+ // Refresh search results when names and page number changes (controlled by searchResults dependency array)
100
103
useEffect ( ( ) => {
101
- searchNames ( '' ) ;
102
- } , [ index , searchNames ] ) ;
104
+ searchResults ( ) ;
105
+ } , [ index , searchResults ] ) ;
103
106
104
107
const searchNamesAfterTimeout = ( query : string ) => {
105
108
if ( pendingRequest ) {
106
109
clearTimeout ( pendingRequest ) ;
107
110
}
108
111
const timeout = setTimeout ( ( ) => {
109
- searchNames ( query ) ;
112
+ searchNames ( query , 0 ) ;
110
113
setPendingRequest ( null ) ;
111
114
} , SEARCH_TIMEOUT_MS ) ;
112
115
setPendingRequest ( timeout ) ;
0 commit comments