frontpage/components/ProjectModal.tsx

54 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-01-14 14:27:19 +01:00
import type { NextPage } from "next";
2022-06-12 14:22:15 +02:00
import { useContext, useEffect, useRef, useState } from "react";
2022-01-14 14:27:19 +01:00
import asciidoctor from "asciidoctor";
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-03-15 17:21:10 +01:00
//import Link from "next/link";
2022-01-14 14:27:19 +01:00
2022-06-12 14:22:15 +02:00
const ProjectModal: NextPage = () => {
const [visible, setVisible] = useState<boolean>(false);
const [diaryPages, setDiaryPages] = useState<string[]>([]);
const [content, setContent] = useState<string>(projectEmpty);
2022-01-14 14:27:19 +01:00
2022-06-12 14:22:15 +02:00
const setModalContent = async (content: Project|Diary, selectedPage?: number) => {
if (content.type === "diary") setDiaryPages(content.entries.map(entry => entry.title));
setContent(await generateContent(content, selectedPage));
};
2022-01-14 14:27:19 +01:00
2022-06-12 14:22:15 +02:00
const { updateCallbacks: updateCmdCallbacks } = useCommands();
const { updateCallbacks: updateModalCallbacks } = useModalFunctions();
updateCmdCallbacks({ setModalVisible: setVisible, setModalContent});
updateModalCallbacks({ setVisible, setContent: setModalContent, setHtml: setContent });
2022-02-04 23:29:51 +01:00
2022-06-12 14:22:15 +02:00
const containerRef = useRef<HTMLDivElement>(null);
2022-01-14 14:27:19 +01:00
useEffect(() => {
2022-06-12 14:22:15 +02:00
if (content && containerRef.current) {
containerRef.current.innerHTML = content;
2022-01-14 14:27:19 +01:00
}
2022-06-12 14:22:15 +02:00
}, [content]);
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-02-05 00:11:59 +01:00
return <div className={styles.modal}>
2022-06-12 14:22:15 +02:00
<a onClick={() => setVisible(false)}>
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-12 14:22:15 +02:00
{
// TODO
// If diaryPages
// Show page selector
}
2022-02-05 23:11:56 +01:00
<div className={`${styles.modalText} asciidoc`} ref={containerRef}>
</div>
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;