@@ -44,8 +44,8 @@ export default function DiscoverPage() {
4444 const [ searchState , setSearchState ] = useState < SearchState > ( {
4545 mode : "topic" ,
4646 topics : [ ] ,
47- timeFilter : "7d " ,
48- sortBy : "score " ,
47+ timeFilter : "24h " ,
48+ sortBy : "time " ,
4949 textQuery : "" ,
5050 articles : [ ] ,
5151 isLoading : false ,
@@ -80,6 +80,78 @@ export default function DiscoverPage() {
8080 fetchStats ( )
8181 } , [ ] )
8282
83+ // Function to fetch recent articles
84+ const fetchRecentArticles = useCallback ( async ( ) => {
85+ setSearchState ( ( prev ) => {
86+ // Only fetch if no topics selected and no text query
87+ if ( prev . topics . length === 0 && ! prev . textQuery ) {
88+ // Start loading
89+ const loadingState = { ...prev , isLoading : true , error : null }
90+
91+ // Fetch articles asynchronously
92+ const params = new URLSearchParams ( {
93+ timeFilter : prev . timeFilter ,
94+ sortBy : "time" ,
95+ } )
96+
97+ fetch ( `/api/articles?${ params } ` )
98+ . then ( ( response ) => {
99+ if ( ! response . ok ) {
100+ return response . json ( ) . then ( ( errorData ) => {
101+ throw new Error ( errorData . error || "Failed to fetch articles" )
102+ } )
103+ }
104+ return response . json ( )
105+ } )
106+ . then ( ( data ) => {
107+ setSearchState ( ( current ) => ( {
108+ ...current ,
109+ articles : data . articles ,
110+ isLoading : false ,
111+ error : null ,
112+ } ) )
113+ } )
114+ . catch ( ( error ) => {
115+ console . error ( "Error fetching recent articles:" , error )
116+ setSearchState ( ( current ) => ( {
117+ ...current ,
118+ isLoading : false ,
119+ error :
120+ error instanceof Error
121+ ? error . message
122+ : "Failed to fetch recent articles" ,
123+ } ) )
124+ } )
125+
126+ return loadingState
127+ }
128+ return prev
129+ } )
130+ } , [ ] )
131+
132+ // Fetch recent articles on mount when no search is active
133+ useEffect ( ( ) => {
134+ fetchRecentArticles ( )
135+ // eslint-disable-next-line react-hooks/exhaustive-deps
136+ } , [ ] ) // Only run on mount
137+
138+ // Refetch recent articles when time filter changes (if no active search)
139+ useEffect ( ( ) => {
140+ if (
141+ shouldTriggerSearch . current &&
142+ searchState . topics . length === 0 &&
143+ ! searchState . textQuery
144+ ) {
145+ shouldTriggerSearch . current = false
146+ fetchRecentArticles ( )
147+ }
148+ } , [
149+ searchState . timeFilter ,
150+ searchState . topics . length ,
151+ searchState . textQuery ,
152+ fetchRecentArticles ,
153+ ] )
154+
83155 const performTextSearch = useCallback (
84156 async ( query : string ) => {
85157 setSearchState ( ( prev ) => ( { ...prev , isLoading : true , error : null } ) )
@@ -184,6 +256,10 @@ export default function DiscoverPage() {
184256 articles : [ ] ,
185257 error : null ,
186258 } ) )
259+ // If switching away from search with no active search, fetch recent articles
260+ if ( searchState . topics . length === 0 && ! searchState . textQuery ) {
261+ shouldTriggerSearch . current = true
262+ }
187263 }
188264
189265 const handleTopicsChange = ( topics : string [ ] ) => {
@@ -192,6 +268,10 @@ export default function DiscoverPage() {
192268
193269 const handleTimeFilterChange = ( timeFilter : TimeFilter ) => {
194270 setSearchState ( ( prev ) => ( { ...prev , timeFilter } ) )
271+ // If no active search, refetch recent articles with new time filter
272+ if ( searchState . topics . length === 0 && ! searchState . textQuery ) {
273+ shouldTriggerSearch . current = true
274+ }
195275 }
196276
197277 const handleSortChange = ( sortBy : SortBy ) => {
@@ -210,6 +290,9 @@ export default function DiscoverPage() {
210290 searchState . textQuery . length >= 3
211291 ) {
212292 performTextSearch ( searchState . textQuery )
293+ } else if ( searchState . topics . length === 0 && ! searchState . textQuery ) {
294+ // Refetch recent articles if no active search
295+ fetchRecentArticles ( )
213296 }
214297 }
215298 } , [
@@ -219,6 +302,7 @@ export default function DiscoverPage() {
219302 searchState . textQuery ,
220303 performTopicSearch ,
221304 performTextSearch ,
305+ fetchRecentArticles ,
222306 ] )
223307
224308 const handleTextQueryChange = ( textQuery : string ) => {
@@ -275,7 +359,7 @@ export default function DiscoverPage() {
275359 : "text-foreground opacity-50"
276360 } `}
277361 >
278- < TrendingUp className = "h-4 w-4 text-primary sm:size-6 " />
362+ < TrendingUp className = "sm:size-6 h-4 w-4 text-primary" />
279363 < span className = "whitespace-nowrap" > By Topics</ span >
280364 </ Button >
281365 < Button
@@ -288,7 +372,7 @@ export default function DiscoverPage() {
288372 : "text-foreground opacity-50"
289373 } `}
290374 >
291- < Search className = "h-4 w-4 text-primary sm:size-6 " />
375+ < Search className = "sm:size-6 h-4 w-4 text-primary" />
292376 < span className = "whitespace-nowrap" > Vector Search</ span >
293377 </ Button >
294378 </ div >
@@ -406,6 +490,28 @@ export default function DiscoverPage() {
406490 </ Card >
407491 ) }
408492
493+ { /* Default View Message */ }
494+ { searchState . topics . length === 0 &&
495+ ! searchState . textQuery &&
496+ ! searchState . isLoading &&
497+ searchState . articles . length > 0 && (
498+ < Card className = "mb-6 border-blue-200 bg-blue-50 dark:border-blue-800 dark:bg-blue-900/20" >
499+ < CardContent className = "p-4" >
500+ < p className = "text-sm text-blue-800 dark:text-blue-200" >
501+ < span className = "font-medium" >
502+ Displaying most recent news from{ " " }
503+ { TIME_FILTER_OPTIONS . find (
504+ ( opt ) => opt . value === searchState . timeFilter
505+ ) ?. label . toLowerCase ( ) }
506+ .
507+ </ span > { " " }
508+ Select topics and search articles, or adjust the time filter
509+ to explore more content.
510+ </ p >
511+ </ CardContent >
512+ </ Card >
513+ ) }
514+
409515 { /* Active Filters Display */ }
410516 { ( searchState . topics . length > 0 || searchState . textQuery ) && (
411517 < Card className = "mb-6" >
@@ -429,7 +535,7 @@ export default function DiscoverPage() {
429535 ) }
430536
431537 < Badge variant = "outline" >
432- < Clock className = "mr-1 w -3 h -3" />
538+ < Clock className = "mr-1 h -3 w -3" />
433539 {
434540 TIME_FILTER_OPTIONS . find (
435541 ( opt ) => opt . value === searchState . timeFilter
@@ -438,7 +544,7 @@ export default function DiscoverPage() {
438544 </ Badge >
439545
440546 < Badge variant = "outline" >
441- < Filter className = "mr-1 w -3 h -3" />
547+ < Filter className = "mr-1 h -3 w -3" />
442548 { searchState . sortBy === "score"
443549 ? "Most Relevant"
444550 : "Most Recent" }
0 commit comments