Files
eu/app/acts/[id]/page.tsx
T
2026-07-28 17:05:45 +03:00

220 lines
9.5 KiB
TypeScript

import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import {
ArticleContent,
PreambleContent,
LEGAL_STATUS_LABELS,
} from "@/lib/acts/types";
import {
getActById,
getActTypeById,
getInstitutionById,
getActVersions,
getCurrentActVersion,
getActBlocks,
getActApprovals,
} from "@/lib/acts/repository";
import { getPositions, getCitizenshipPositions } from "@/lib/positions/repository";
import { getActiveCitizenship } from "@/lib/citizenship/repository";
import { getActFlow, getNextPhase } from "@/lib/acts/flows";
import { ApproveButton } from "./approve-button";
import { VetoButton } from "./veto-button";
import { getCurrentUser, getUsers } from "@/lib/user";
import { cn, formatDate } from "@/lib/utils";
import { Metadata } from "next";
import { Merriweather } from "next/font/google";
import Image from "next/image";
import Link from "next/link";
import { notFound } from "next/navigation";
const documentText = Merriweather({ weight: "400" });
const documentHeading = Merriweather({ weight: "700" });
export async function generateMetadata({ params }: { params: Promise<{ id: string }> }): Promise<Metadata> {
const { id } = await params;
const act = await getActById(id);
if (!act) return {};
const type = await getActTypeById(act.actTypeId);
return {
title: `${type?.name ?? ""}${act.title ? ` “${act.title}”` : ""}`,
};
}
export default async function Act({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const user = await getCurrentUser();
const act = await getActById(id);
if (!act) notFound();
const type = await getActTypeById(act.actTypeId);
const institution = await getInstitutionById(act.issuingInstitutionId);
const [author] = await getUsers(act.authorId);
const versions = await getActVersions(id);
const version = await getCurrentActVersion(id);
if (!version) notFound();
const blocks = await getActBlocks(version.id);
const needsRegion = institution?.kind === "REGION";
const preamble = blocks.find((b) => b.type === "PREAMBLE");
const preambleContent = preamble?.content as PreambleContent | undefined;
const approvals = await getActApprovals(version.id);
const positions = await getPositions();
const positionNames = new Map(positions.map((p) => [p.id, p.name]));
const flow = getActFlow(type?.code ?? "");
const phase = getNextPhase(flow, approvals);
const stageLabel = phase?.label ?? "Completed";
let canApprove = false;
let canVeto = false;
if (user && act.legalStatus === "DRAFT" && phase) {
const citizenship = await getActiveCitizenship(user.id);
if (citizenship) {
const citizenshipPositions = await getCitizenshipPositions(citizenship.id);
const holds = citizenshipPositions.some(
(p) => !p.resignedAt && p.position.capabilities.some((c) => c.capability === phase.capability)
);
canApprove = holds;
canVeto = holds && !!phase.allowVeto;
}
}
return (
<div className="flex flex-col p-4 md:p-16 gap-8 md:gap-16 mb-4 md:mb-0">
<div className="flex flex-col gap-8 md:flex-row justify-between">
<div className="flex flex-col gap-8">
<Card size="sm" className="h-max">
<CardHeader>
<CardTitle>
<h3>
{type?.name} {act.title && `“${act.title}”`}{" "}
{act.numberInSeries ? `№${act.numberInSeries}/${act.numberYear}` : <span className="text-muted-foreground">without a number</span>}
</h3>
</CardTitle>
</CardHeader>
<CardContent>
<div className="flex flex-col">
<p className="text-muted-foreground">Issuing institution</p>
<p>{institution?.name}</p>
</div>
<div className="flex flex-row gap-4 items-center">
<p className="text-muted-foreground">Author</p>
<p>{author?.firstName} {author?.secondName}</p>
</div>
<div className="flex flex-row gap-4 items-center">
<p className="text-muted-foreground">Created</p>
<p>{formatDate(act.createdAt)}</p>
</div>
<div className="flex flex-row gap-4 items-center">
<p className="text-muted-foreground">Version from</p>
<p>{formatDate(version.createdAt)}</p>
</div>
<div className="flex flex-row gap-4 items-center">
<p className="text-muted-foreground">Process stage</p>
<p>{stageLabel}</p>
</div>
<div className="flex flex-row gap-4 items-center">
<p className="text-muted-foreground">Legal status</p>
{act.legalStatus === "NOT_ADOPTED" ? <Badge variant="destructive">{LEGAL_STATUS_LABELS[act.legalStatus]}</Badge> : <p>{LEGAL_STATUS_LABELS[act.legalStatus]}</p>}
</div>
</CardContent>
</Card>
<Card size="sm" className="h-max">
<CardHeader>
<CardTitle><h3>History</h3></CardTitle>
</CardHeader>
<CardContent>
<div className="flex flex-col gap-4">
{versions.map((v) =>
v.id === version.id ? (
<div className="flex flex-col gap-1" key={v.id}>
<h4>Version from {formatDate(v.createdAt)}</h4>
<Badge>Current</Badge>
</div>
) : (
<Link href={`/acts/${act.id}/${v.id}`} className="flex flex-col gap-1" key={v.id}>
<h4 className="text-muted-foreground">Version from {formatDate(v.createdAt)}</h4>
<Badge variant="secondary">Outdated</Badge>
</Link>
)
)}
</div>
</CardContent>
</Card>
{ (canApprove || canVeto) && (
<Card size="sm" className="h-max">
<CardHeader>
<CardTitle><h3>Signature</h3></CardTitle>
<CardDescription><p>You are able to sign or reject this act</p></CardDescription>
</CardHeader>
<CardContent className="flex flex-row gap-3">
{canApprove && <ApproveButton actId={act.id} />}
{canVeto && <VetoButton actId={act.id} vetoLabel={phase?.vetoLabel || "Veto"} />}
</CardContent>
</Card>
)}
</div>
<div className="flex flex-1 flex-col gap-6">
<div className="flex flex-col items-center gap-6">
<Image src="/emblems/europe.png" width={52} height={32} alt="Emblem of the European Federation" />
<div className={cn("flex flex-col gap-1", documentHeading.className)}>
<h3 className="text-center">{!needsRegion ? "European Federation" : "Region"}</h3>
<h3 className="text-center">{institution?.name}</h3>
</div>
<h3 className={cn("text-center", documentHeading.className)}>
{type?.name} {act.numberInSeries && `№${act.numberInSeries}/${act.numberYear}`} {act.title && `“${act.title}”`}
</h3>
</div>
<div className="flex flex-col gap-4">
{preambleContent && (
<h4 className={cn("shrink-0", documentText.className)}>{preambleContent.text}</h4>
)}
{blocks
.filter((b) => b.type === "ARTICLE")
.map((block, index) => {
const article = block.content as ArticleContent;
return (
<div className="flex flex-col gap-2" key={block.stableKey}>
<div className="flex items-center gap-2">
<h4 className={cn("shrink-0", documentHeading.className)}>
Article {index + 1}{article.title && `. ${article.title}.`}
</h4>
</div>
{article.intro && <h4 className={documentText.className}>{article.intro}</h4>}
<div className="flex flex-col gap-2">
{article.clauses.map((clause, clauseIndex) => (
<h4 key={clause.id} className={documentText.className}>
{article.markerStyle === "NUMBERS" && `${clauseIndex + 1}. `}
{article.markerStyle === "LETTERS" && `${String.fromCharCode(97 + clauseIndex)}) `}
{clause.kind === "APPOINTMENT"
? `${clause.action === "ASSIGN" ? "Appoint" : "Dismiss"} ${clause.citizenshipName} ${clause.action === "ASSIGN" ? "to" : "from"} ${positionNames.get(clause.positionId) ?? "an unknown position"}.`
: clause.text}
</h4>
))}
</div>
</div>
);
})}
</div>
<div className="flex flex-col gap-4">
{approvals.map((a) => (
<div className="flex flex-col gap-1 border-4 border-primary w-max p-2" key={a.id}>
<h4 className={documentHeading.className}>{a.name}</h4>
<h4 className={documentText.className}>{a.role}</h4>
<p className={documentText.className}>
{a.decision === "APPROVE" ? "Approved" : "Vetoed"} at {formatDate(a.decidedAt)}
</p>
</div>
))}
</div>
</div>
</div>
</div>
);
}