From 8995c63f4c246ee75cf1c9c7f387a955c4ed4a6c Mon Sep 17 00:00:00 2001 From: Elias Almqvist Date: Thu, 27 Feb 2025 23:36:36 -0800 Subject: [PATCH] stuff --- src/app/essays/page.tsx | 60 ----------------------------------- src/app/essays/route.ts | 30 ++++++++++++++++++ src/app/page.tsx | 23 +++++--------- src/components/layout/nav.tsx | 52 +++++++++++++++++------------- 4 files changed, 68 insertions(+), 97 deletions(-) delete mode 100644 src/app/essays/page.tsx create mode 100644 src/app/essays/route.ts diff --git a/src/app/essays/page.tsx b/src/app/essays/page.tsx deleted file mode 100644 index c257bad..0000000 --- a/src/app/essays/page.tsx +++ /dev/null @@ -1,60 +0,0 @@ -import fs from 'fs/promises'; -import path from 'path'; -import matter from 'gray-matter'; -import Link from 'next/link'; - -type PostMeta = { - title: string; - createdAt: string; - slug: string; -}; - -async function getPosts(): Promise { - const postsDirectory = path.join(process.cwd(), 'content/essays'); - const files = await fs.readdir(postsDirectory); - - const posts = await Promise.all( - files - .filter(file => file.endsWith('.mdx')) - .map(async (file) => { - const fullPath = path.join(postsDirectory, file); - const fileContents = await fs.readFile(fullPath, 'utf8'); - const { data } = matter(fileContents); - - return { - title: data.title, - createdAt: data.createdAt, - slug: file.replace(/\.mdx$/, ''), - }; - }) - ); - - return posts.sort((a, b) => - new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() - ); -} - -export default async function PostsPage() { - const posts = await getPosts(); - - return ( -
-

Essays

-
    - {posts.map((post) => ( -
  • - -

    {post.title}

    - - -
  • - ))} -
-
- ); -} \ No newline at end of file diff --git a/src/app/essays/route.ts b/src/app/essays/route.ts new file mode 100644 index 0000000..7bb5be8 --- /dev/null +++ b/src/app/essays/route.ts @@ -0,0 +1,30 @@ +import { NextResponse } from 'next/server'; +import fs from 'fs/promises'; +import path from 'path'; +import matter from 'gray-matter'; + +export async function GET() { + const postsDirectory = path.join(process.cwd(), 'content/essays'); + const files = await fs.readdir(postsDirectory); + + const essays = await Promise.all( + files + .filter(file => file.endsWith('.mdx')) + .map(async (file) => { + const fullPath = path.join(postsDirectory, file); + const fileContents = await fs.readFile(fullPath, 'utf8'); + const { data } = matter(fileContents); + + return { + title: data.title, + createdAt: data.createdAt, + slug: file.replace(/\.mdx$/, ''), + }; + }) + ); + + // Sort essays by creation date, newest first + essays.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); + + return NextResponse.json(essays); +} \ No newline at end of file diff --git a/src/app/page.tsx b/src/app/page.tsx index c5e1da8..30504c9 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -12,7 +12,7 @@ type PostMeta = { slug: string; }; -async function getRecentPosts(): Promise { +async function getAllPosts(): Promise { const postsDirectory = path.join(process.cwd(), 'content/essays'); const files = await fs.readdir(postsDirectory); @@ -32,17 +32,15 @@ async function getRecentPosts(): Promise { }) ); - return posts - .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()) - .slice(0, 4); // Get only the 4 most recent posts + return posts.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); } export default async function Home() { - const recentPosts = await getRecentPosts(); + const posts = await getAllPosts(); return (
-
+
{/* Intro */}
@@ -59,7 +57,7 @@ export default async function Home() { . Building energy-efficient chips for AI training & inference.

- I'm a dropout, autodidactic polymath, and this is my digital notebook. Everything here is written by me, and everything here is my own opinion or philosophical beliefs. They are not intended to be taken at face value, but rather as a medium for me to personally reflect on my own thoughts as a therapeutic exercise, or just for fun. + I'm a dropout, autodidactic polymath, and this is my digital notebook. Everything here is written by me, and everything here is my own opinion, philosophical beliefs, or just random thoughts that have no real-world application. They are not intended to be taken at face value, but rather as a medium for me to personally reflect on my own thoughts as a therapeutic exercise, or just for fun.

Based in the Silicon Valley (San Francisco Bay Area), United States. Originally from Mölndal/Gothenburg, Sweden. @@ -67,16 +65,11 @@ export default async function Home() {

- {/* Recent Essays */} + {/* Essays */}
-
-

Recent Essays

- - View all → - -
+

Essays

    - {recentPosts.map((post) => ( + {posts.map((post) => (
  • = ({ className, ...props }) => { const router = useRouter(); const { open: actionOpen, setOpen: setActionOpen } = useActionCommand(); const { theme, setTheme } = useTheme(); + const [essays, setEssays] = useState([]); + + useEffect(() => { + const fetchEssays = async () => { + const response = await fetch('/essays'); + const data = await response.json(); + setEssays(data); + }; + fetchEssays(); + }, []); const action = ( callback: ((value: string) => Promise) | ((value: string) => void), @@ -67,30 +84,21 @@ const NavCommand: React.FC = ({ className, ...props }) => { Home ⌘H - { - router.push("/essays"); - })} - > - - Essays - ⌘P - - {/* - { - setTheme(theme === "light" ? "dark" : "light"); - })} - > - - - Switch to {theme === "light" ? "Dark" : "Light"} Theme - - ⌘L - - */} + + {essays.map((essay) => ( + { + router.push(`/essays/${essay.slug}`); + })} + > + + {essay.title} + + ))} + {NavLinks.map((link) => (