frontpage/components/ProjectModal.tsx

74 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-01-14 14:27:19 +01:00
import type { NextPage } from "next";
2022-06-13 13:14:00 +02:00
import { useEffect, useRef, useState } from "react";
2022-01-14 14:27:19 +01:00
import styles from "../styles/ProjectModal.module.css";
2022-06-12 14:22:15 +02:00
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";
2022-01-14 14:27:19 +01:00
2022-06-13 13:14:00 +02:00
// 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);
2022-06-12 14:22:15 +02:00
const ProjectModal: NextPage = () => {
const [visible, setVisible] = useState<boolean>(false);
2022-06-13 12:54:40 +02:00
const [currentContent, setCurrentContent] = useState<Project | Diary | undefined>(undefined);
const [currentPage, setCurrentPage] = useState<number>(0);
const [HTMLContent, setHTMLContent] = useState<string>(projectEmpty);
const setModalContent = async (content: Project | Diary, selectedPage?: number) => {
setCurrentContent(content);
if (content.type === "diary") setCurrentPage(selectedPage === undefined ? 0 : selectedPage);
setHTMLContent(await generateContent(content, selectedPage));
2022-06-12 14:22:15 +02:00
};
2022-01-14 14:27:19 +01:00
2022-06-12 14:22:15 +02:00
const { updateCallbacks: updateCmdCallbacks } = useCommands();
const { updateCallbacks: updateModalCallbacks } = useModalFunctions();
2022-06-13 12:54:40 +02:00
updateCmdCallbacks({ setModalVisible: setVisible, setModalContent, setModalHTML: setHTMLContent });
updateModalCallbacks({ setVisible, setContent: setModalContent, setHtml: setHTMLContent });
2022-02-04 23:29:51 +01:00
2022-06-13 13:14:00 +02:00
useEffect(() => {
hljs.highlightAll();
}, [HTMLContent]);
2022-06-12 14:22:15 +02:00
const containerRef = useRef<HTMLDivElement>(null);
2022-01-14 14:27:19 +01:00
2022-01-14 17:17:55 +01:00
if (!visible) return <></>;
2022-01-14 14:27:19 +01:00
2022-06-13 12:54:40 +02:00
const nextPageSelector = (() => {
if (!currentContent || currentContent?.type !== "diary" || currentContent.entries.length === 0) return null;
const prev = <span className={styles.leftSelectSpace}>{currentPage > 0 ? <a href="#" onClick={() => setModalContent(currentContent, currentPage - 1)}>&lt; {currentPage - 1 > 0 ? currentContent.entries[currentPage - 2].title : "Main page"}</a> : null}</span>;
const next = <span className={styles.rightSelectSpace}>{currentPage < currentContent.entries.length ? <a href="#" onClick={() => setModalContent(currentContent, currentPage + 1)}>{currentContent.entries[currentPage].title} &gt;</a> : null}</span>;
const select = (
<select onChange={(e) => setModalContent(currentContent, Number.parseInt(e.target.value))} value={currentPage}>
<option key={-1} value={0}>Main page</option>
{currentContent.entries.map((entry, i) => <option key={i} value={i + 1}>{entry.title}</option>)}
</select>
);
return <div className={styles.pageSelector}>{prev}{currentPage > 0 ? <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span> : null}{select}{currentPage < currentContent.entries.length ? <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span> : null}{next}</div>;
})();
2022-02-05 00:11:59 +01:00
return <div className={styles.modal}>
2022-06-13 14:48:37 +02:00
<a onClick={() => setVisible(false)} href={"#"}>
2022-02-05 00:31:04 +01:00
<div className={styles.modalClose}><div className={styles.modalCloseAlign}>X</div></div>
</a>
2022-02-05 23:11:56 +01:00
<div className={styles.modalContainer}>
2022-06-13 12:54:40 +02:00
{nextPageSelector}
<div className={`${styles.modalText} asciidoc`} ref={containerRef} dangerouslySetInnerHTML={{ __html: HTMLContent ? HTMLContent : projectEmpty }}>
2022-02-05 23:11:56 +01:00
</div>
2022-06-13 12:54:40 +02:00
{nextPageSelector}
2022-01-14 14:27:19 +01:00
</div>
2022-01-14 17:17:55 +01:00
</div>;
};
2022-01-14 14:27:19 +01:00
export default ProjectModal;