Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add loading component to recent search #94

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/components/islands/recent-searches-loading.jsx
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>
);
};

38 changes: 23 additions & 15 deletions src/components/islands/recent-searches.jsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
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);
}
const timer = setTimeout(() => {
setIsLoading(false);
}, 2000);

return () => clearTimeout(timer);
}, []);

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;
Copy link
Member

@babblebey babblebey Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recentSearches will be false on the server, so the LoadingComponent in place of null would be loaded from the server until hydrated on the client to either load the recent searches in local storage or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay... But if the local storage is empty, the loading component will remain on the page

Copy link
Member

@babblebey babblebey Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my! that is true 🤔

So, we could use an Effect to understand when the page is loaded then kick the LoadingComponent or RecentSearchComponent or null on that, something like....

const [ready, setReady] = useState(false);
useEffect(() => {
  setReady(true);
}, []);

// This loads from the server while page isn't ready or not hydrated
if (!ready) {
  return <LoadingComponent />
}

// After Hydration, useEffect kicks in to either display the recentSearch if found or not; removing the LoadingComponent for sure
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">
      {Object.values(recentSearches).slice(0, 5).map((item, i) => (
        <li key={i}>
          <a href={item.url}>
            {item.word}
          </a>
        </li>
      ))}
    </ol>
  </div>
) : null;

Thinking out loud here 😉

}
}