diff --git a/components/ProjectModal.tsx b/components/ProjectModal.tsx index 4d1061b..4e87226 100644 --- a/components/ProjectModal.tsx +++ b/components/ProjectModal.tsx @@ -1,5 +1,6 @@ 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"; @@ -20,17 +21,30 @@ const ProjectModal: NextPage = () => { 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 { updateCallbacks: updateCmdCallbacks } = useCommands(); - const { updateCallbacks: updateModalCallbacks } = useModalFunctions(); - updateCmdCallbacks({ setModalVisible: setVisible, setModalContent, setModalHTML: setHTMLContent }); - updateModalCallbacks({ setVisible, setContent: setModalContent, setHtml: setHTMLContent }); + 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(); @@ -43,8 +57,8 @@ const ProjectModal: NextPage = () => { 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 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 = (