Säkerhetskopior, papperskorg, historik, härdning, adminsida och snabbsök

- Automatisk nattlig säkerhetskopiering (zip per bok med bilder), schemaläggare via
  instrumentation, adminsida med manuell körning och nedladdning
- Papperskorg för poster (ögonblicksbild med relationer, scheman, markeringar) och
  böcker (zip), återställning och automatisk rensning
- Ändringshistorik för poster med fältvis diff och återställning
- Härdning: säkerhetsrubriker och CSP via middleware, hastighetsbegränsning, lagringskvot
  per användare, konfigurerbar sessionstid
- Bildoptimering med sharp (EXIF-rotation, nedskalning, metadata borttagen) och miniatyrer
- Strukturerade JSON-loggar, onRequestError och Prometheus-mätvärden på /api/metrics
- Snabbsök och kommandopalett (Ctrl+K) med sökning i poster och mallfält
- Migration 0005_system, nya volymer och miljövariabler, versionsinfo via build-args
- README, .env.example och TODO uppdaterade

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-27 23:04:05 +01:00
1 parent 323e50d9ac
commit 4e8d79ee10
60 files changed
+2507 -259

No files matched your search

+3 -2
View File
@@ -13,10 +13,10 @@ export default async function NewEntityPage({
searchParams,
}: {
params: Promise<{ bookId: string; kind: string }>;
searchParams: Promise<{ name?: string }>;
searchParams: Promise<{ name?: string; error?: string }>;
}) {
const { bookId, kind: slug } = await params;
const { name } = await searchParams;
const { name, error } = await searchParams;
await requireBook(bookId);
const info = KIND_BY_SLUG.get(slug);
if (!info) notFound();
@@ -32,6 +32,7 @@ export default async function NewEntityPage({
<>
<PageHeader title={t(`kind.${info.kind}.new`)} pretitle={t(`kind.${info.kind}.other`)} />
<PageBody>
{error === "quota" ? <div className="alert alert-danger">{t("quota.exceeded")}</div> : null}
<EntityForm
action={saveEntity}
bookId={bookId}
+1 -1
View File
@@ -88,7 +88,7 @@ export default async function EntityListPage({
<table className="table table-vcenter card-table">
<tbody>
{entities.map((entity) => {
const image = fileUrl(entity.imageFile);
const image = fileUrl(entity.imageFile, "thumb");
return (
<tr key={entity.id}>
<td className="w-1">
@@ -6,8 +6,15 @@ import { prisma } from "@/lib/db";
import { requireEntity } from "@/lib/session";
import { getTemplate } from "@/lib/templates";
export default async function EditEntityPage({ params }: { params: Promise<{ bookId: string; entityId: string }> }) {
export default async function EditEntityPage({
params,
searchParams,
}: {
params: Promise<{ bookId: string; entityId: string }>;
searchParams: Promise<{ error?: string }>;
}) {
const { bookId, entityId } = await params;
const { error } = await searchParams;
const entity = await requireEntity(bookId, entityId);
const { t, locale } = await getT();
const { fields } = await getTemplate(bookId, entity.kind, locale);
@@ -21,6 +28,7 @@ export default async function EditEntityPage({ params }: { params: Promise<{ boo
<>
<PageHeader title={t("entity.editTitle", { name: entity.name })} pretitle={t(`kind.${entity.kind}.one`)} />
<PageBody>
{error === "quota" ? <div className="alert alert-danger">{t("quota.exceeded")}</div> : null}
<EntityForm
action={saveEntity}
bookId={bookId}
@@ -0,0 +1,101 @@
import Link from "next/link";
import { IconArrowBackUp } from "@tabler/icons-react";
import { restoreVersion } from "@/actions/history";
import { ConfirmSubmit } from "@/components/confirm-submit";
import { EmptyState, PageBody, PageHeader } from "@/components/page";
import { getT } from "@/i18n/server";
import { prisma } from "@/lib/db";
import { diffSnapshots, MAX_VERSIONS, snapshotOf, type Snapshot } from "@/lib/history";
import { requireEntity } from "@/lib/session";
import { getTemplate } from "@/lib/templates";
export const metadata = { title: "Historik" };
export default async function HistoryPage({ params }: { params: Promise<{ bookId: string; entityId: string }> }) {
const { bookId, entityId } = await params;
const entity = await requireEntity(bookId, entityId);
const { t, locale } = await getT();
const { fields } = await getTemplate(bookId, entity.kind, locale);
const versions = await prisma.entityVersion.findMany({ where: { entityId, bookId }, orderBy: { createdAt: "desc" } });
const users = await prisma.user.findMany({
where: { id: { in: [...new Set(versions.map((v) => v.userId).filter((id): id is string => !!id))] } },
select: { id: true, name: true, email: true },
});
const userName = new Map(users.map((u) => [u.id, u.name ?? u.email ?? ""]));
const labels = { name: t("common.name"), summary: t("common.summary"), tags: t("common.tags") };
// Varje version jämförs med det som kom efter den (nästa version, eller nuvarande för den senaste)
let newer: Snapshot = snapshotOf(entity);
const rows = versions.map((version) => {
const snapshot = version.snapshot as unknown as Snapshot;
const changes = diffSnapshots(snapshot, newer, fields, labels);
newer = snapshot;
return { version, changes };
});
return (
<>
<PageHeader
title={t("history.title", { name: entity.name })}
pretitle={
<Link href={`/books/${bookId}/entity/${entityId}`} className="text-reset">
{entity.name}
</Link>
}
/>
<PageBody>
<p className="text-secondary">{t("history.intro", { n: MAX_VERSIONS })}</p>
{rows.length === 0 ? (
<div className="card">
<div className="card-body">
<EmptyState>{t("history.empty")}</EmptyState>
</div>
</div>
) : (
rows.map(({ version, changes }) => (
<div className="card mb-3" key={version.id}>
<div className="card-header">
<h3 className="card-title">
{version.createdAt.toISOString().slice(0, 16).replace("T", " ")}
{version.userId && userName.get(version.userId) ? (
<span className="text-secondary fw-normal"> · {userName.get(version.userId)}</span>
) : null}
</h3>
<div className="card-actions">
<form action={restoreVersion}>
<input type="hidden" name="bookId" value={bookId} />
<input type="hidden" name="entityId" value={entityId} />
<input type="hidden" name="versionId" value={version.id} />
<ConfirmSubmit className="btn btn-sm" message={t("history.restoreConfirm")}>
<IconArrowBackUp size={16} className="me-1" />
{t("history.restore")}
</ConfirmSubmit>
</form>
</div>
</div>
<div className="list-group list-group-flush">
{changes.length === 0 ? <div className="list-group-item text-secondary">{t("history.noChanges")}</div> : null}
{changes.map((change) => (
<div className="list-group-item" key={change.label}>
<div className="fw-bold">{change.label}</div>
<div className="row g-2">
<div className="col-md-6">
<div className="text-secondary small">{t("history.before")}</div>
<div className="bm-prewrap">{change.before || <span className="text-secondary">—</span>}</div>
</div>
<div className="col-md-6">
<div className="text-secondary small">{t("history.after")}</div>
<div className="bm-prewrap">{change.after || <span className="text-secondary">—</span>}</div>
</div>
</div>
</div>
))}
</div>
</div>
))
)}
</PageBody>
</>
);
}
@@ -1,5 +1,5 @@
import Link from "next/link";
import { IconMapPin, IconPencil, IconTrash, IconX } from "@tabler/icons-react";
import { IconHistory, IconMapPin, IconPencil, IconTrash, IconX } from "@tabler/icons-react";
import { deleteEntity } from "@/actions/entities";
import { deleteRelation } from "@/actions/relations";
import { ConfirmSubmit } from "@/components/confirm-submit";
@@ -64,6 +64,7 @@ export default async function EntityPage({ params }: { params: Promise<{ bookId:
prisma.bookMap.findMany({ where: { bookId }, orderBy: { name: "asc" } }),
]);
const versionCount = await prisma.entityVersion.count({ where: { entityId: entity.id } });
const info = KIND_INFO[entity.kind];
const Icon = kindIcons[entity.kind];
const image = fileUrl(entity.imageFile);
@@ -155,10 +156,16 @@ export default async function EntityPage({ params }: { params: Promise<{ bookId:
<IconPencil size={18} className="me-1" />
{t("common.edit")}
</Link>
{versionCount > 0 ? (
<Link href={`${entityHref(entity.id)}/history`} className="btn">
<IconHistory size={18} className="me-1" />
{t("history.button", { n: versionCount })}
</Link>
) : null}
<form action={deleteEntity}>
<input type="hidden" name="bookId" value={bookId} />
<input type="hidden" name="id" value={entity.id} />
<ConfirmSubmit className="btn btn-outline-danger">
<ConfirmSubmit className="btn btn-outline-danger" message={t("trash.moveConfirm")}>
<IconTrash size={18} className="me-1" />
{t("common.delete")}
</ConfirmSubmit>
+9 -1
View File
@@ -8,8 +8,15 @@ import { requireBook } from "@/lib/session";
export const metadata = { title: "Kartor" };
export default async function MapsPage({ params }: { params: Promise<{ bookId: string }> }) {
export default async function MapsPage({
params,
searchParams,
}: {
params: Promise<{ bookId: string }>;
searchParams: Promise<{ error?: string }>;
}) {
const { bookId } = await params;
const { error } = await searchParams;
await requireBook(bookId);
const { t } = await getT();
const maps = await prisma.bookMap.findMany({
@@ -22,6 +29,7 @@ export default async function MapsPage({ params }: { params: Promise<{ bookId: s
<>
<PageHeader title={t("map.title")} />
<PageBody>
{error === "quota" ? <div className="alert alert-danger">{t("quota.exceeded")}</div> : null}
<div className="row row-cards">
<div className="col-lg-8">
{maps.length === 0 ? (
+104
View File
@@ -0,0 +1,104 @@
import Link from "next/link";
import { IconArrowBackUp, IconTrash } from "@tabler/icons-react";
import { emptyTrash, purgeTrash, restoreTrashEntity } from "@/actions/trash";
import { ConfirmSubmit } from "@/components/confirm-submit";
import { kindIcons } from "@/components/kind-icons";
import { EmptyState, PageBody, PageHeader } from "@/components/page";
import { getT } from "@/i18n/server";
import { prisma } from "@/lib/db";
import { KIND_INFO, parseKind } from "@/lib/kinds";
import { requireBook } from "@/lib/session";
import { TRASH_DAYS } from "@/lib/trash";
export const metadata = { title: "Papperskorg" };
export default async function TrashPage({
params,
searchParams,
}: {
params: Promise<{ bookId: string }>;
searchParams: Promise<{ restore?: string }>;
}) {
const { bookId } = await params;
const { restore } = await searchParams;
const book = await requireBook(bookId);
const { t } = await getT();
const items = await prisma.trashItem.findMany({
where: { ownerId: book.ownerId, bookId, kind: "entity" },
orderBy: { deletedAt: "desc" },
});
return (
<>
<PageHeader
title={t("trash.title")}
actions={
items.length > 0 ? (
<form action={emptyTrash}>
<input type="hidden" name="bookId" value={bookId} />
<ConfirmSubmit className="btn btn-outline-danger" message={t("trash.emptyConfirm")}>
<IconTrash size={18} className="me-1" />
{t("trash.empty")}
</ConfirmSubmit>
</form>
) : null
}
/>
<PageBody>
{restore === "failed" ? <div className="alert alert-warning">{t("trash.restoreFailed")}</div> : null}
<p className="text-secondary">{t("trash.intro", { days: TRASH_DAYS })}</p>
<div className="card">
{items.length === 0 ? (
<div className="card-body">
<EmptyState>{t("trash.emptyState")}</EmptyState>
</div>
) : (
<div className="list-group list-group-flush">
{items.map((item) => {
const kind = parseKind(item.detail);
const Icon = kind ? kindIcons[kind] : null;
const daysLeft = Math.max(0, TRASH_DAYS - Math.floor((Date.now() - item.deletedAt.getTime()) / 86_400_000));
return (
<div key={item.id} className="list-group-item d-flex align-items-center gap-3">
{Icon && kind ? (
<span className={`avatar avatar-sm bg-${KIND_INFO[kind].color}-lt`}>
<Icon size={16} />
</span>
) : null}
<div className="flex-fill min-w-0">
<div className="fw-bold text-truncate">{item.label}</div>
<div className="text-secondary small">
{kind ? t(`kind.${kind}.one`) : ""} · {t("trash.deleted", { date: item.deletedAt.toISOString().slice(0, 10) })} ·{" "}
{t("trash.daysLeft", { n: daysLeft })}
</div>
</div>
<form action={restoreTrashEntity}>
<input type="hidden" name="bookId" value={bookId} />
<input type="hidden" name="id" value={item.id} />
<button type="submit" className="btn btn-sm">
<IconArrowBackUp size={16} className="me-1" />
{t("trash.restore")}
</button>
</form>
<form action={purgeTrash}>
<input type="hidden" name="bookId" value={bookId} />
<input type="hidden" name="id" value={item.id} />
<ConfirmSubmit className="btn btn-sm btn-outline-danger" message={t("trash.purgeConfirm")}>
{t("trash.purge")}
</ConfirmSubmit>
</form>
</div>
);
})}
</div>
)}
</div>
<div className="mt-3">
<Link href={`/books/${bookId}`} className="btn">
{t("common.back")}
</Link>
</div>
</PageBody>
</>
);
}