Släktträd och tidslinje
Relationer får ett släktband (förälder, partner, syskon) och en period. Nytt släktträd som ritas av släktbanden, och en tidslinje som samlar födelse och död, scenarier, egna händelser och relationsperioder med ålder vid varje tillfälle. Datum skrivs som ÅÅÅÅ, ÅÅÅÅ-MM eller ÅÅÅÅ-MM-DD. Följer med i export, import och papperskorg. Migreringen 0006 ger gamla relationer som "Förälder till" rätt släktband. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dcb5d43429
commit
bd269d70a5
32 files changed
+2285
-25
No files matched your search
@@ -0,0 +1,104 @@
|
||||
import Link from "next/link";
|
||||
import { EmptyState, PageBody, PageHeader } from "@/components/page";
|
||||
import { FamilyTree } from "@/components/family-tree";
|
||||
import { getT } from "@/i18n/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { familyComponent, layoutFamily, parseBond, type FamilyBondInput, type FamilyPerson } from "@/lib/family";
|
||||
import { requireBook } from "@/lib/session";
|
||||
import { storyYear } from "@/lib/story-date";
|
||||
|
||||
export const metadata = { title: "Släktträd" };
|
||||
|
||||
export default async function FamilyPage({
|
||||
params,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<{ bookId: string }>;
|
||||
searchParams: Promise<{ root?: string }>;
|
||||
}) {
|
||||
const { bookId } = await params;
|
||||
const { root } = await searchParams;
|
||||
await requireBook(bookId);
|
||||
const { t } = await getT();
|
||||
|
||||
const relations = await prisma.relation.findMany({
|
||||
where: { bookId, bond: { not: null } },
|
||||
include: {
|
||||
from: { select: { id: true, name: true, data: true } },
|
||||
to: { select: { id: true, name: true, data: true } },
|
||||
members: { include: { entity: { select: { id: true, name: true, data: true } } } },
|
||||
},
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
|
||||
const year = (data: unknown, key: string) => {
|
||||
const value = (data as Record<string, unknown> | null)?.[key];
|
||||
return typeof value === "string" ? storyYear(value) : null;
|
||||
};
|
||||
const everyone = new Map<string, FamilyPerson>();
|
||||
const bonds: FamilyBondInput[] = [];
|
||||
for (const r of relations) {
|
||||
const bond = parseBond(r.bond);
|
||||
if (!bond) continue;
|
||||
for (const e of [r.from, r.to, ...r.members.map((m) => m.entity)]) {
|
||||
if (everyone.has(e.id)) continue;
|
||||
const born = year(e.data, "birthDate");
|
||||
const died = year(e.data, "deathDate");
|
||||
const subtitle = born && died ? `${born}–${died}` : born ? t("family.born", { year: born }) : died ? t("family.died", { year: died }) : null;
|
||||
everyone.set(e.id, { id: e.id, name: e.name, subtitle });
|
||||
}
|
||||
bonds.push({ bond, fromId: r.fromId, toId: r.toId, memberIds: r.members.map((m) => m.entityId) });
|
||||
}
|
||||
|
||||
const options = [...everyone.values()].sort((a, b) => a.name.localeCompare(b.name, "sv"));
|
||||
const focus = root && everyone.has(root) ? familyComponent(bonds, root) : null;
|
||||
const shownBonds = focus ? bonds.filter((b) => focus.has(b.fromId)) : bonds;
|
||||
const layout = layoutFamily(focus ? options.filter((p) => focus.has(p.id)) : options, shownBonds);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader
|
||||
title={t("family.title")}
|
||||
actions={
|
||||
<Link href={`/books/${bookId}/relations`} className="btn btn-primary">
|
||||
{t("family.addBond")}
|
||||
</Link>
|
||||
}
|
||||
/>
|
||||
<PageBody>
|
||||
{options.length === 0 ? (
|
||||
<div className="card">
|
||||
<div className="card-body">
|
||||
<EmptyState>{t("family.empty")}</EmptyState>
|
||||
<p className="text-secondary text-center mb-0">{t("family.help")}</p>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("family.people", { count: layout.nodes.length })}</h3>
|
||||
<div className="card-actions">
|
||||
<form className="d-flex gap-2">
|
||||
<select name="root" className="form-select form-select-sm" defaultValue={root ?? ""} aria-label={t("family.around")}>
|
||||
<option value="">{t("family.everyone")}</option>
|
||||
{options.map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button className="btn btn-sm" type="submit">
|
||||
{t("timeline.show")}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<FamilyTree bookId={bookId} layout={layout} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</PageBody>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user