|
| 1 | +import { useState } from "react"; |
| 2 | +import { SnippetType } from "../types"; |
1 | 3 | import { useAppContext } from "../contexts/AppContext"; |
2 | 4 | import { useSnippets } from "../hooks/useSnippets"; |
| 5 | +import slugify from "../utils/slugify"; |
| 6 | + |
3 | 7 | import Button from "./Button"; |
4 | | -import { CopyIcon, ExpandIcon } from "./Icons"; |
| 8 | +import CodePreview from "./CodePreview"; |
| 9 | +import SnippetModal from "./SnippetModal"; |
| 10 | +import CopyToClipboard from "./CopyToClipboard"; |
| 11 | +import { CloseIcon, ExpandIcon } from "./Icons"; |
5 | 12 |
|
6 | 13 | const SnippetList = () => { |
7 | | - const { language } = useAppContext(); |
| 14 | + const { language, setSnippet } = useAppContext(); |
8 | 15 | const { fetchedSnippets } = useSnippets(); |
| 16 | + const [isModalOpen, setIsModalOpen] = useState(false); |
9 | 17 |
|
10 | 18 | if (!fetchedSnippets) return <p>Empty List</p>; |
11 | 19 |
|
| 20 | + const handleOpenModal = (activeSnippet: SnippetType) => { |
| 21 | + setIsModalOpen(true); |
| 22 | + setSnippet(activeSnippet); |
| 23 | + }; |
| 24 | + |
| 25 | + const handleCloseModal = () => { |
| 26 | + setIsModalOpen(false); |
| 27 | + setSnippet(null); |
| 28 | + }; |
| 29 | + |
12 | 30 | return ( |
13 | 31 | <ul role="list" className="snippets"> |
14 | | - {fetchedSnippets.map((snippet) => ( |
15 | | - <li className="snippet"> |
| 32 | + {fetchedSnippets.map((snippet, idx) => ( |
| 33 | + <li key={idx} className="snippet"> |
16 | 34 | <div className="snippet__preview"> |
17 | 35 | <img src={language.icon} alt={language.lang} /> |
18 | | - <Button isIcon={true} className="snippet__copy"> |
| 36 | + {/* <Button isIcon={true} className="snippet__copy"> |
19 | 37 | <CopyIcon /> |
20 | | - </Button> |
| 38 | + </Button> */} |
21 | 39 | </div> |
22 | 40 |
|
23 | 41 | <div className="snippet__content"> |
24 | 42 | <h3 className="snippet__title">{snippet.title}</h3> |
25 | | - <Button isIcon={true}> |
| 43 | + <Button isIcon={true} onClick={() => handleOpenModal(snippet)}> |
26 | 44 | <ExpandIcon /> |
27 | 45 | </Button> |
28 | 46 | </div> |
| 47 | + {isModalOpen && ( |
| 48 | + <SnippetModal> |
| 49 | + <div className="modal | flow" data-flow-space="lg"> |
| 50 | + <div className="modal__header"> |
| 51 | + <h2 className="section-title">{snippet.title}</h2> |
| 52 | + <Button isIcon={true} onClick={handleCloseModal}> |
| 53 | + <CloseIcon /> |
| 54 | + </Button> |
| 55 | + </div> |
| 56 | + <div className="code-preview"> |
| 57 | + <CopyToClipboard className="modal__copy" /> |
| 58 | + <CodePreview language={slugify(language.lang)}> |
| 59 | + {snippet.code} |
| 60 | + </CodePreview> |
| 61 | + </div> |
| 62 | + <p> |
| 63 | + <b>Description: </b> |
| 64 | + {snippet.description} |
| 65 | + </p> |
| 66 | + <p> |
| 67 | + Contributed by <b>{snippet.author}</b> |
| 68 | + </p> |
| 69 | + <ul role="list" className="modal__tags"> |
| 70 | + {snippet.tags.map((tag) => ( |
| 71 | + <li key={tag} className="modal__tag"> |
| 72 | + {tag} |
| 73 | + </li> |
| 74 | + ))} |
| 75 | + </ul> |
| 76 | + </div> |
| 77 | + </SnippetModal> |
| 78 | + )} |
29 | 79 | </li> |
30 | 80 | ))} |
31 | 81 | </ul> |
|
0 commit comments