Skip to content

Commit 65e35d1

Browse files
feat(search): implement recentSearches to hold 5 maximum entries (#115)
- Removed redundant `.slice()` method call to improve code clarity - Implemented a 5-item limit for recent searches to prevent unlimited growth ### Related Issue Fixes #70 --------- Co-authored-by: Olabode Lawal-Shittabey <babblebey@gmail.com>
1 parent 7f5ffb9 commit 65e35d1

2 files changed

Lines changed: 25 additions & 19 deletions

File tree

src/components/islands/recent-searches.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export default function RecentSearches() {
1717

1818
return Object.values(recentSearches).length ? (
1919
<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">
20+
<h2 className="text-2xl md:text-4xl font-black">Recent</h2>
21+
<ol className="space-y-1.5 underline">
2222
{Object.values(recentSearches).slice(0, 5).map((item, i) => (
2323
<li key={i}>
2424
<a href={item.url}>

src/lib/stores/search.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ export const $recentSearches = map({});
1414
/**
1515
* Add search term to recent search history
1616
* @param {SearchedItem} item
17-
*
18-
* @todo implement logic to allow holding maximum of 5 words by removing older words when new a one gets added
1917
*/
2018
export function $addToRecentSearchesFn({ word, url }) {
21-
// Re-initialise the state with current localStorage value
22-
$recentSearches.set({...JSON.parse(localStorage.getItem("jargons.dev:recent_searches"))});
19+
// Re-initialise the state with the current localStorage value
20+
const storedSearches = JSON.parse(localStorage.getItem("jargons.dev:recent_searches")) || {};
21+
$recentSearches.set({ ...storedSearches });
2322

2423
const lowercaseKey = word.toLowerCase();
2524
const key = lowercaseKey.includes(" ") ? lowercaseKey.split(" ").join("-") : lowercaseKey;
26-
const isInRecentSearch = $recentSearches.get()[key];
27-
28-
if (!isInRecentSearch) {
29-
const recentSearchesCopy = $recentSearches.get();
30-
recentSearchesCopy[key] = {
31-
word,
32-
url
33-
};
34-
$recentSearches.set({...recentSearchesCopy});
35-
} else {
36-
return;
25+
26+
const recentSearchesCopy = $recentSearches.get();
27+
28+
// Remove Search Entry If it already exists
29+
if (recentSearchesCopy[key]) {
30+
delete recentSearchesCopy[key];
31+
}
32+
33+
const searchEntries = Object.entries(recentSearchesCopy);
34+
35+
// Remove one Search Entry when list reach a count of 5
36+
if (searchEntries.length >= 5) {
37+
searchEntries.pop();
3738
}
38-
39-
localStorage.setItem("jargons.dev:recent_searches", JSON.stringify($recentSearches.get()));
39+
40+
const updatedEntries = [[key, { word, url }], ...searchEntries];
41+
42+
const updatedSearches = Object.fromEntries(updatedEntries);
43+
$recentSearches.set(updatedSearches);
44+
45+
localStorage.setItem("jargons.dev:recent_searches", JSON.stringify(updatedSearches));
4046
}

0 commit comments

Comments
 (0)