From 85471bc712be12036a5af8847466230e4f4a5b2d Mon Sep 17 00:00:00 2001 From: lew Date: Thu, 29 Jan 2026 01:38:33 +0000 Subject: [PATCH] feat: add auth-protected /draft/ index page --- apps/www/src/pages/draft/index.astro | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 apps/www/src/pages/draft/index.astro diff --git a/apps/www/src/pages/draft/index.astro b/apps/www/src/pages/draft/index.astro new file mode 100644 index 0000000..636c8df --- /dev/null +++ b/apps/www/src/pages/draft/index.astro @@ -0,0 +1,71 @@ +--- +export const prerender = false; + +import { getSession } from 'auth-astro/server'; +import { getCollection } from 'astro:content'; +import { isAdmin } from '../../lib/auth'; +import Layout from '../../layouts/Layout.astro'; + +let session; +try { + session = await getSession(Astro.request); +} catch { + return new Response('Auth not configured', { status: 500 }); +} + +if (!session) { + return Astro.redirect('/api/auth/signin'); +} + +if (!isAdmin(session.user?.id)) { + return new Response('Forbidden', { status: 403 }); +} + +const posts = await getCollection('posts', ({ data }) => data.draft === true); + +// Group by category (default: "posts") +const grouped = posts.reduce((acc, post) => { + const category = post.data.category ?? 'posts'; + if (!acc[category]) acc[category] = []; + acc[category].push(post); + return acc; +}, {} as Record); + +// Sort categories: "posts" first, then alphabetically +const sortedCategories = Object.keys(grouped).sort((a, b) => { + if (a === 'posts') return -1; + if (b === 'posts') return 1; + return a.localeCompare(b); +}); + +// Sort posts within each category: pinned first, then by date descending +for (const category of sortedCategories) { + grouped[category].sort((a, b) => { + if (a.data.pinned && !b.data.pinned) return -1; + if (!a.data.pinned && b.data.pinned) return 1; + return b.data.date.getTime() - a.data.date.getTime(); + }); +} + +function formatDate(date: Date): string { + const d = String(date.getDate()).padStart(2, '0'); + const m = String(date.getMonth() + 1).padStart(2, '0'); + const y = String(date.getFullYear()).slice(-2); + return `${d}/${m}/${y}`; +} +--- + + +

logged in as {session.user?.name} sign out

+ +{sortedCategories.length === 0 ? ( +

no drafts

+) : ( + sortedCategories.map(category => ( +
+ {category} +
 `${formatDate(post.data.date)}    ${post.data.title}${post.data.pinned ? ' [pinned]' : ''}`).join('\n')} />
+    
+ )) +)} +