Skip to content

Commit b91a02f

Browse files
committed
Consolidated snippet and search snippet list
1 parent 3474894 commit b91a02f

File tree

3 files changed

+18
-77
lines changed

3 files changed

+18
-77
lines changed

src/components/SearchSnippetList.tsx

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/components/SnippetList.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
import { useState } from "react";
1+
import { useState, useMemo } from "react";
22
import { SnippetType } from "../types";
33
import { useAppContext } from "../contexts/AppContext";
44
import { useSnippets } from "../hooks/useSnippets";
55

66
import SnippetModal from "./SnippetModal";
7-
import { LeftAngleArrowIcon } from "./Icons";
87

9-
const SnippetList = () => {
8+
const SnippetList = ({ query }: { query?: string | null }) => {
109
const { language, snippet, setSnippet } = useAppContext();
11-
const { fetchedSnippets } = useSnippets();
10+
const { fetchedSnippets, loading } = useSnippets();
1211
const [isModalOpen, setIsModalOpen] = useState(false);
1312

14-
if (!fetchedSnippets)
15-
return (
16-
<div>
17-
<LeftAngleArrowIcon />
18-
</div>
13+
const filteredSnippets = useMemo(() => {
14+
if (!query) return fetchedSnippets;
15+
return fetchedSnippets.filter((snippet) =>
16+
snippet.title.toLowerCase().includes(query.toLowerCase())
1917
);
18+
}, [fetchedSnippets, query]);
19+
20+
if (loading) return <div>Loading...</div>;
21+
if (!filteredSnippets || filteredSnippets.length === 0)
22+
return <div>No results found for "{query}"</div>;
2023

2124
const handleOpenModal = (activeSnippet: SnippetType) => {
2225
setIsModalOpen(true);
@@ -31,7 +34,7 @@ const SnippetList = () => {
3134
return (
3235
<>
3336
<ul role="list" className="snippets">
34-
{fetchedSnippets.map((snippet, idx) => (
37+
{filteredSnippets.map((snippet, idx) => (
3538
<li key={idx}>
3639
<button
3740
className="snippet | flow"
@@ -41,8 +44,10 @@ const SnippetList = () => {
4144
<div className="snippet__preview">
4245
<img src={language.icon} alt={language.lang} />
4346
</div>
44-
4547
<h3 className="snippet__title">{snippet.title}</h3>
48+
{query && (
49+
<p className="snippet__description">{snippet.description}</p>
50+
)}
4651
</button>
4752
</li>
4853
))}

src/pages/SearchPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useSearchParams } from "react-router-dom";
2-
import SearchSnippetList from "../components/SearchSnippetList";
2+
import SnippetList from "../components/SnippetList";
33
import Sidebar from "../layouts/Sidebar";
44

55
const SearchPage = () => {
@@ -11,7 +11,7 @@ const SearchPage = () => {
1111
<Sidebar />
1212
<section className="flow">
1313
<h2 className="section-title">Search Results for: {query}</h2>
14-
<SearchSnippetList query={query} />
14+
<SnippetList query={query} />
1515
</section>
1616
</>
1717
);

0 commit comments

Comments
 (0)