1- import { useEffect } from " react" ;
2- import { useStore } from " @nanostores/react" ;
1+ import { useEffect , useState } from ' react' ;
2+ import { useStore } from ' @nanostores/react' ;
33import { $recentSearches } from "../../lib/stores/search.js" ;
4+ import RecentSearchesLoading from './recent-searches-loading' ;
45
5- /**
6- * Recent Searches Component - An Island that displays a user's last 5 searches
7- *
8- * @todo implement a default list instead of `null` when no `$recentSearch` is found
9- * @todo implement loading component to avoid flickering UI
10- */
116export default function RecentSearches ( ) {
127 const recentSearches = useStore ( $recentSearches ) ;
8+ const [ isLoading , setIsLoading ] = useState ( true ) ;
139
1410 useEffect ( ( ) => {
15- $recentSearches . set ( { ...JSON . parse ( localStorage . getItem ( "jargons.dev:recent_searches" ) ) } )
11+ const savedSearches = JSON . parse ( localStorage . getItem ( "jargons.dev:recent_searches" ) ) ;
12+ if ( savedSearches ) {
13+ $recentSearches . set ( savedSearches ) ;
14+ }
15+ setTimeout ( ( ) => {
16+ setIsLoading ( false ) ;
17+ } , 2000 )
1618 } , [ ] ) ;
1719
20+ if ( isLoading ) {
21+ return < RecentSearchesLoading recentSearches = { recentSearches } /> ;
22+ }
23+
1824 return Object . values ( recentSearches ) . length ? (
1925 < div className = "space-y-3 ml-2 mt-4 md:mt-6" >
20- < h2 className = "text-2xl md:text-4xl font-black" > Recent</ h2 >
21- < ol className = "space-y-1.5 underline" >
26+ < h2 className = "text-2xl md:text-4xl font-black" > Recent</ h2 >
27+ < ol className = "space-y-1.5 underline" >
2228 { Object . values ( recentSearches ) . slice ( 0 , 5 ) . map ( ( item , i ) => (
2329 < li key = { i } >
2430 < a href = { item . url } >
25- { item . word }
31+ { item . word }
2632 </ a >
2733 </ li >
2834 ) ) }
29- </ ol >
30- </ div >
35+ </ ol >
36+ </ div >
3137 ) : null ;
32- }
38+ }
0 commit comments