import Link from "next/link"; import { Check, X } from "lucide-react"; import { requireSession } from "@/server/auth"; import { dbForTenant } from "@/server/db"; import { PageHead, Pill } from "@/components/mockup-ui"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { approveTask, rejectTask, commentTask } from "@/server/actions/tasks"; const STATUS_TONE: Record = { OPEN: "info", DONE: "ok", REJECTED: "warn", CANCELLED: "mut" }; const STATUS_LABEL: Record = { OPEN: "Offen", DONE: "Freigegeben", REJECTED: "Abgelehnt", CANCELLED: "Zurückgezogen" }; const KIND_LABEL: Record = { submit: "eingereicht", approve: "freigegeben", reject: "abgelehnt", comment: "Kommentar" }; interface TaskWithComments { id: string; type: string; title: string; status: string; entityType: string | null; entityRef: string | null; assigneeId: string | null; createdById: string | null; createdAt: Date; comments: { id: string; authorId: string | null; kind: string; body: string; createdAt: Date }[]; } function entityHref(t: TaskWithComments): string | null { return t.entityType === "policy_document" && t.entityRef ? `/policies/${t.entityRef}` : null; } function TaskCard({ task, me, name }: { task: TaskWithComments; me: string; name: (id: string | null) => string }) { const isAssignee = task.assigneeId === me; const isOpen = task.status === "OPEN"; const href = entityHref(task); return (

{href ? {task.title} : task.title}

Eingereicht von {name(task.createdById)} · Freigeber {name(task.assigneeId)} · {task.createdAt.toLocaleDateString("de-DE")}

{STATUS_LABEL[task.status] ?? task.status}
{task.comments.length > 0 && (
    {task.comments.map((c) => (
  • {name(c.authorId)} · {KIND_LABEL[c.kind] ?? c.kind}: {c.body}
  • ))}
)} {isOpen && isAssignee && (
)} {isOpen && !isAssignee && (

Wartet auf Freigabe durch {name(task.assigneeId)}.

)}
); } export default async function TasksPage() { const session = await requireSession(); const db = dbForTenant(session.user.tenantId); const me = session.user.id; const [tasks, users] = await Promise.all([ db.task.findMany({ where: { OR: [{ assigneeId: me }, { createdById: me }] }, include: { comments: { orderBy: { createdAt: "asc" } } }, orderBy: [{ createdAt: "desc" }], }), db.user.findMany({ select: { id: true, name: true } }), ]); const name = (id: string | null) => users.find((u) => u.id === id)?.name ?? "—"; const open = tasks.filter((t) => t.status === "OPEN"); const resolved = tasks.filter((t) => t.status !== "OPEN"); const myOpen = open.filter((t) => t.assigneeId === me); return (

Offen

{open.length === 0 &&

Keine offenen Aufgaben.

} {open.map((t) => )}
{resolved.length > 0 && (

Erledigt

{resolved.map((t) => )}
)}
); }