2022-01-14 14:27:19 +01:00
|
|
|
import type { NextPage } from "next";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
import asciidoctor from "asciidoctor";
|
|
|
|
import styles from "../styles/ProjectModal.module.css";
|
|
|
|
|
|
|
|
interface ModalInput {
|
|
|
|
project: string;
|
|
|
|
visible: boolean;
|
|
|
|
setVisible: CallableFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ad = asciidoctor();
|
|
|
|
|
|
|
|
const ProjectModal: NextPage<ModalInput> = ({ project, visible, setVisible }) => {
|
|
|
|
const projectEmpty = "<div>Kein Projekt ausgewählt.</div>";
|
|
|
|
const [projectData, setProjectData] = useState<string>(projectEmpty);
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const projectNotFoundHtml = `<div class="${"error"}">Sorry! There is no data for this project. Please check back later to see if that changed!</div>`;
|
|
|
|
const projectServerErrorHtml = `<div class="${"error"}">Sorry! A server error happend when the project data was fetched!</div>`;
|
|
|
|
|
2022-02-04 23:29:51 +01:00
|
|
|
const generateFooter = (project: string, lastUpdate: string) => `<hr>
|
|
|
|
<div id="footer">
|
|
|
|
<div id="footer-text">
|
|
|
|
Last updated: ${lastUpdate} | <a href="https://git.c0ntroller.de/c0ntroller/frontpage-projects/src/branch/senpai/${project}.adoc">Document source</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
2022-01-14 14:27:19 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (project && project !== "") {
|
|
|
|
// TODO
|
|
|
|
// set Spinner
|
2022-02-04 23:29:51 +01:00
|
|
|
setProjectData("Loading...");
|
2022-01-16 15:16:24 +01:00
|
|
|
fetch(`/api/projects/${project}`).then((res) => {
|
2022-01-14 14:27:19 +01:00
|
|
|
if (res.status === 404) setProjectData(projectNotFoundHtml);
|
|
|
|
if (res.status !== 200) setProjectData(projectServerErrorHtml);
|
|
|
|
res.text().then(data => {
|
|
|
|
try {
|
2022-02-04 23:29:51 +01:00
|
|
|
const adDoc = ad.load(data, {attributes: {showtitle: true}});
|
|
|
|
setProjectData(adDoc.convert(adDoc).toString() + generateFooter(project, adDoc.getAttribute("docdatetime")));
|
2022-01-14 14:27:19 +01:00
|
|
|
} catch {
|
|
|
|
setProjectData(projectServerErrorHtml);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-01-14 17:17:55 +01:00
|
|
|
} else if (project === "") setProjectData(projectEmpty);
|
|
|
|
}, [project, projectEmpty, projectNotFoundHtml, projectServerErrorHtml]);
|
2022-01-14 14:27:19 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (projectData && containerRef.current && projectData !== "") {
|
|
|
|
containerRef.current.innerHTML = projectData;
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:55 +01:00
|
|
|
}, [projectData, visible]);
|
2022-01-14 14:27:19 +01:00
|
|
|
|
|
|
|
const onEscClose = (e: React.KeyboardEvent) => {
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
e.preventDefault();
|
|
|
|
setVisible(false);
|
|
|
|
}
|
2022-01-14 17:17:55 +01:00
|
|
|
};
|
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
|
|
|
|
|
|
|
return <div className={styles.modal} onKeyDown={onEscClose}>
|
2022-02-04 23:29:51 +01:00
|
|
|
<div ref={containerRef} className={`${styles.modalContainer} asciidoc`}>
|
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;
|