import Link from "next/link"; import { notFound } from "next/navigation"; import { Fragment } from "react"; import { IconTrash } from "@tabler/icons-react"; import { deleteRelation, saveRelation } from "@/actions/relations"; import { ConfirmSubmit } from "@/components/confirm-submit"; import { ConflictForm } from "@/components/conflict-form"; import { EntitySelect } from "@/components/entity-select"; import { FieldInput } from "@/components/entity-form"; import { RelationBondFields } from "@/components/relation-bond-fields"; import { PageBody, PageHeader } from "@/components/page"; import { getT } from "@/i18n/server"; import { versionOf } from "@/lib/conflict"; import { whoIsEditing } from "@/lib/presence"; import { prisma } from "@/lib/db"; import { requireBook } from "@/lib/session"; import { getTemplate } from "@/lib/templates"; export default async function RelationPage({ params, searchParams, }: { params: Promise<{ bookId: string; relationId: string }>; searchParams: Promise<{ saved?: string }>; }) { const { bookId, relationId } = await params; const { saved } = await searchParams; const access = await requireBook(bookId, "EDITOR"); const { t, locale } = await getT(); const relation = await prisma.relation.findFirst({ where: { id: relationId, bookId }, include: { from: true, to: true, members: { include: { entity: true } } }, }); if (!relation) notFound(); const editing = await whoIsEditing(bookId, "relation", relation.id, access.userId); const candidates = await prisma.entity.findMany({ where: { bookId, id: { notIn: [relation.fromId, relation.toId] } }, select: { id: true, name: true, kind: true }, orderBy: { name: "asc" }, }); const refEntities = await prisma.entity.findMany({ where: { bookId }, select: { id: true, name: true, kind: true, code: true }, orderBy: { name: "asc" }, }); const memberEntities = relation.members.map((m) => m.entity); const isGroup = memberEntities.length > 0; const { fields } = await getTemplate(bookId, "RELATION", locale); const data = (relation.data ?? {}) as Record; const suggestions = t("relation.suggestions").split(","); return ( <> {t("relation.title")} {relation.code ? {relation.code} : null} } title={ isGroup ? ( {[relation.from, relation.to, ...memberEntities].map((e, i) => ( {i > 0 ? ", " : ""} {e.name} ))} · {relation.type} ) : ( {relation.from.name} · {relation.type} · {relation.to.name} ) } actions={
{t("common.delete")}
} /> {saved ?
{t("common.saved")}
: null}
{suggestions.map((s) => (
e.id)} />
{t("relation.membersHelp")}
{fields.length > 0 ? (

{t("entity.details")}

{fields.map((field) => ( {field.section ? (

{field.section}

) : null}
{field.type !== "checkbox" ? : null}
))}
) : null}
{t("common.back")}
); }