From 9c2d40ea9627ca4e8e891c31f570a845a69706a7 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 18 Apr 2025 16:59:45 +0200 Subject: [PATCH] fix --- news/0.md | 22 ----------- src/app/news/[id]/page.tsx | 76 +++++++++++++++++++++++++++++--------- src/lib/news.ts | 44 ++-------------------- 3 files changed, 62 insertions(+), 80 deletions(-) delete mode 100644 news/0.md diff --git a/news/0.md b/news/0.md deleted file mode 100644 index 3b683ca..0000000 --- a/news/0.md +++ /dev/null @@ -1,22 +0,0 @@ ---- -authors: Kleo -title: Refactoring -description: The current codebase isn't practical, so we are refractoring it -image: https://preview.redd.it/axscigvvfik71.png?auto=webp&s=ddd458f865c6f50d21a66fe76dc12a07c8b1e198 -date: 2025-04-18 -tags: development, news ---- -Due to Minecraft's nature of refactors for the past decade (which is normal for all apps), we have decided to refactor the codebase to make it more maintainable and practical. This will also take us in other versions of Minecraft. - -We will be working on this in the next few weeks trying to improve both the codebase and the user experience. - -## Goals -- 📖 Document every change done to the code, both in discord and in the github wiki, this is mandatory. -- 🧩 Separate as much code from Minecraft as possible (abstractions), important for adding other versions. -- 🎨 Make the UI as customizable as possible, good for user experience, we should keep this in mind. - -For updates, check out our [Discord](https://discord.gg/HFRsNxfCqT) or [GitHub issue](https://github.com/saturnclientmc/saturnclient/issues/1) - ---- - -Credits to [Electronic_Sample_96](https://www.reddit.com/r/Minecraft/comments/pejvos/an_ultrawide_wallpaper_on_minecraft_2560_x_1080/) for the image diff --git a/src/app/news/[id]/page.tsx b/src/app/news/[id]/page.tsx index 0f28c13..ae69617 100644 --- a/src/app/news/[id]/page.tsx +++ b/src/app/news/[id]/page.tsx @@ -1,41 +1,83 @@ -import { readFileSync } from "fs"; +"use client"; + +import { useEffect, useState } from "react"; import smartSplit from "@/lib/smartSplit"; -import { parseNews } from "@/lib/news"; +import { parseNews } from "@/lib/newsParser"; import Header from "@/components/Header"; import Footer from "@/components/Footer"; import Md from "./Markdown"; -export default async function News({ - params, -}: { - params: Promise<{ id: string }>; -}) { - const contents = readFileSync(`news/${(await params).id}.md`, "utf-8"); +export default function News({ params }: { params: Promise<{ id: string }> }) { + const [newsData, setNewsData] = useState<{ + authors: string[]; + title: string; + description: string; + image: string; + date: string; + content: string; + } | null>(null); + const [isLoading, setIsLoading] = useState(true); - const { authors, title, description, image, date } = parseNews( - contents, - (await params).id - ); + useEffect(() => { + const fetchNews = async () => { + try { + const resolvedParams = await params; + const response = await fetch(`/news-md/${resolvedParams.id}.md`); + const contents = await response.text(); + console.log(contents); + const { authors, title, description, image, date } = parseNews( + contents, + resolvedParams.id + ); + const content = smartSplit(contents, "---", 3)[2]; + setNewsData({ authors, title, description, image, date, content }); + } catch (error) { + console.error("Error fetching news:", error); + } finally { + setIsLoading(false); + } + }; + + fetchNews(); + }, [params]); + + if (isLoading) { + return ( +
+

Loading...

+
+ ); + } + + if (!newsData) { + return ( +
+

Failed to load news article

+
+ ); + } return (

- {title} + {newsData.title}

-

{description}

-

written by {authors.join(", ")}

+

{newsData.description}

+

+ written by {newsData.authors.join(", ")} +

- {smartSplit(contents, "---", 3)[2]} + {newsData.content}
diff --git a/src/lib/news.ts b/src/lib/news.ts index 3121270..9343025 100644 --- a/src/lib/news.ts +++ b/src/lib/news.ts @@ -1,52 +1,14 @@ import { readdirSync, readFileSync } from "fs"; -import smartSplit from "./smartSplit"; - -export interface News { - authors: string[]; - title: string; - description: string; - image: string; - date: string; - id: string; -} - -export function parseNews(news: string, id: string): News { - const content = news.split("---", 2)[1]; - - const params: News = { - authors: [], - title: "Undefined title", - description: "", - image: "/background.png", - date: "Undefined date", - id, - }; - - content.split("\n").forEach((v) => { - const [key, value] = smartSplit(v, ":", 2).map((v) => v.trim()); - if (key === "authors") { - params.authors = value.split(",").map((v) => v.trim()); - } else if ( - key === "title" || - key === "description" || - key === "image" || - key === "date" - ) { - params[key] = value; - } - }); - - return params; -} +import { News, parseNews } from "./newsParser"; export default function listNews(): News[] { - const newsIdList = readdirSync("news"); + const newsIdList = readdirSync("public/news-md"); if (newsIdList === null) return []; return newsIdList.map((v) => { return parseNews( - readFileSync(`news/${v}`, "utf-8"), + readFileSync(`public/news-md/${v}`, "utf-8"), v.replaceAll(".md", "") ); });