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>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -6,6 +6,7 @@ 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 { RelationBondFields } from "@/components/relation-bond-fields";
|
||||
import { PageBody, PageHeader } from "@/components/page";
|
||||
import { getT } from "@/i18n/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
@@ -102,7 +103,7 @@ export default async function RelationPage({
|
||||
<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} />
|
||||
<input name="type" className="form-control" list="relation-types" maxLength={100} defaultValue={relation.type} />
|
||||
<datalist id="relation-types">
|
||||
{suggestions.map((s) => (
|
||||
<option key={s} value={s} />
|
||||
@@ -124,6 +125,7 @@ export default async function RelationPage({
|
||||
/>
|
||||
<div className="form-hint">{t("relation.membersHelp")}</div>
|
||||
</div>
|
||||
<RelationBondFields t={t} bond={relation.bond} startDate={relation.startDate} endDate={relation.endDate} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -107,6 +107,11 @@ export default async function RelationsPage({
|
||||
{r.type}
|
||||
</Link>
|
||||
{r.code ? <span className="badge ms-2">{r.code}</span> : null}
|
||||
{r.startDate || r.endDate ? (
|
||||
<span className="text-secondary small ms-2">
|
||||
{r.startDate ?? ""}–{r.endDate ?? ""}
|
||||
</span>
|
||||
) : null}
|
||||
</td>
|
||||
<td>
|
||||
{[r.to, ...r.members.map((m) => m.entity)].map((e, i) => (
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { IconTrash } from "@tabler/icons-react";
|
||||
import { deleteEvent, saveEvent } from "@/actions/events";
|
||||
import { ConfirmSubmit } from "@/components/confirm-submit";
|
||||
import { EntitySelect } from "@/components/entity-select";
|
||||
import { PageBody, PageHeader } from "@/components/page";
|
||||
import { StoryDateInput } from "@/components/story-date-input";
|
||||
import { getT } from "@/i18n/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { requireBook } from "@/lib/session";
|
||||
|
||||
export default async function EventPage({
|
||||
params,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<{ bookId: string; eventId: string }>;
|
||||
searchParams: Promise<{ saved?: string; error?: string }>;
|
||||
}) {
|
||||
const { bookId, eventId } = await params;
|
||||
const { saved, error } = await searchParams;
|
||||
await requireBook(bookId);
|
||||
const { t } = await getT();
|
||||
|
||||
const event = await prisma.storyEvent.findFirst({ where: { id: eventId, bookId }, include: { participants: true } });
|
||||
if (!event) notFound();
|
||||
const entities = await prisma.entity.findMany({ where: { bookId }, select: { id: true, name: true, kind: true }, orderBy: { name: "asc" } });
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader
|
||||
pretitle={
|
||||
<Link href={`/books/${bookId}/timeline`} className="text-reset">
|
||||
{t("timeline.title")}
|
||||
</Link>
|
||||
}
|
||||
title={event.title}
|
||||
actions={
|
||||
<form action={deleteEvent}>
|
||||
<input type="hidden" name="bookId" value={bookId} />
|
||||
<input type="hidden" name="id" value={event.id} />
|
||||
<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}
|
||||
{error === "date" ? <div className="alert alert-danger">{t("timeline.invalidDate")}</div> : null}
|
||||
<form action={saveEvent}>
|
||||
<input type="hidden" name="bookId" value={bookId} />
|
||||
<input type="hidden" name="id" value={event.id} />
|
||||
<div className="card mb-3">
|
||||
<div className="card-body">
|
||||
<div className="row g-3">
|
||||
<div className="col-12">
|
||||
<label className="form-label required">{t("timeline.eventTitle")}</label>
|
||||
<input name="title" className="form-control" required maxLength={200} defaultValue={event.title} />
|
||||
</div>
|
||||
<div className="col-md-6">
|
||||
<label className="form-label required">{t("timeline.date")}</label>
|
||||
<StoryDateInput name="date" required defaultValue={event.date} />
|
||||
</div>
|
||||
<div className="col-md-6">
|
||||
<label className="form-label">{t("timeline.endDate")}</label>
|
||||
<StoryDateInput name="endDate" defaultValue={event.endDate} />
|
||||
</div>
|
||||
<div className="col-12 form-hint mt-0">{t("timeline.dateHelp")}</div>
|
||||
<div className="col-12">
|
||||
<label className="form-label">{t("common.description")}</label>
|
||||
<textarea name="description" className="form-control" rows={4} maxLength={5000} defaultValue={event.description ?? ""} />
|
||||
</div>
|
||||
<div className="col-12">
|
||||
<label className="form-label">{t("timeline.participants")}</label>
|
||||
<EntitySelect name="participantIds" entities={entities} t={t} multiple defaultValues={event.participants.map((p) => p.entityId)} />
|
||||
<div className="form-hint">{t("relation.membersHelp")}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="d-flex justify-content-end gap-2">
|
||||
<Link href={`/books/${bookId}/timeline`} className="btn">
|
||||
{t("common.back")}
|
||||
</Link>
|
||||
<button type="submit" className="btn btn-primary">
|
||||
{t("common.save")}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</PageBody>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
import Link from "next/link";
|
||||
import { addEvent } from "@/actions/events";
|
||||
import { EntitySelect } from "@/components/entity-select";
|
||||
import { EmptyState, PageBody, PageHeader } from "@/components/page";
|
||||
import { StoryDateInput } from "@/components/story-date-input";
|
||||
import { getT } from "@/i18n/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { requireBook } from "@/lib/session";
|
||||
import { storyYear } from "@/lib/story-date";
|
||||
import { loadTimeline, TIMELINE_KINDS, type TimelineItem, type TimelineKind } from "@/lib/timeline";
|
||||
|
||||
export const metadata = { title: "Tidslinje" };
|
||||
|
||||
const KIND_COLOR: Record<TimelineKind, string> = {
|
||||
birth: "green",
|
||||
death: "dark",
|
||||
scenario: "pink",
|
||||
event: "blue",
|
||||
relation: "azure",
|
||||
};
|
||||
|
||||
export default async function TimelinePage({
|
||||
params,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<{ bookId: string }>;
|
||||
searchParams: Promise<{ person?: string; kind?: string | string[]; error?: string }>;
|
||||
}) {
|
||||
const { bookId } = await params;
|
||||
const { person, kind, error } = await searchParams;
|
||||
await requireBook(bookId);
|
||||
const { t } = await getT();
|
||||
|
||||
const wanted = (Array.isArray(kind) ? kind : kind ? [kind] : []).filter((k): k is TimelineKind => (TIMELINE_KINDS as string[]).includes(k));
|
||||
const kinds = new Set<TimelineKind>(wanted.length > 0 ? wanted : TIMELINE_KINDS);
|
||||
|
||||
const [entities, timeline] = await Promise.all([
|
||||
prisma.entity.findMany({ where: { bookId }, select: { id: true, name: true, kind: true }, orderBy: { name: "asc" } }),
|
||||
loadTimeline(bookId, kinds, person || undefined),
|
||||
]);
|
||||
const people = entities.filter((e) => e.kind === "CHARACTER" || e.kind === "PET");
|
||||
|
||||
const byYear = new Map<number, TimelineItem[]>();
|
||||
for (const item of timeline.items) {
|
||||
const year = storyYear(item.date)!;
|
||||
byYear.set(year, [...(byYear.get(year) ?? []), item]);
|
||||
}
|
||||
|
||||
const title = (item: TimelineItem) => {
|
||||
const who = item.people.map((p) => p.name).join(", ");
|
||||
switch (item.kind) {
|
||||
case "birth":
|
||||
return t("timeline.born", { name: item.title });
|
||||
case "death":
|
||||
return t("timeline.died", { name: item.title });
|
||||
case "relation":
|
||||
return item.endOnly ? t("timeline.relationEnds", { type: `${who} · ${item.title}` }) : `${who ? `${who} · ` : ""}${item.title}`;
|
||||
default:
|
||||
return item.title;
|
||||
}
|
||||
};
|
||||
const when = (item: TimelineItem) => (item.endDate ? `${item.date} – ${item.endDate}` : item.date);
|
||||
|
||||
// Perioder (händelser och relationer med slutdatum) som staplar på en gemensam årsaxel
|
||||
const periods = timeline.items.filter((i) => i.endDate);
|
||||
const spanStart = Math.min(...periods.map((i) => storyYear(i.date)!));
|
||||
const spanEnd = Math.max(...periods.map((i) => storyYear(i.endDate!)!));
|
||||
const span = Math.max(spanEnd - spanStart + 1, 1);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader title={t("timeline.title")} />
|
||||
<PageBody>
|
||||
{error === "date" ? <div className="alert alert-danger">{t("timeline.invalidDate")}</div> : null}
|
||||
<div className="row row-cards">
|
||||
<div className="col-lg-8">
|
||||
<div className="card mb-3">
|
||||
<div className="card-body">
|
||||
<form className="row g-2 align-items-end">
|
||||
<div className="col-md-4">
|
||||
<label className="form-label">{t("timeline.person")}</label>
|
||||
<EntitySelect name="person" entities={people} t={t} defaultValue={person} emptyLabel={t("timeline.everyone")} />
|
||||
</div>
|
||||
<div className="col-md-8">
|
||||
<div className="d-flex flex-wrap gap-3 align-items-center">
|
||||
{TIMELINE_KINDS.map((k) => (
|
||||
<label key={k} className="form-check mb-0">
|
||||
<input type="checkbox" className="form-check-input" name="kind" value={k} defaultChecked={kinds.has(k)} />
|
||||
<span className="form-check-label">{t(`timeline.kind.${k}`)}</span>
|
||||
</label>
|
||||
))}
|
||||
<button className="btn btn-sm ms-auto" type="submit">
|
||||
{t("timeline.show")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="card mb-3">
|
||||
{timeline.items.length === 0 ? (
|
||||
<div className="card-body">
|
||||
<EmptyState>{t("timeline.empty")}</EmptyState>
|
||||
<p className="text-secondary text-center mb-0">{t("timeline.help")}</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="list-group list-group-flush">
|
||||
{[...byYear.entries()].map(([year, items]) => (
|
||||
<div key={year} className="list-group-item">
|
||||
<div className="row">
|
||||
<div className="col-auto" style={{ width: "5rem" }}>
|
||||
<div className="h2 mb-0">{year}</div>
|
||||
</div>
|
||||
<div className="col">
|
||||
{items.map((item) => (
|
||||
<div key={item.id} className="d-flex align-items-start gap-2 py-1">
|
||||
<span className={`badge bg-${KIND_COLOR[item.kind]}-lt mt-1`} style={{ minWidth: "5.5rem" }}>
|
||||
{t(`timeline.kind.${item.kind}`)}
|
||||
</span>
|
||||
<div>
|
||||
<Link href={`/books/${bookId}/${item.link}`} className="fw-bold">
|
||||
{title(item)}
|
||||
</Link>
|
||||
<span className="text-secondary ms-2">{when(item)}</span>
|
||||
{item.detail ? <div className="text-secondary small">{item.detail}</div> : null}
|
||||
{item.ages.length > 0 ? (
|
||||
<div className="text-secondary small">{item.ages.map((a) => t("timeline.age", { name: a.name, age: a.age })).join(" · ")}</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{periods.length > 0 ? (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("timeline.periods")}</h3>
|
||||
<div className="card-actions text-secondary small">
|
||||
{spanStart} – {spanEnd}
|
||||
</div>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
{periods.map((item) => {
|
||||
const from = storyYear(item.date)!;
|
||||
const to = storyYear(item.endDate!)!;
|
||||
return (
|
||||
<div key={item.id} className="mb-2">
|
||||
<div className="small">
|
||||
<Link href={`/books/${bookId}/${item.link}`}>{title(item)}</Link> <span className="text-secondary">{when(item)}</span>
|
||||
</div>
|
||||
<div className="position-relative bg-body-secondary rounded" style={{ height: "0.75rem" }}>
|
||||
<div
|
||||
className={`position-absolute top-0 bottom-0 rounded bg-${KIND_COLOR[item.kind]}`}
|
||||
style={{ left: `${((from - spanStart) / span) * 100}%`, width: `${Math.max(((to - from + 1) / span) * 100, 1)}%` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{timeline.undated.length > 0 ? (
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("timeline.undated")}</h3>
|
||||
</div>
|
||||
<div className="list-group list-group-flush">
|
||||
{timeline.undated.map((u) => (
|
||||
<Link key={u.id} href={`/books/${bookId}/${u.link}`} className="list-group-item list-group-item-action">
|
||||
{u.title}
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="col-lg-4">
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("timeline.addEvent")}</h3>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<form action={addEvent} className="row g-2">
|
||||
<input type="hidden" name="bookId" value={bookId} />
|
||||
<div className="col-12">
|
||||
<label className="form-label required">{t("timeline.eventTitle")}</label>
|
||||
<input name="title" className="form-control" required maxLength={200} />
|
||||
</div>
|
||||
<div className="col-12">
|
||||
<label className="form-label required">{t("timeline.date")}</label>
|
||||
<StoryDateInput name="date" required />
|
||||
</div>
|
||||
<div className="col-12">
|
||||
<label className="form-label">{t("timeline.endDate")}</label>
|
||||
<StoryDateInput name="endDate" />
|
||||
</div>
|
||||
<div className="col-12 form-hint mt-0">{t("timeline.dateHelp")}</div>
|
||||
<div className="col-12">
|
||||
<label className="form-label">{t("common.description")}</label>
|
||||
<textarea name="description" className="form-control" rows={3} maxLength={5000} />
|
||||
</div>
|
||||
<div className="col-12">
|
||||
<label className="form-label">{t("timeline.participants")}</label>
|
||||
<EntitySelect name="participantIds" entities={entities} t={t} multiple />
|
||||
<div className="form-hint">{t("relation.membersHelp")}</div>
|
||||
</div>
|
||||
<div className="col-12 text-end">
|
||||
<button type="submit" className="btn btn-primary">
|
||||
{t("timeline.addEvent")}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</PageBody>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user