From 86490a3a44351e33a63df6c03041cd5d9a9c3314 Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Tue, 15 Mar 2022 17:21:10 +0100 Subject: [PATCH] Support diaries (kinda) --- components/ProjectModal.tsx | 18 ++++++++++-------- components/REPL/REPLInput.tsx | 7 ++++--- components/REPL/index.tsx | 1 + lib/commands/definitions.ts | 10 +++++++--- lib/commands/index.ts | 11 ++++++----- lib/projects/types.ts | 19 ++++++++++++++++++- pages/index.tsx | 8 +++++--- 7 files changed, 51 insertions(+), 23 deletions(-) diff --git a/components/ProjectModal.tsx b/components/ProjectModal.tsx index 7f3902b..faa6bc7 100644 --- a/components/ProjectModal.tsx +++ b/components/ProjectModal.tsx @@ -2,17 +2,19 @@ import type { NextPage } from "next"; import { useEffect, useRef, useState } from "react"; import asciidoctor from "asciidoctor"; import styles from "../styles/ProjectModal.module.css"; -import Link from "next/link"; +import type { Project, Diary } from "../lib/projects/types"; +//import Link from "next/link"; interface ModalInput { - project: string; + project: Project|Diary|undefined; + projectType: "project"|"diary"; visible: boolean; setVisible: CallableFunction; } const ad = asciidoctor(); -const ProjectModal: NextPage = ({ project, visible, setVisible }) => { +const ProjectModal: NextPage = ({ project, projectType, visible, setVisible }) => { const projectEmpty = "
Kein Projekt ausgewählt.
"; const [projectData, setProjectData] = useState(projectEmpty); const containerRef = useRef(null); @@ -29,24 +31,24 @@ Last updated: ${lastUpdate} | { + 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, adDoc.getAttribute("docdatetime"))); + setProjectData(adDoc.convert(adDoc).toString() + generateFooter(project.name, adDoc.getAttribute("docdatetime"))); } catch { setProjectData(projectServerErrorHtml); } }); }); - } else if (project === "") setProjectData(projectEmpty); - }, [project, projectEmpty, projectNotFoundHtml, projectServerErrorHtml]); + } else if (typeof project === "undefined") setProjectData(projectEmpty); + }, [project, projectType, projectEmpty, projectNotFoundHtml, projectServerErrorHtml]); useEffect(() => { if (projectData && containerRef.current && projectData !== "") { diff --git a/components/REPL/REPLInput.tsx b/components/REPL/REPLInput.tsx index 3aab380..50cf107 100644 --- a/components/REPL/REPLInput.tsx +++ b/components/REPL/REPLInput.tsx @@ -3,7 +3,7 @@ import useSWR from "swr"; import { MutableRefObject, useState, createRef, useEffect } from "react"; import { CommandInterface } from "../../lib/commands"; import styles from "../../styles/REPL/REPLInput.module.css"; -import { Project } from "../../lib/projects/types"; +import type { ProjectList } from "../../lib/projects/types"; interface REPLInputParams { historyCallback: CallableFunction; @@ -12,10 +12,11 @@ interface REPLInputParams { modalManipulation: { setModalVisible: CallableFunction; setModalProject: CallableFunction; + setModalProjectType: CallableFunction; } } -async function fetchProjects(endpoint: string): Promise { +async function fetchProjects(endpoint: string): Promise { const res = await fetch(endpoint); return res.json(); } @@ -28,7 +29,7 @@ const REPLInput: NextPage = ({historyCallback, historyClear, in const [inCmdHistory, setInCmdHistory] = useState(-1); const [cmdHistory, setCmdHistory] = useState([]); const [usrInputTmp, setUsrInputTmp] = useState(""); - const [cmdIf, setCmdIf] = useState(new CommandInterface(modalManipulation, [])); + const [cmdIf, setCmdIf] = useState(new CommandInterface(modalManipulation, {projects: [], diaries: []})); const { data: projects, error: projectsError } = useSWR("/api/projects?swr=1", fetchProjects); const setInput = (inputRef: HTMLInputElement, input: string) => { diff --git a/components/REPL/index.tsx b/components/REPL/index.tsx index efb2af9..b685480 100644 --- a/components/REPL/index.tsx +++ b/components/REPL/index.tsx @@ -9,6 +9,7 @@ interface IREPLProps { modalManipulation: { setModalVisible: CallableFunction; setModalProject: CallableFunction; + setModalProjectType: CallableFunction; } } diff --git a/lib/commands/definitions.ts b/lib/commands/definitions.ts index ee4e3b2..4f037ad 100644 --- a/lib/commands/definitions.ts +++ b/lib/commands/definitions.ts @@ -146,7 +146,9 @@ const project: Command = { execute: (flags, args, _raw, cmdIf) => { if (project.flags && checkFlagInclude(flags, project.flags.list)) { const result = ["Found the following projects:"]; - cmdIf.projects.forEach(project => result.push(`\t${project.name}\t${project.short}`)); + cmdIf.projects.projects.forEach(project => result.push(`\t${project.name}\t${project.short_desc}`)); + result.push("And the following diaries:"); + cmdIf.projects.diaries.forEach(diary => result.push(`\t${diary.name}\t${diary.short_desc}`)); return result; } @@ -154,7 +156,8 @@ const project: Command = { if (args[0] === "this") args[0] = "homepage"; - const pjt = cmdIf.projects.find(p => p.name === args[0]); + let [pjt, ptype] = [cmdIf.projects.projects.find(p => p.name === args[0]), "project"]; + if (!pjt) [pjt, ptype] = [cmdIf.projects.diaries.find(p => p.name === args[0]), "diary"]; if (!pjt) return [ `Cannot find project ${args[0]}!`, "You can see available projects using 'project -l'." @@ -170,7 +173,8 @@ const project: Command = { } if (project.flags && checkFlagInclude(flags, project.flags.minimal)) return pjt.desc; - cmdIf.callbacks.setModalProject(args[0]); + cmdIf.callbacks.setModalProjectType(ptype); + cmdIf.callbacks.setModalProject(pjt); cmdIf.callbacks.setModalVisible(true); return []; } diff --git a/lib/commands/index.ts b/lib/commands/index.ts index 1c6b3a2..5c6f312 100644 --- a/lib/commands/index.ts +++ b/lib/commands/index.ts @@ -1,16 +1,17 @@ -import { Project } from "../projects/types"; +import type { ProjectList } from "../projects/types"; import { printSyntax, commandList } from "./definitions"; interface CommandInterfaceCallbacks { - setModalVisible: CallableFunction, - setModalProject: CallableFunction + setModalVisible: CallableFunction; + setModalProject: CallableFunction; + setModalProjectType: CallableFunction; } export class CommandInterface { callbacks: CommandInterfaceCallbacks; - projects: Project[]; + projects: ProjectList; - constructor(callbacks: CommandInterfaceCallbacks, projects: Project[]) { + constructor(callbacks: CommandInterfaceCallbacks, projects: ProjectList) { this.callbacks = callbacks; this.projects = projects; } diff --git a/lib/projects/types.ts b/lib/projects/types.ts index feca901..47acfa8 100644 --- a/lib/projects/types.ts +++ b/lib/projects/types.ts @@ -1,7 +1,24 @@ +export interface ProjectList { + projects: Project[]; + diaries: Diary[]; +} + export interface Project { name: string; desc: string[]; - short: string; + short_desc: string; more?: string; repo?: string; +} + +export interface Diary { + name: string; + desc: string[]; + short_desc: string; + more?: string; + repo?: string; + entries: { + title: string; + path: string; + }[]; } \ No newline at end of file diff --git a/pages/index.tsx b/pages/index.tsx index 62c8527..17949b8 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -4,12 +4,14 @@ import { GithubLogo, InstagramLogo, DiscordLogo, GameController } from "phosphor import { useRef, useState } from "react"; import ProjectModal from "../components/ProjectModal"; import REPL from "../components/REPL"; +import type { Project, Diary } from "../lib/projects/types"; import styles from "../styles/Home.module.css"; const Home: NextPage = () => { const inputRef = useRef(null); const [modalVisible, setModalVisible] = useState(false); - const [modalProject, setModalProject] = useState(""); + const [modalProject, setModalProject] = useState(undefined); + const [modalProjectType, setModalProjectType] = useState<"project"|"diary">("project"); const focusInput = () => { if (inputRef.current) inputRef.current.focus(); }; @@ -24,7 +26,7 @@ const Home: NextPage = () => { c0ntroller.de - + ); };