// This file is used for the generation of the static HTML files in the frontend. // It is used by the terminal. import type { Project, Diary } from "./types"; import type { APIReturn } from "./generateBackend"; export const projectEmpty = "
Kein Projekt ausgewählt.
"; 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!
`; export async function generateContent(content: Project|Diary, selectedPage?: number): Promise { if(!content) return projectEmpty; switch (content.type) { case "project": return await generateProjectHTML(content); case "diary": return await generateDiaryHTML(content, selectedPage); default: return projectNotFoundHtml; } } async function generateProjectHTML(project: Project): Promise { const resp = await fetch(`/api/contentRendering?name=${project.name}`); if (resp.status !== 200) return projectServerErrorHtml; const response = await resp.json() as APIReturn; if (!response || !response.type) return projectServerErrorHtml; if (response.type === "error") return response.html; else { return `${response.html}
`; } } async function generateDiaryHTML(diary: Diary, selectedPage?: number): Promise { const url = `/api/contentRendering?name=${diary.name}${selectedPage ? `&page=${selectedPage}` : ""}`; const resp = await fetch(url); const response = await resp.json() as APIReturn; if (!response || !response.type) return projectServerErrorHtml; if (response.type === "error") return response.html; return `${response.html}
`; }