prod
This commit is contained in:
@@ -3,6 +3,23 @@ import { getCurrentUser } from "@/lib/user";
|
||||
import { redirect, notFound } from "next/navigation";
|
||||
import { PAGE_TYPES, PageType } from "@/lib/pages/types";
|
||||
import EditPageClient from "./client";
|
||||
import { Metadata } from "next";
|
||||
|
||||
export async function generateMetadata({ params }: { params: Promise<{ type: string; slug: string }>}): Promise<Metadata> {
|
||||
const { type, slug } = await params;
|
||||
|
||||
const pageType = type.toUpperCase() as PageType;
|
||||
|
||||
const page = await getPageBySlug(slug, pageType);
|
||||
|
||||
return {
|
||||
title: page ? `Edit «${page.title}» page` : "Edit page",
|
||||
robots: {
|
||||
index: false,
|
||||
follow: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default async function EditPage({ params }: { params: Promise<{ type: string; slug: string }> }) {
|
||||
const user = await getCurrentUser();
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import Header from "@/components/header";
|
||||
import Footer from "@/components/footer";
|
||||
import { getPageBySlug, getBlocksByPageId } from "@/lib/pages/repository";
|
||||
import { getCurrentUser, getUsers } from "@/lib/user";
|
||||
import { notFound } from "next/navigation";
|
||||
@@ -14,7 +12,83 @@ import { ArchiveButton } from "./archive-button";
|
||||
import { PinToggleButton } from "./pin-toggle-button";
|
||||
import { getNavLinkByPageId } from "@/lib/navlinks/repository";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import Image from "next/image";
|
||||
import { Metadata } from "next";
|
||||
import { getImageUrl } from "@/lib/image";
|
||||
|
||||
function makeDescription(text?: string, maxLength = 160) {
|
||||
if (!text) return undefined;
|
||||
|
||||
const normalized = text.replace(/\s+/g, " ").trim();
|
||||
|
||||
if (normalized.length <= maxLength) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const cut = normalized.lastIndexOf(" ", maxLength);
|
||||
|
||||
return (cut > 0 ? normalized.slice(0, cut) : normalized.slice(0, maxLength)) + "…";
|
||||
}
|
||||
|
||||
async function resolveBlockImageUrls(blocks: Block[]): Promise<Block[]> {
|
||||
return Promise.all(
|
||||
blocks.map(async (block) => {
|
||||
if (block.type !== "IMAGE") return block;
|
||||
const content = block.content as { type: "IMAGE"; url: string; caption?: string };
|
||||
if (!content.url) return block;
|
||||
try {
|
||||
const presignedUrl = await getImageUrl(content.url);
|
||||
return { ...block, content: { ...content, url: presignedUrl } };
|
||||
} catch {
|
||||
return block;
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function generateMetadata({ params }: { params: Promise<{ type: string; slug: string }>}): Promise<Metadata> {
|
||||
const { type, slug } = await params;
|
||||
|
||||
const pageType = type.toUpperCase() as PageType;
|
||||
if (!PAGE_TYPES.includes(pageType)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const page = await getPageBySlug(slug, pageType);
|
||||
if (!page) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const blocks = await getBlocksByPageId(page.id);
|
||||
const resolvedBlocksForMeta = await resolveBlockImageUrls(blocks);
|
||||
|
||||
const firstParagraph = resolvedBlocksForMeta.find(
|
||||
(b): b is Block & { content: { type: "PARAGRAPH"; text: string } } =>
|
||||
b.content.type === "PARAGRAPH"
|
||||
)?.content.text;
|
||||
|
||||
const firstImage = resolvedBlocksForMeta.find(
|
||||
(b): b is Block & { content: { type: "IMAGE"; url: string } } =>
|
||||
b.content.type === "IMAGE"
|
||||
)?.content.url;
|
||||
|
||||
const description = makeDescription(firstParagraph);
|
||||
|
||||
return {
|
||||
title: page.title,
|
||||
description,
|
||||
openGraph: {
|
||||
title: page.title,
|
||||
description,
|
||||
images: firstImage ? [{ url: firstImage }] : undefined,
|
||||
},
|
||||
twitter: {
|
||||
card: firstImage ? "summary_large_image" : "summary",
|
||||
title: page.title,
|
||||
description,
|
||||
images: firstImage ? [firstImage] : undefined,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function BlockRenderer({ block }: { block: Block }) {
|
||||
const b = block.content;
|
||||
@@ -63,7 +137,7 @@ function BlockRenderer({ block }: { block: Block }) {
|
||||
case "IMAGE":
|
||||
return (
|
||||
<figure className="flex flex-col gap-2">
|
||||
<Image
|
||||
<img
|
||||
src={b.url}
|
||||
alt={b.caption || "Image"}
|
||||
width={1200}
|
||||
@@ -146,6 +220,7 @@ export default async function Page({ params }: { params: Promise<{ type: string;
|
||||
const navlink = await getNavLinkByPageId(page.id);
|
||||
|
||||
const blocks = page ? await getBlocksByPageId(page.id) : [];
|
||||
const resolvedBlocks = await resolveBlockImageUrls(blocks);
|
||||
|
||||
const users = await getUsers(page.createdBy, page.updatedBy);
|
||||
const createdBy = users.find((u) => u.id === page.createdBy) ?? null;
|
||||
@@ -196,7 +271,7 @@ export default async function Page({ params }: { params: Promise<{ type: string;
|
||||
<AlertDescription>It can be seen only by administrators. Make any edits to make it visible again.</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
{blocks.map((block) => (
|
||||
{resolvedBlocks.map((block) => (
|
||||
<BlockRenderer key={block.id} block={block} />
|
||||
))}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user