frontpage/lib/content/generateBrowser.ts

54 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-10-07 23:03:39 +02:00
// This file is used for the generation of the static HTML files in the frontend.
// It is used by the terminal.
2022-06-12 14:22:15 +02:00
import type { Project, Diary } from "./types";
2022-11-03 14:49:32 +01:00
import type { APIReturn } from "./generateBackend";
2022-06-12 14:22:15 +02:00
export const projectEmpty = "<div>Kein Projekt ausgewählt.</div>";
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>`;
export async function generateContent(content: Project|Diary, selectedPage?: number): Promise<string> {
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<string> {
2022-11-03 14:49:32 +01:00
const resp = await fetch(`/api/contentRendering?name=${project.name}`);
2022-06-12 14:22:15 +02:00
if (resp.status !== 200) return projectServerErrorHtml;
2022-11-03 14:49:32 +01:00
const response = await resp.json() as APIReturn;
if (!response || !response.type) return projectServerErrorHtml;
if (response.type === "error") return response.html;
else {
return `${response.html}
2022-06-12 14:22:15 +02:00
<hr>
<div id="footer">
<div id="footer-text">
2022-11-03 14:49:32 +01:00
Last updated: ${new Date(response.date).toLocaleString()} | <a href="${response.repoUrl}" target="_blank">Document source</a>
2022-06-12 14:22:15 +02:00
</div>
</div>`;
2022-11-03 14:49:32 +01:00
}
2022-06-12 14:22:15 +02:00
}
async function generateDiaryHTML(diary: Diary, selectedPage?: number): Promise<string> {
2022-11-03 14:49:32 +01:00
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}
2022-06-12 14:22:15 +02:00
<hr>
<div id="footer">
<div id="footer-text">
2022-11-03 14:49:32 +01:00
Last updated: ${new Date(response.date).toLocaleString()} | <a href="${response.repoUrl}" target="_blank">Document source</a>
2022-06-12 14:22:15 +02:00
</div>
</div>`;
}