-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add loading component to recent searches island
- Loading branch information
1 parent
76b1889
commit 8e4e3e2
Showing
2 changed files
with
41 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
|
||
export default function RecentSearchesLoading ({ recentSearches }) { | ||
return ( | ||
<div className="space-y-3 ml-2 mt-4 md:mt-6"> | ||
<h2 className="text-2xl md:text-4xl font-black">Recent</h2> | ||
<ol className="space-y-1.5 underline"> | ||
{Object.values(recentSearches).slice(0, 5).map((item, i) => ( | ||
<li key={i}> | ||
<div | ||
className="placeholder-text bg-gray-300 rounded animate-pulse" | ||
style={{ width: `${item.word.length * 8}px`, height: '24px' }} | ||
></div> | ||
</li> | ||
))} | ||
</ol> | ||
</div> | ||
); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,38 @@ | ||
import { useEffect } from "react"; | ||
import { useStore } from "@nanostores/react"; | ||
import { useEffect, useState } from 'react'; | ||
import { useStore } from '@nanostores/react'; | ||
import { $recentSearches } from "../../lib/stores/search.js"; | ||
import RecentSearchesLoading from './recent-searches-loading'; | ||
|
||
/** | ||
* Recent Searches Component - An Island that displays a user's last 5 searches | ||
* | ||
* @todo implement a default list instead of `null` when no `$recentSearch` is found | ||
* @todo implement loading component to avoid flickering UI | ||
*/ | ||
export default function RecentSearches() { | ||
const recentSearches = useStore($recentSearches); | ||
const [isLoading, setIsLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
$recentSearches.set({...JSON.parse(localStorage.getItem("jargons.dev:recent_searches"))}) | ||
const savedSearches = JSON.parse(localStorage.getItem("jargons.dev:recent_searches")); | ||
if (savedSearches) { | ||
$recentSearches.set(savedSearches); | ||
} | ||
setTimeout(()=>{ | ||
setIsLoading(false); | ||
},2000) | ||
}, []); | ||
|
||
if (isLoading) { | ||
return <RecentSearchesLoading recentSearches={recentSearches} />; | ||
} | ||
|
||
return Object.values(recentSearches).length ? ( | ||
<div className="space-y-3 ml-2 mt-4 md:mt-6"> | ||
<h2 className="text-2xl md:text-4xl font-black">Recent</h2> | ||
<ol className="space-y-1.5 underline"> | ||
<h2 className="text-2xl md:text-4xl font-black">Recent</h2> | ||
<ol className="space-y-1.5 underline"> | ||
{Object.values(recentSearches).slice(0, 5).map((item, i) => ( | ||
<li key={i}> | ||
<a href={item.url}> | ||
{ item.word } | ||
{item.word} | ||
</a> | ||
</li> | ||
))} | ||
</ol> | ||
</div> | ||
</ol> | ||
</div> | ||
) : null; | ||
} | ||
} |