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 all 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
26 changes: 26 additions & 0 deletions src/components/islands/loading-component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


export default function LoadingComponent() {


return (
<div className="space-y-3 ml-2 mt-4 md:mt-6 loading">
<h2 className="font-black bg-gray-300 rounded animate-pulse" style={{ height: "30px", width: "150px" }}>

</h2>

{/* List of recent searches or placeholders */}
<ol className="space-y-1.5 underline">
{[...Array(5)].map((_, i) => (
<li key={i}>
<div
className="placeholder-text bg-gray-300 rounded animate-pulse"
style={{ width: "100px", height: "24px" }}
></div>
</li>
))}

</ol>
</div>
);
}
35 changes: 19 additions & 16 deletions src/components/islands/recent-searches.jsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
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";

/**
* 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);

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 loader = document.querySelector(".loading");
if (loader) loader.remove();

}, []);



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 😉

}
}
7 changes: 7 additions & 0 deletions src/components/recent-searches.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import RecentSearch from "./islands/recent-searches";
import LoadingComponent from "./islands/loading-component";
---

<LoadingComponent />
<RecentSearch client:load />
54 changes: 28 additions & 26 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@
import { getCollection } from "astro:content";
import BaseLayout from "../layouts/base.astro";
import Search from "../components/islands/search.jsx";
import RecentSearches from "../components/islands/recent-searches.jsx";

import RecentSearches from "../components/recent-searches.astro";
const dictionary = await getCollection("dictionary");
---

<BaseLayout
pageTitle="Jargons.dev"
subtitle="Simplified Meaning & Definition to Technical Terms"
<BaseLayout
pageTitle="Jargons.dev"
subtitle="Simplified Meaning & Definition to Technical Terms"
>
<main class="flex flex-col max-w-screen-lg p-5 justify-center mx-auto min-h-screen">
<!-- Title -->
<div class="mb-4 md:mb-6">
<h1 class="relative text-5xl md:text-7xl lg:text-8xl font-black w-fit">
<a href="/" class="absolute font-normal text-[0.6rem] md:text-xs right-0 md:top-1.5 lg:top-3">
jargons.dev
</a>
Dictionary
</h1>
</div>

<!-- Search -->
<Search triggerSize="md" dictionary={dictionary} client:load />
<main
class="flex flex-col max-w-screen-lg p-5 justify-center mx-auto min-h-screen"
>
<!-- Title -->
<div class="mb-4 md:mb-6">
<h1 class="relative text-5xl md:text-7xl lg:text-8xl font-black w-fit">
<a
href="/"
class="absolute font-normal text-[0.6rem] md:text-xs right-0 md:top-1.5 lg:top-3"
>
jargons.dev
</a>
Dictionary
</h1>
</div>

<!-- Search -->
<Search triggerSize="md" dictionary={dictionary} client:load />

<!-- Browse words -->
<div class="ml-2 mt-4 md:mt-6" id="whirl-browse-words">
<a href="/browse">
Browse Words
</a>
<!-- Browse words -->
<div class="ml-2 mt-4 md:mt-6" id="whirl-browse-words">
<a href="/browse"> Browse Words </a>
</div>

<!-- Recent Word Look Up -->
<RecentSearches client:load />
</main>
<!-- Recent Word Look Up -->
<RecentSearches />
</main>
</BaseLayout>