first commit
This commit is contained in:
commit
b7b0f253ce
85 files changed
+7560
No files matched your search
@@ -0,0 +1,161 @@
|
||||
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 { EntitySelect } from "@/components/entity-select";
|
||||
import { FieldInput } from "@/components/entity-form";
|
||||
import { PageBody, PageHeader } from "@/components/page";
|
||||
import { getT } from "@/i18n/server";
|
||||
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;
|
||||
await requireBook(bookId);
|
||||
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 candidates = await prisma.entity.findMany({
|
||||
where: { bookId, id: { notIn: [relation.fromId, relation.toId] } },
|
||||
select: { id: true, name: true, kind: 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<string, unknown>;
|
||||
const suggestions = t("relation.suggestions").split(",");
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader
|
||||
pretitle={
|
||||
<Link href={`/books/${bookId}/relations`} className="text-reset">
|
||||
{t("relation.title")}
|
||||
</Link>
|
||||
}
|
||||
title={
|
||||
isGroup ? (
|
||||
<span>
|
||||
{[relation.from, relation.to, ...memberEntities].map((e, i) => (
|
||||
<span key={e.id}>
|
||||
{i > 0 ? ", " : ""}
|
||||
<Link href={`/books/${bookId}/entity/${e.id}`} className="text-reset">
|
||||
{e.name}
|
||||
</Link>
|
||||
</span>
|
||||
))}
|
||||
<span className="text-secondary fw-normal"> · {relation.type}</span>
|
||||
</span>
|
||||
) : (
|
||||
<span>
|
||||
<Link href={`/books/${bookId}/entity/${relation.fromId}`} className="text-reset">
|
||||
{relation.from.name}
|
||||
</Link>
|
||||
<span className="text-secondary fw-normal"> · {relation.type} · </span>
|
||||
<Link href={`/books/${bookId}/entity/${relation.toId}`} className="text-reset">
|
||||
{relation.to.name}
|
||||
</Link>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
actions={
|
||||
<form action={deleteRelation}>
|
||||
<input type="hidden" name="bookId" value={bookId} />
|
||||
<input type="hidden" name="id" value={relation.id} />
|
||||
<input type="hidden" name="redirect" value="list" />
|
||||
<ConfirmSubmit className="btn btn-outline-danger">
|
||||
<IconTrash size={18} className="me-1" />
|
||||
{t("common.delete")}
|
||||
</ConfirmSubmit>
|
||||
</form>
|
||||
}
|
||||
/>
|
||||
<PageBody>
|
||||
{saved ? <div className="alert alert-success">{t("common.saved")}</div> : null}
|
||||
<form action={saveRelation}>
|
||||
<input type="hidden" name="bookId" value={bookId} />
|
||||
<input type="hidden" name="id" value={relation.id} />
|
||||
<div className="card mb-3">
|
||||
<div className="card-body">
|
||||
<div className="row g-3">
|
||||
<div className="col-md-6">
|
||||
<label className="form-label required">{t("relation.type")}</label>
|
||||
<input name="type" className="form-control" list="relation-types" required maxLength={100} defaultValue={relation.type} />
|
||||
<datalist id="relation-types">
|
||||
{suggestions.map((s) => (
|
||||
<option key={s} value={s} />
|
||||
))}
|
||||
</datalist>
|
||||
</div>
|
||||
<div className="col-md-6">
|
||||
<label className="form-label">{t("common.note")}</label>
|
||||
<input name="description" className="form-control" maxLength={2000} defaultValue={relation.description ?? ""} />
|
||||
</div>
|
||||
<div className="col-12">
|
||||
<label className="form-label">{t("relation.members")}</label>
|
||||
<EntitySelect
|
||||
name="memberIds"
|
||||
entities={candidates}
|
||||
t={t}
|
||||
multiple
|
||||
defaultValues={memberEntities.map((e) => e.id)}
|
||||
/>
|
||||
<div className="form-hint">{t("relation.membersHelp")}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{fields.length > 0 ? (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("entity.details")}</h3>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<div className="row g-3">
|
||||
{fields.map((field) => (
|
||||
<Fragment key={field.key}>
|
||||
{field.section ? (
|
||||
<div className="col-12">
|
||||
<h4 className="subheader border-bottom pb-2 mb-0 mt-2">{field.section}</h4>
|
||||
</div>
|
||||
) : null}
|
||||
<div className={field.type === "textarea" ? "col-12" : "col-md-6"}>
|
||||
{field.type !== "checkbox" ? <label className="form-label">{field.label}</label> : null}
|
||||
<FieldInput field={field} value={data[field.key]} />
|
||||
</div>
|
||||
</Fragment>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<div className="d-flex justify-content-end gap-2">
|
||||
<Link href={`/books/${bookId}/relations`} className="btn">
|
||||
{t("common.back")}
|
||||
</Link>
|
||||
<button type="submit" className="btn btn-primary">
|
||||
{t("common.save")}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</PageBody>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import Link from "next/link";
|
||||
import { IconX } from "@tabler/icons-react";
|
||||
import { deleteRelation } from "@/actions/relations";
|
||||
import { EntitySelect } from "@/components/entity-select";
|
||||
import { EmptyState, PageBody, PageHeader } from "@/components/page";
|
||||
import { RelationForm } from "@/components/relation-form";
|
||||
import { RelationGraph } from "@/components/relation-graph";
|
||||
import { getT } from "@/i18n/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { requireBook } from "@/lib/session";
|
||||
|
||||
export const metadata = { title: "Relationer" };
|
||||
|
||||
export default async function RelationsPage({
|
||||
params,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<{ bookId: string }>;
|
||||
searchParams: Promise<{ entity?: string }>;
|
||||
}) {
|
||||
const { bookId } = await params;
|
||||
const { entity: entityFilter } = await searchParams;
|
||||
await requireBook(bookId);
|
||||
const { t } = await getT();
|
||||
|
||||
const [entities, relations] = await Promise.all([
|
||||
prisma.entity.findMany({ where: { bookId }, select: { id: true, name: true, kind: true }, orderBy: { name: "asc" } }),
|
||||
prisma.relation.findMany({
|
||||
where: {
|
||||
bookId,
|
||||
...(entityFilter
|
||||
? { OR: [{ fromId: entityFilter }, { toId: entityFilter }, { members: { some: { entityId: entityFilter } } }] }
|
||||
: {}),
|
||||
},
|
||||
include: { from: true, to: true, members: { include: { entity: true } } },
|
||||
orderBy: { createdAt: "asc" },
|
||||
}),
|
||||
]);
|
||||
|
||||
const nodeMap = new Map<string, { id: string; name: string; kind: (typeof entities)[number]["kind"] }>();
|
||||
for (const r of relations) {
|
||||
nodeMap.set(r.from.id, r.from);
|
||||
nodeMap.set(r.to.id, r.to);
|
||||
for (const m of r.members) nodeMap.set(m.entity.id, m.entity);
|
||||
}
|
||||
const nodes = [...nodeMap.values()];
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader title={t("relation.title")} />
|
||||
<PageBody>
|
||||
<div className="row row-cards">
|
||||
<div className="col-lg-8">
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("relation.diagram")}</h3>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
{relations.length === 0 ? (
|
||||
<EmptyState>{t("relation.empty")}</EmptyState>
|
||||
) : nodes.length > 40 ? null : (
|
||||
<RelationGraph
|
||||
bookId={bookId}
|
||||
nodes={nodes}
|
||||
edges={relations.map((r) => ({
|
||||
id: r.id,
|
||||
type: r.type,
|
||||
participantIds: [r.fromId, r.toId, ...r.members.map((m) => m.entityId)],
|
||||
}))}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("relation.all")}</h3>
|
||||
<div className="card-actions">
|
||||
<form className="d-flex gap-2">
|
||||
<EntitySelect
|
||||
name="entity"
|
||||
entities={entities}
|
||||
t={t}
|
||||
defaultValue={entityFilter}
|
||||
emptyLabel={t("relation.filter")}
|
||||
className="form-select form-select-sm"
|
||||
/>
|
||||
<button className="btn btn-sm" type="submit">
|
||||
{t("common.search")}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{relations.length === 0 ? null : (
|
||||
<div className="table-responsive">
|
||||
<table className="table table-vcenter card-table">
|
||||
<tbody>
|
||||
{relations.map((r) => (
|
||||
<tr key={r.id}>
|
||||
<td>
|
||||
<Link href={`/books/${bookId}/entity/${r.fromId}`} className="fw-bold">
|
||||
{r.from.name}
|
||||
</Link>
|
||||
</td>
|
||||
<td>
|
||||
<Link href={`/books/${bookId}/relations/${r.id}`} className="text-secondary" title={t("common.edit")}>
|
||||
{r.type}
|
||||
</Link>
|
||||
</td>
|
||||
<td>
|
||||
{[r.to, ...r.members.map((m) => m.entity)].map((e, i) => (
|
||||
<span key={e.id}>
|
||||
{i > 0 ? ", " : ""}
|
||||
<Link href={`/books/${bookId}/entity/${e.id}`} className="fw-bold">
|
||||
{e.name}
|
||||
</Link>
|
||||
</span>
|
||||
))}
|
||||
</td>
|
||||
<td className="text-secondary">{r.description}</td>
|
||||
<td className="w-1">
|
||||
<form action={deleteRelation}>
|
||||
<input type="hidden" name="bookId" value={bookId} />
|
||||
<input type="hidden" name="id" value={r.id} />
|
||||
<button type="submit" className="btn btn-icon btn-sm btn-ghost-secondary" aria-label={t("common.delete")} title={t("common.delete")}>
|
||||
<IconX size={16} />
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="col-lg-4">
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("relation.add")}</h3>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<RelationForm bookId={bookId} entities={entities} t={t} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</PageBody>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user