Skip to content

Commit 78c4d83

Browse files
author
ACR1209
committed
<tweak> Enhance SnippetList component to handle loading state and display message when no snippets are found
1 parent 27d8016 commit 78c4d83

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/components/SnippetList.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@ import { useAppContext } from "@contexts/AppContext";
55
import { useSnippets } from "@hooks/useSnippets";
66
import { SnippetType } from "@types";
77

8-
import { LeftAngleArrowIcon } from "./Icons";
98
import SnippetModal from "./SnippetModal";
109

1110
const SnippetList = () => {
1211
const { language, snippet, setSnippet } = useAppContext();
13-
const { fetchedSnippets } = useSnippets();
12+
const { fetchedSnippets, loading } = useSnippets();
1413
const [isModalOpen, setIsModalOpen] = useState(false);
1514

1615
const shouldReduceMotion = useReducedMotion();
1716

18-
if (!fetchedSnippets)
19-
return (
20-
<div>
21-
<LeftAngleArrowIcon />
22-
</div>
23-
);
17+
if (loading) return null;
2418

2519
const handleOpenModal = (activeSnippet: SnippetType) => {
2620
setIsModalOpen(true);
@@ -34,9 +28,25 @@ const SnippetList = () => {
3428

3529
return (
3630
<>
37-
<motion.ul role="list" className="snippets">
31+
<motion.ul
32+
role="list"
33+
className={`snippets ${fetchedSnippets && fetchedSnippets.length === 0 ? "data-empty" : ""}`}
34+
>
3835
<AnimatePresence mode="popLayout">
39-
{fetchedSnippets.map((snippet, idx) => {
36+
{fetchedSnippets && fetchedSnippets.length === 0 && (
37+
<div className="category-no-snippets-found">
38+
<p>No snippets found for this category. Why not add one? 🚀</p>
39+
<a
40+
href="https://github.com/dostonnabotov/quicksnip/blob/main/CONTRIBUTING.md"
41+
target="_blank"
42+
rel="noopener noreferrer"
43+
className="styled-link"
44+
>
45+
Add your own snippet
46+
</a>
47+
</div>
48+
)}
49+
{fetchedSnippets?.map((snippet, idx) => {
4050
const uniqueId = `${language.name}-${snippet.title}`;
4151
return (
4252
<motion.li

src/styles/main.css

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ abbr {
220220
--_flow-space: 2rem;
221221
}
222222

223+
.flow:has(.data-empty) {
224+
height: 100%;
225+
}
226+
223227
/* Text */
224228
.main-title {
225229
font-size: var(--fs-800);
@@ -565,7 +569,7 @@ abbr {
565569
* 1. Responsive grid that adjusts columns automatically
566570
* Each item has a minimum width of 17.5rem and maximum of 100%
567571
*/
568-
.snippets {
572+
.snippets:not(.data-empty) {
569573
display: grid;
570574
gap: 1.5rem;
571575
grid-template-columns: repeat(
@@ -604,6 +608,13 @@ abbr {
604608
color: var(--clr-text-secondary);
605609
}
606610

611+
.category-no-snippets-found {
612+
text-align: center;
613+
font-size: var(--fs-500);
614+
color: var(--clr-text-primary);
615+
padding: 1rem;
616+
height: 100%;
617+
}
607618
/*------------------------------------*\
608619
#MODAL
609620
\*------------------------------------*/

0 commit comments

Comments
 (0)