-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathSnippetList.tsx
141 lines (127 loc) · 4.38 KB
/
SnippetList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { motion, AnimatePresence, useReducedMotion } from "motion/react";
import { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { useAppContext } from "@contexts/AppContext";
import { useSnippets } from "@hooks/useSnippets";
import { SnippetType } from "@types";
import { QueryParams } from "@utils/enums";
import {
getLanguageDisplayLogo,
getLanguageDisplayName,
} from "@utils/languageUtils";
import { slugify } from "@utils/slugify";
import SnippetModal from "./SnippetModal";
const SnippetList = () => {
const [searchParams, setSearchParams] = useSearchParams();
const { fetchedSnippets, loading } = useSnippets();
const { language, subLanguage, snippet, setSnippet } = useAppContext();
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const shouldReduceMotion = useReducedMotion();
const handleOpenModal = (selected: SnippetType) => () => {
setIsModalOpen(true);
setSnippet(selected);
searchParams.set(QueryParams.SNIPPET, slugify(selected.title));
setSearchParams(searchParams);
};
const handleCloseModal = () => {
setIsModalOpen(false);
setSnippet(null);
searchParams.delete(QueryParams.SNIPPET);
setSearchParams(searchParams);
};
/**
* open the relevant modal if the snippet is in the search params
*/
useEffect(() => {
const snippetSlug = searchParams.get(QueryParams.SNIPPET);
if (!snippetSlug) {
return;
}
const selectedSnippet = (fetchedSnippets ?? []).find(
(item) => slugify(item.title) === snippetSlug
);
if (selectedSnippet) {
handleOpenModal(selectedSnippet)();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetchedSnippets, searchParams]);
if (loading) return null;
return (
<>
<motion.ul
role="list"
className={`snippets ${fetchedSnippets && fetchedSnippets.length === 0 ? "data-empty" : ""}`}
>
<AnimatePresence mode="popLayout">
{fetchedSnippets && fetchedSnippets.length === 0 && (
<div className="category-no-snippets-found">
<p>No snippets found for this category. Why not add one? 🚀</p>
<a
href="https://github.com/technoph1le/quicksnip/blob/main/CONTRIBUTING.md"
target="_blank"
rel="noopener noreferrer"
className="styled-link"
>
Add your own snippet
</a>
</div>
)}
{fetchedSnippets.map((snippet, idx) => {
const uniqueId = `${language.name}-${snippet.title}-${idx}`;
return (
<motion.li
key={uniqueId}
layoutId={uniqueId}
initial={{ opacity: 0, y: 20 }}
animate={{
opacity: 1,
y: 0,
transition: {
duration: shouldReduceMotion ? 0 : 0.2,
},
}}
exit={{
opacity: 0,
y: -20,
transition: {
duration: shouldReduceMotion ? 0 : 0.09,
},
}}
transition={{
ease: [0, 0.75, 0.25, 1],
duration: shouldReduceMotion ? 0 : 0.25,
}}
>
<motion.button
className="snippet | flow"
data-flow-space="sm"
onClick={handleOpenModal(snippet)}
whileHover={{ scale: 1.01 }}
whileTap={{ scale: 0.98 }}
>
<div className="snippet__preview">
<img
src={getLanguageDisplayLogo(language.name, subLanguage)}
alt={getLanguageDisplayName(language.name, subLanguage)}
/>
</div>
<h3 className="snippet__title">{snippet.title}</h3>
</motion.button>
</motion.li>
);
})}
</AnimatePresence>
</motion.ul>
<AnimatePresence mode="wait">
{isModalOpen && snippet && (
<SnippetModal
snippet={snippet}
handleCloseModal={handleCloseModal}
extension={snippet.extension}
/>
)}
</AnimatePresence>
</>
);
};
export default SnippetList;