fix(web): Sidebar as client component (no DB during SSG)
- Sidebar.tsx is now "use client" — fetches /api/docs at runtime instead of calling listDocuments() during SSG (CI has no DB, causes ECONNREFUSED) - GET /api/docs added to route.ts (returns doc list for sidebar) - Removed SidebarContent.tsx (merged into Sidebar.tsx) - Fixed isAuthorized double-brace typo
This commit is contained in:
@@ -1,12 +1,15 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import {
|
import {
|
||||||
createDocument,
|
createDocument,
|
||||||
|
updateDocument,
|
||||||
|
deleteDocument,
|
||||||
|
listDocuments,
|
||||||
} from "@mcpedia/core";
|
} from "@mcpedia/core";
|
||||||
import { WEBHOOK_SECRET } from "@mcpedia/config";
|
import { WEBHOOK_SECRET } from "@mcpedia/config";
|
||||||
import { timingSafeEqual } from "node:crypto";
|
import { timingSafeEqual } from "node:crypto";
|
||||||
|
|
||||||
// Web CRUD auth: either the `x-webhook-secret` header (API/MCP style) OR the
|
// Web CRUD auth: either the x-webhook-secret header (API/MCP style) OR the
|
||||||
// `mcpedia_admin` cookie (web login). One of the two must be present + valid.
|
// mcpedia_admin cookie (web login). One of the two must be valid.
|
||||||
function isAuthorized(req: NextRequest): boolean {
|
function isAuthorized(req: NextRequest): boolean {
|
||||||
if (!WEBHOOK_SECRET) return false;
|
if (!WEBHOOK_SECRET) return false;
|
||||||
const headerSecret = req.headers.get("x-webhook-secret") ?? "";
|
const headerSecret = req.headers.get("x-webhook-secret") ?? "";
|
||||||
@@ -21,8 +24,13 @@ function unauthorized() {
|
|||||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GET /api/docs — list all documents (for sidebar navigation).
|
||||||
|
export async function GET() {
|
||||||
|
const docs = await listDocuments();
|
||||||
|
return NextResponse.json(docs);
|
||||||
|
}
|
||||||
|
|
||||||
// POST /api/docs — Create a new document.
|
// POST /api/docs — Create a new document.
|
||||||
// Body: { slug, title, section, body, type?, status?, author?, tags? }
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
if (!isAuthorized(req)) return unauthorized();
|
if (!isAuthorized(req)) return unauthorized();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { listDocuments } from "@mcpedia/core";
|
|
||||||
|
interface Doc {
|
||||||
|
slug: string;
|
||||||
|
title: string;
|
||||||
|
section: string;
|
||||||
|
tags: string[];
|
||||||
|
}
|
||||||
|
|
||||||
const SECTIONS = [
|
const SECTIONS = [
|
||||||
{ id: "docs", label: "Documentation" },
|
{ id: "docs", label: "Documentation" },
|
||||||
@@ -8,20 +17,25 @@ const SECTIONS = [
|
|||||||
{ id: "notes", label: "Notes" },
|
{ id: "notes", label: "Notes" },
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export default async function Sidebar() {
|
/** Client-side sidebar (fetches doc list via /api/docs at runtime).
|
||||||
const docs = await listDocuments();
|
* Client component so `next build` SSG doesn't hit Postgres (CI has no DB). */
|
||||||
|
export default function Sidebar() {
|
||||||
|
const [docs, setDocs] = useState<Doc[]>([]);
|
||||||
|
|
||||||
// Build a tree: section → top-level slugs + children
|
useEffect(() => {
|
||||||
const bySection: Record<string, { slug: string; title: string; depth: number }[]> = {};
|
fetch("/api/docs")
|
||||||
|
.then((r) => r.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (Array.isArray(data)) setDocs(data);
|
||||||
|
else if (data?.docs) setDocs(data.docs);
|
||||||
|
})
|
||||||
|
.catch(() => setDocs([]));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const bySection: Record<string, Doc[]> = {};
|
||||||
for (const doc of docs) {
|
for (const doc of docs) {
|
||||||
if (!bySection[doc.section]) bySection[doc.section] = [];
|
if (!bySection[doc.section]) bySection[doc.section] = [];
|
||||||
// Count depth from dots in a flattened slug: docs/websocket/contract
|
bySection[doc.section].push(doc);
|
||||||
// We'll show all docs at their depth level
|
|
||||||
bySection[doc.section].push({
|
|
||||||
slug: doc.slug,
|
|
||||||
title: doc.title,
|
|
||||||
depth: doc.slug.split("/").length,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -37,13 +51,14 @@ export default async function Sidebar() {
|
|||||||
</div>
|
</div>
|
||||||
<ul className="space-y-0.5">
|
<ul className="space-y-0.5">
|
||||||
{sectionDocs.map((doc) => {
|
{sectionDocs.map((doc) => {
|
||||||
const parts = doc.slug.split("/").slice(1); // remove section prefix
|
const parts = doc.slug.split("/").slice(1);
|
||||||
const label = parts[parts.length - 1];
|
const label = parts[parts.length - 1];
|
||||||
const formatted = label
|
const formatted = label
|
||||||
.split(/[-_]/)
|
.split(/[-_]/)
|
||||||
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
|
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
|
||||||
.join(" ");
|
.join(" ");
|
||||||
const indent = doc.depth - 1;
|
const depth = doc.slug.split("/").length;
|
||||||
|
const indent = depth - 1;
|
||||||
return (
|
return (
|
||||||
<li key={doc.slug} style={{ marginLeft: `${indent * 12}px` }}>
|
<li key={doc.slug} style={{ marginLeft: `${indent * 12}px` }}>
|
||||||
<Link
|
<Link
|
||||||
|
|||||||
Reference in New Issue
Block a user