Skip to content

Commit 8e4e3e2

Browse files
feat: add loading component to recent searches island
1 parent 76b1889 commit 8e4e3e2

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
export default function RecentSearchesLoading ({ recentSearches }) {
4+
return (
5+
<div className="space-y-3 ml-2 mt-4 md:mt-6">
6+
<h2 className="text-2xl md:text-4xl font-black">Recent</h2>
7+
<ol className="space-y-1.5 underline">
8+
{Object.values(recentSearches).slice(0, 5).map((item, i) => (
9+
<li key={i}>
10+
<div
11+
className="placeholder-text bg-gray-300 rounded animate-pulse"
12+
style={{ width: `${item.word.length * 8}px`, height: '24px' }}
13+
></div>
14+
</li>
15+
))}
16+
</ol>
17+
</div>
18+
);
19+
};
20+
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
import { useEffect } from "react";
2-
import { useStore } from "@nanostores/react";
1+
import { useEffect, useState } from 'react';
2+
import { useStore } from '@nanostores/react';
33
import { $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-
*/
116
export 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

Comments
 (0)