import type { NextPage } from "next"; import { useEffect, useRef, useState } from "react"; import asciidoctor from "asciidoctor"; import styles from "../styles/ProjectModal.module.css"; import type { Project, Diary } from "../lib/projects/types"; //import Link from "next/link"; interface ModalInput { project: Project|Diary|undefined; projectType: "project"|"diary"; visible: boolean; setVisible: CallableFunction; } const ad = asciidoctor(); const ProjectModal: NextPage = ({ project, projectType, visible, setVisible }) => { const projectEmpty = "
Kein Projekt ausgewählt.
"; const [projectData, setProjectData] = useState(projectEmpty); const containerRef = useRef(null); const projectNotFoundHtml = `
Sorry! There is no data for this project. Please check back later to see if that changed!
`; const projectServerErrorHtml = `
Sorry! A server error happend when the project data was fetched!
`; const generateFooter = (project: string, lastUpdate: string) => `
`; useEffect(() => { if (project && project.name) { // TODO // set Spinner setProjectData("Loading..."); fetch(`/api/${projectType === "diary" ? "diaries" : "projects"}/${project.name}`).then((res) => { if (res.status === 404) setProjectData(projectNotFoundHtml); if (res.status !== 200) setProjectData(projectServerErrorHtml); res.text().then(data => { try { const adDoc = ad.load(data, { attributes: { showtitle: true } }); setProjectData(adDoc.convert(adDoc).toString() + generateFooter(project.name, adDoc.getAttribute("docdatetime"))); } catch { setProjectData(projectServerErrorHtml); } }); }); } else if (typeof project === "undefined") setProjectData(projectEmpty); }, [project, projectType, projectEmpty, projectNotFoundHtml, projectServerErrorHtml]); useEffect(() => { if (projectData && containerRef.current && projectData !== "") { containerRef.current.innerHTML = projectData; } }, [projectData, visible]); if (!visible) return <>; return
setVisible(false)}>
X
; }; export default ProjectModal;