feat: list of pages

This commit is contained in:
Ivan Rezchikov
2026-07-13 20:11:42 +03:00
parent 2e356e4c47
commit 7a7ca2d831
7 changed files with 122 additions and 68 deletions
-2
View File
@@ -141,8 +141,6 @@ function BlockRenderer({ block }: { block: Block }) {
<img <img
src={b.url} src={b.url}
alt={b.caption || "Image"} alt={b.caption || "Image"}
width={1200}
height={800}
style={imageWidth && imageWidth < 100 ? { width: `${imageWidth}%` } : undefined} style={imageWidth && imageWidth < 100 ? { width: `${imageWidth}%` } : undefined}
className={cn( className={cn(
"h-auto rounded-md border object-contain", "h-auto rounded-md border object-contain",
-33
View File
@@ -1,33 +0,0 @@
import { NewsCard } from "@/components/news-card";
import { getPages } from "@/lib/pages/repository";
import { PAGE_TYPES, PageType } from "@/lib/pages/types";
import { getCurrentUser } from "@/lib/user";
import { Metadata } from "next";
import { notFound, redirect } from "next/navigation";
export const metadata: Metadata = { title: "Pages" };
export default async function Pages({ params }: { params: Promise<{ type: string }> }) {
const user = await getCurrentUser();
if (!user?.isAdmin) redirect("/");
const { type } = await params;
if (!PAGE_TYPES.includes(type as PageType)) {
notFound();
}
const pages = await getPages(type as PageType, "PUBLISHED");
return (
<div className="flex flex-col p-4 md:p-16 gap-8 md:gap-16 mb-4 md:mb-0">
<h2>All pages of type ${type}</h2>
<div className="flex flex-col gap-6">
{
pages.length > 0 ? pages.map((n) => (
<NewsCard news={n} key={n.id} />
)) : <h2 className="text-muted-foreground">There are no pages of that type yet...</h2>
}
</div>
</div>
);
}
+45
View File
@@ -0,0 +1,45 @@
import { getPages } from "@/lib/pages/repository";
import { getCurrentUser } from "@/lib/user";
import { formatDate } from "@/lib/utils";
import { ArchiveIcon, CalendarIcon } from "lucide-react";
import { Metadata } from "next";
import Link from "next/link";
import { redirect } from "next/navigation";
export const metadata: Metadata = { title: "List of pages" };
export default async function List() {
const user = await getCurrentUser();
if (!user?.isAdmin) redirect("/");
const pagesPublished = await getPages("PAGE", "PUBLISHED");
const pagesArchived = await getPages("PAGE", "ARCHIVED");
const pages = [...pagesPublished, ...pagesArchived].sort((a, b) => {
const aDate = new Date(a.updatedAt ?? a.publishedAt).getTime();
const bDate = new Date(b.updatedAt ?? b.publishedAt).getTime();
return bDate - aDate;
});
return (
<div className="flex flex-col p-4 md:p-16 gap-6 md:gap-10 mb-4 md:mb-0">
<h1>List of pages</h1>
<div className="flex flex-col gap-4">
{
pages.length > 0 ? pages.map((p) => (
<div className="flex flex-col gap-1" key={p.id}>
<Link href={`/page/${p.slug}`} className="max-w-vh">
<h3 className="truncate">{p.title}</h3>
</Link>
<div className="flex flex-row gap-4">
<h5 className="flex gap-1 items-center"><CalendarIcon size={14} />{formatDate(p.publishedAt)}</h5>
{p.status === "ARCHIVED" && <h5 className="flex gap-1 items-center"><ArchiveIcon size={14} />Archived</h5>}
</div>
</div>
)) : <h2 className="text-muted-foreground">There are no pages yet...</h2>
}
</div>
</div>
);
}
+14 -30
View File
@@ -16,6 +16,8 @@ import { Alert } from "./ui/alert";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { uploadPageImage, getImagePreviewUrl } from "@/lib/pages/actions"; import { uploadPageImage, getImagePreviewUrl } from "@/lib/pages/actions";
import { ImageUploader } from "@/components/image-uploader"; import { ImageUploader } from "@/components/image-uploader";
import { Slider } from "@/components/ui/slider";
import { Label } from "@/components/ui/label";
export type EditorData = { export type EditorData = {
title: string; title: string;
@@ -621,8 +623,6 @@ function TableEditor({ content, onChange }: { content: { type: "TABLE"; headers:
); );
} }
const WIDTH_PRESETS = [25, 50, 75, 100] as const;
function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Block; previewUrl?: string; onChange: (c: Block["content"]) => void; onPreviewReady: (id: string, url: string) => void }) { function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Block; previewUrl?: string; onChange: (c: Block["content"]) => void; onPreviewReady: (id: string, url: string) => void }) {
const content = block.content as { type: "IMAGE"; url: string; caption?: string; width?: number }; const content = block.content as { type: "IMAGE"; url: string; caption?: string; width?: number };
const [isUploading, setIsUploading] = useState(false); const [isUploading, setIsUploading] = useState(false);
@@ -646,17 +646,14 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
onChange({ type: "IMAGE", url: "", caption: content.caption ?? "", width: content.width }); onChange({ type: "IMAGE", url: "", caption: content.caption ?? "", width: content.width });
} }
function handleWidthChange(width: number) {
onChange({ type: "IMAGE", url: content.url, caption: content.caption ?? "", width });
}
return ( return (
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-1.5">
<ImageUploader <ImageUploader
id={`image-${block.id}`} id={`image-${block.id}`}
label="" label=""
variant="editor" variant="editor"
value={previewUrl} value={previewUrl}
previewStyle={currentWidth < 100 ? { width: `${currentWidth}%` } : undefined}
onFileSelect={handleFileSelect} onFileSelect={handleFileSelect}
onRemove={handleRemove} onRemove={handleRemove}
isUploading={isUploading} isUploading={isUploading}
@@ -665,30 +662,17 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
/> />
{content.url && ( {content.url && (
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-2">
<div className="flex items-center gap-3"> <div className="flex items-center justify-between gap-2">
<input <Label className="text-xs md:text-sm">Image size</Label>
type="range" <span className="text-xs md:text-sm text-muted-foreground">{currentWidth}%</span>
min={10}
max={100}
value={currentWidth}
onChange={(e) => handleWidthChange(Number(e.target.value))}
className="flex-1 h-1.5 bg-muted rounded-full appearance-none cursor-pointer accent-primary"
/>
<span className="text-xs text-muted-foreground w-8 text-right tabular-nums">{currentWidth}%</span>
</div>
<div className="flex gap-1.5">
{WIDTH_PRESETS.map((preset) => (
<Button
key={preset}
variant={currentWidth === preset ? "default" : "outline"}
size="sm"
className="h-7 px-2 text-xs"
onClick={() => handleWidthChange(preset)}
>
{preset}%
</Button>
))}
</div> </div>
<Slider
value={[currentWidth]}
min={10}
max={100}
step={1}
onValueChange={([v]) => onChange({ type: "IMAGE", url: content.url, caption: content.caption ?? "", width: v })}
/>
</div> </div>
)} )}
</div> </div>
+3 -2
View File
@@ -16,6 +16,7 @@ type Props = {
error?: string; error?: string;
variant?: "default" | "editor"; variant?: "default" | "editor";
previewClassName?: string; previewClassName?: string;
previewStyle?: React.CSSProperties;
accept?: string; accept?: string;
required?: boolean; required?: boolean;
value?: string; value?: string;
@@ -55,7 +56,7 @@ function fileToFileList(file: File): FileList {
return dt.files; return dt.files;
} }
export function ImageUploader({ id, name, label, description, error, variant = "default", previewClassName, accept = DEFAULT_ACCEPT, required, value, onFileSelect, onRemove, isUploading, children, caption, onCaptionChange }: Props) { export function ImageUploader({ id, name, label, description, error, variant = "default", previewClassName, previewStyle, accept = DEFAULT_ACCEPT, required, value, onFileSelect, onRemove, isUploading, children, caption, onCaptionChange }: Props) {
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
const [previewUrl, setPreviewUrl] = useState<string | null>(null); const [previewUrl, setPreviewUrl] = useState<string | null>(null);
const [fileInfo, setFileInfo] = useState<{ name: string; size: number } | null>(null); const [fileInfo, setFileInfo] = useState<{ name: string; size: number } | null>(null);
@@ -124,7 +125,7 @@ export function ImageUploader({ id, name, label, description, error, variant = "
{displayUrl ? ( {displayUrl ? (
<div className={EDITOR_WRAPPER_CLASS}> <div className={EDITOR_WRAPPER_CLASS}>
<div className="group relative rounded-md"> <div className="group relative rounded-md">
<img src={displayUrl} alt="" className={previewClassName ?? EDITOR_IMG_CLASS} /> <img src={displayUrl} alt="" className={previewClassName ?? EDITOR_IMG_CLASS} style={previewStyle} />
<div className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity"> <div className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity">
<Button type="button" variant="secondary" size="sm" onClick={() => inputRef.current?.click()} disabled={isUploading}> <Button type="button" variant="secondary" size="sm" onClick={() => inputRef.current?.click()} disabled={isUploading}>
+1 -1
View File
@@ -6,7 +6,7 @@ import { formatDate } from "@/lib/utils";
export function NewsCard({ news }: { news: PagePublic }) { export function NewsCard({ news }: { news: PagePublic }) {
return ( return (
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-1">
<Link href={`/news/${news.slug}`} className="w-max"> <Link href={`/news/${news.slug}`} className="w-max">
<h2 className="hover:underline underline-offset-8 decoration-2 hover:duration-200 cursor-pointer">{news.title}</h2> <h2 className="hover:underline underline-offset-8 decoration-2 hover:duration-200 cursor-pointer">{news.title}</h2>
</Link> </Link>
+59
View File
@@ -0,0 +1,59 @@
"use client"
import * as React from "react"
import { Slider as SliderPrimitive } from "radix-ui"
import { cn } from "@/lib/utils"
function Slider({
className,
defaultValue,
value,
min = 0,
max = 100,
...props
}: React.ComponentProps<typeof SliderPrimitive.Root>) {
const _values = React.useMemo(
() =>
Array.isArray(value)
? value
: Array.isArray(defaultValue)
? defaultValue
: [min, max],
[value, defaultValue, min, max]
)
return (
<SliderPrimitive.Root
data-slot="slider"
defaultValue={defaultValue}
value={value}
min={min}
max={max}
className={cn(
"relative flex w-full touch-none items-center select-none data-disabled:opacity-50 data-vertical:h-full data-vertical:min-h-40 data-vertical:w-auto data-vertical:flex-col",
className
)}
{...props}
>
<SliderPrimitive.Track
data-slot="slider-track"
className="relative grow overflow-hidden rounded-2xl bg-input/90 data-horizontal:h-1 data-horizontal:w-full data-vertical:h-full data-vertical:w-1"
>
<SliderPrimitive.Range
data-slot="slider-range"
className="absolute bg-primary select-none data-horizontal:h-full data-vertical:w-full"
/>
</SliderPrimitive.Track>
{Array.from({ length: _values.length }, (_, index) => (
<SliderPrimitive.Thumb
data-slot="slider-thumb"
key={index}
className="block size-4 shrink-0 rounded-2xl bg-white shadow-md ring-1 ring-black/10 transition-[color,box-shadow] duration-200 select-none not-dark:bg-clip-padding hover:ring-4 hover:ring-ring/30 focus-visible:ring-4 focus-visible:ring-ring/30 focus-visible:outline-hidden disabled:pointer-events-none disabled:opacity-50"
/>
))}
</SliderPrimitive.Root>
)
}
export { Slider }