first commit

This commit is contained in:
bobban committed 2026-10-22 21:00:25 +02:00
commit b7b0f253ce
85 files changed
+7560

No files matched your search

+397
View File
@@ -0,0 +1,397 @@
import Link from "next/link";
import { IconX } from "@tabler/icons-react";
import { addShifts, deleteShift } from "@/actions/shifts";
import { EntitySelect } from "@/components/entity-select";
import { EmptyState, PageBody, PageHeader } from "@/components/page";
import type { TFunction } from "@/i18n/core";
import { getT } from "@/i18n/server";
import { prisma } from "@/lib/db";
import { WORKPLACE_KINDS } from "@/lib/kinds";
import { requireBook } from "@/lib/session";
export const metadata = { title: "Scheman" };
const DAYS = [0, 1, 2, 3, 4, 5, 6];
type ShiftRow = {
id: string;
weekday: number;
startTime: string;
endTime: string;
weeks: "ALL" | "ODD" | "EVEN";
label: string | null;
person: { id: string; name: string };
workplace: { id: string; name: string } | null;
};
function ShiftChip({
shift,
bookId,
t,
showPerson,
}: {
shift: ShiftRow;
bookId: string;
t: TFunction;
showPerson?: boolean;
}) {
return (
<div className="d-flex align-items-start gap-1 border rounded px-2 py-1 mb-1">
<div className="flex-fill min-w-0 small">
<div className="fw-bold">
{showPerson ? (
<Link href={`/books/${bookId}/entity/${shift.person.id}`} className="text-reset">
{shift.person.name}
</Link>
) : (
<>
{shift.startTime}–{shift.endTime}
</>
)}
</div>
<div className="text-secondary">
{showPerson ? `${shift.startTime}–${shift.endTime}` : null}
{[
shift.workplace?.name,
shift.label,
shift.weeks !== "ALL" ? t(`schedule.weeks.${shift.weeks}`) : null,
]
.filter(Boolean)
.map((part, i) => (
<span key={i}>
{showPerson || i > 0 ? " · " : ""}
{part}
</span>
))}
</div>
</div>
<form action={deleteShift}>
<input type="hidden" name="bookId" value={bookId} />
<input type="hidden" name="id" value={shift.id} />
<button type="submit" className="btn btn-icon btn-sm btn-ghost-secondary" title={t("common.delete")} aria-label={t("common.delete")}>
<IconX size={14} />
</button>
</form>
</div>
);
}
export default async function SchedulesPage({
params,
searchParams,
}: {
params: Promise<{ bookId: string }>;
searchParams: Promise<{ view?: string; range?: string; person?: string; workplace?: string }>;
}) {
const { bookId } = await params;
const { view = "person", range: rangeParam, person, workplace } = await searchParams;
const range = rangeParam === "night" ? "night" : "day";
await requireBook(bookId);
const { t } = await getT();
const [people, workplaces, shifts] = await Promise.all([
prisma.entity.findMany({
where: { bookId, kind: { in: ["CHARACTER"] } },
select: { id: true, name: true, kind: true },
orderBy: { name: "asc" },
}),
prisma.entity.findMany({
where: { bookId, kind: { in: WORKPLACE_KINDS } },
select: { id: true, name: true, kind: true },
orderBy: { name: "asc" },
}),
prisma.shift.findMany({
where: {
bookId,
...(person ? { personId: person } : {}),
...(workplace ? { workplaceId: workplace } : {}),
},
include: { person: { select: { id: true, name: true } }, workplace: { select: { id: true, name: true } } },
orderBy: [{ startTime: "asc" }],
}),
]);
const byDay = view === "day";
const byGrid = view === "grid";
const queryFor = (v: string, r: string = range) => {
const q = new URLSearchParams({ view: v });
if (v === "grid") q.set("range", r);
if (person) q.set("person", person);
if (workplace) q.set("workplace", workplace);
return `?${q}`;
};
// Timrutnät som i dag- och nattscheman: dag 06–18, natt 18–06. Natten hör till dagen den började.
const gridHours = range === "day" ? [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] : [18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5];
const toHours = (time: string) => {
const [h, m] = time.split(":").map(Number);
return h + m / 60;
};
const covers = (s: (typeof shifts)[number], column: number, hour: number) => {
const slot = range === "night" && hour < 6 ? (column + 1) * 24 + hour : column * 24 + hour;
const start = s.weekday * 24 + toHours(s.startTime);
let end = s.weekday * 24 + toHours(s.endTime);
if (end <= start) end += 24;
return [slot - 168, slot, slot + 168].some((a) => start < a + 1 && end > a);
};
// Rader per person, i kolumnordning efter namn
const personRows = people
.map((p) => ({ person: p, shifts: shifts.filter((s) => s.personId === p.id) }))
.filter((row) => row.shifts.length > 0);
return (
<>
<PageHeader
title={t("schedule.title")}
actions={
<>
{byGrid ? (
<div className="btn-group">
<Link href={queryFor("grid", "day")} className={`btn ${range === "day" ? "active" : ""}`}>
{t("schedule.rangeDay")}
</Link>
<Link href={queryFor("grid", "night")} className={`btn ${range === "night" ? "active" : ""}`}>
{t("schedule.rangeNight")}
</Link>
</div>
) : null}
<div className="btn-group">
<Link href={queryFor("person")} className={`btn ${!byDay && !byGrid ? "active" : ""}`}>
{t("schedule.viewPerson")}
</Link>
<Link href={queryFor("day")} className={`btn ${byDay ? "active" : ""}`}>
{t("schedule.viewDay")}
</Link>
<Link href={queryFor("grid")} className={`btn ${byGrid ? "active" : ""}`}>
{t("schedule.viewGrid")}
</Link>
</div>
</>
}
/>
<PageBody>
<div className="row row-cards">
<div className="col-12">
<div className="card">
<div className="card-body py-3">
<form className="row g-2 align-items-center">
<input type="hidden" name="view" value={view} />
{byGrid ? <input type="hidden" name="range" value={range} /> : null}
<div className="col-md-5">
<EntitySelect
name="person"
entities={people}
t={t}
defaultValue={person}
emptyLabel={t("schedule.filterPerson")}
className="form-select form-select-sm"
/>
</div>
<div className="col-md-5">
<EntitySelect
name="workplace"
entities={workplaces}
t={t}
defaultValue={workplace}
emptyLabel={t("schedule.filterWorkplace")}
className="form-select form-select-sm"
/>
</div>
<div className="col-md-2 d-flex gap-2">
<button className="btn btn-sm" type="submit">
{t("common.search")}
</button>
{person || workplace ? (
<Link href={`?view=${view}`} className="btn btn-sm btn-ghost-secondary">
{t("common.clear")}
</Link>
) : null}
</div>
</form>
</div>
</div>
</div>
<div className="col-12">
<div className="card">
{shifts.length === 0 ? (
<div className="card-body">
<EmptyState>{t("schedule.empty")}</EmptyState>
</div>
) : byGrid ? (
<div className="table-responsive">
<table className="table table-vcenter table-bordered card-table">
<thead>
<tr>
<th className="w-1" />
{DAYS.map((d) => (
<th key={d} className="bm-week-cell">
{t(`day.short.${d}`)}
</th>
))}
</tr>
</thead>
<tbody>
{gridHours.map((hour) => (
<tr key={hour}>
<td className="text-secondary text-nowrap">{String(hour).padStart(2, "0")}:00</td>
{DAYS.map((d) => {
const here = shifts.filter((s) => covers(s, d, hour));
return (
<td key={d} className="bm-week-cell py-1">
{here.map((s) => (
<Link
key={s.id}
href={`/books/${bookId}/entity/${s.person.id}`}
className="badge bg-blue-lt me-1 mb-1 text-decoration-none"
title={[s.workplace?.name, `${s.startTime}–${s.endTime}`, s.label].filter(Boolean).join(" · ")}
>
{s.person.name}
</Link>
))}
</td>
);
})}
</tr>
))}
</tbody>
</table>
</div>
) : byDay ? (
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
<tr>
{DAYS.map((d) => (
<th key={d} className="bm-week-cell">{t(`day.${d}`)}</th>
))}
</tr>
</thead>
<tbody>
<tr>
{DAYS.map((d) => (
<td key={d} className="bm-week-cell">
{shifts
.filter((s) => s.weekday === d)
.map((s) => (
<ShiftChip key={s.id} shift={s} bookId={bookId} t={t} showPerson />
))}
</td>
))}
</tr>
</tbody>
</table>
</div>
) : (
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
<tr>
<th />
{DAYS.map((d) => (
<th key={d} className="bm-week-cell">{t(`day.short.${d}`)}</th>
))}
</tr>
</thead>
<tbody>
{personRows.map(({ person: p, shifts: rowShifts }) => (
<tr key={p.id}>
<td className="fw-bold">
<Link href={`/books/${bookId}/entity/${p.id}`} className="text-reset">
{p.name}
</Link>
</td>
{DAYS.map((d) => (
<td key={d} className="bm-week-cell">
{rowShifts
.filter((s) => s.weekday === d)
.map((s) => (
<ShiftChip key={s.id} shift={s} bookId={bookId} t={t} />
))}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
<div className="col-lg-8">
<div className="card">
<div className="card-header">
<h3 className="card-title">{t("schedule.add")}</h3>
</div>
{people.length === 0 ? (
<div className="card-body text-secondary">{t("schedule.noPeople")}</div>
) : (
<form action={addShifts}>
<input type="hidden" name="bookId" value={bookId} />
<div className="card-body">
<div className="row g-3">
<div className="col-md-6">
<label className="form-label required">{t("schedule.person")}</label>
<EntitySelect name="personId" entities={people} t={t} required defaultValue={person} />
</div>
<div className="col-md-6">
<label className="form-label">{t("schedule.workplace")}</label>
<EntitySelect
name="workplaceId"
entities={workplaces}
t={t}
emptyLabel={t("schedule.noWorkplace")}
defaultValue={workplace}
/>
</div>
<div className="col-12">
<label className="form-label required">{t("schedule.days")}</label>
<div className="d-flex flex-wrap gap-3">
{DAYS.map((d) => (
<label className="form-check" key={d}>
<input type="checkbox" name="days" value={d} className="form-check-input" defaultChecked={d < 5} />
<span className="form-check-label">{t(`day.${d}`)}</span>
</label>
))}
</div>
</div>
<div className="col-6 col-md-3">
<label className="form-label required">{t("schedule.start")}</label>
<input type="time" name="startTime" className="form-control" required defaultValue="08:00" />
</div>
<div className="col-6 col-md-3">
<label className="form-label required">{t("schedule.end")}</label>
<input type="time" name="endTime" className="form-control" required defaultValue="16:00" />
</div>
<div className="col-md-3">
<label className="form-label">{t("schedule.weeks")}</label>
<select name="weeks" className="form-select" defaultValue="ALL">
{(["ALL", "ODD", "EVEN"] as const).map((w) => (
<option key={w} value={w}>
{t(`schedule.weeks.${w}`)}
</option>
))}
</select>
</div>
<div className="col-md-3">
<label className="form-label">{t("schedule.label")}</label>
<input name="label" className="form-control" maxLength={100} placeholder={t("schedule.labelHelp")} />
</div>
</div>
</div>
<div className="card-footer text-end">
<button type="submit" className="btn btn-primary">
{t("common.add")}
</button>
</div>
</form>
)}
</div>
</div>
</div>
</PageBody>
</>
);
}