import type { NextPage } from "next"; import { useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; import styles from "../styles/ProjectModal.module.css"; import type { Project, Diary } from "../lib/content/types"; import { useCommands } from "./contexts/CommandInterface"; import { generateContent, projectEmpty } from "../lib/content/generate"; import { useModalFunctions } from "./contexts/ModalFunctions"; // Code Highlighting import hljs from "highlight.js"; import rust from "highlight.js/lib/languages/rust"; import bash from "highlight.js/lib/languages/shell"; hljs.registerLanguage("rust", rust); hljs.registerLanguage("bash", bash); hljs.registerLanguage("console", bash); hljs.registerLanguage("shell", bash); const ProjectModal: NextPage = () => { const [visible, setVisible] = useState(false); const [currentContent, setCurrentContent] = useState(undefined); const [currentPage, setCurrentPage] = useState(0); const [HTMLContent, setHTMLContent] = useState(projectEmpty); const router = useRouter(); const { updateCallbacks: updateCmdCallbacks, cmdContext } = useCommands(); const { updateCallbacks: updateModalCallbacks } = useModalFunctions(); const setModalContent = async (content: Project | Diary, selectedPage?: number) => { setCurrentContent(content); router.replace("#", `#/${content.type}/${content.name}${content.type === "diary" && selectedPage ? `/${selectedPage}`: ""}`, {shallow: true}); if (content.type === "diary") setCurrentPage(selectedPage === undefined ? 0 : selectedPage); setHTMLContent(await generateContent(content, selectedPage)); }; const setModalVisible = async (visible: boolean) => { if (!visible) router.replace("#", undefined, {shallow: true}); setVisible(visible); }; const onContentReady = () => { const selected = window.location.hash.split("/"); if (selected[2]) cmdContext.executeCommand(`project ${selected[2]}`); }; updateCmdCallbacks({ setModalVisible, setModalContent, setModalHTML: setHTMLContent }); updateModalCallbacks({ setVisible: setModalVisible, setContent: setModalContent, setHtml: setHTMLContent, onContentReady }); useEffect(() => { hljs.highlightAll(); }, [HTMLContent]); const containerRef = useRef(null); if (!visible) return <>; const nextPageSelector = (() => { if (!currentContent || currentContent?.type !== "diary" || currentContent.entries.length === 0) return null; const prev = {currentPage > 0 ? setModalContent(currentContent, currentPage - 1)}>< {currentPage - 1 > 0 ? currentContent.entries[currentPage - 2].title : "Main page"} : null}; const next = {currentPage < currentContent.entries.length ? setModalContent(currentContent, currentPage + 1)}>{currentContent.entries[currentPage].title} > : null}; const select = ( ); return
{prev} 0 ? "visible" : "hidden"}}>  |  {select}  |  {next}
; })(); return
setModalVisible(false)} className={"fake-link"}>
X
{nextPageSelector}
{nextPageSelector}
; }; export default ProjectModal;