import { useState } from "react"; import { useTranslation } from "react-i18next"; import { fileTypeLabelKey } from "~/utils/files/fileTypeLabel"; import type { ProjectSignaturePanelVM, ProjectSubAreasScreenVM } from "@hivegrid/view-models"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { ButtonGroup } from "~/ui/components/ButtonGroup"; import { Card } from "~/ui/components/Card"; import { EmptyState } from "~/ui/components/EmptyState"; import { Select } from "~/ui/components/Select"; import { Table } from "~/ui/components/Table"; import { Tabs } from "~/ui/components/Tabs"; import { TextInput } from "~/ui/components/TextInput"; import { APP_TIME_ZONE } from "~/locale/format"; import { ProjectSignaturePanel } from "~/components/screens/signature/ProjectSignaturePanel"; import { AppLink } from "~/components/app/AppLink"; export type ProjectOperationSubmit = (intent: string, fields?: Record) => void; export type ProjectSubAreasPanelProps = { vm: ProjectSubAreasScreenVM; signature: ProjectSignaturePanelVM; submitting: boolean; errorKey?: string | null; /** User picker options for the member add form (route supplies the tenant directory). */ userOptions?: readonly { id: string; label: string }[]; actions: { onSubmit: ProjectOperationSubmit; onOpenChat: (href: string) => void; /** Chunked PDF upload for the signature wizard (injected by the route). */ uploadPdf?: (file: File) => Promise<{ id: string; displayName: string }>; }; }; type SubAreaTab = "members" | "appointments" | "invoices" | "checklists" | "tasks" | "materials" | "documents"; const DATETIME_INPUT_TYPE = "datetime-local" as unknown as "text"; const DATE_INPUT_TYPE = "date" as unknown as "text"; const mutedText = "text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary"; const headingText = "text-body-large-0 font-body-large-0 text-text-neutral-primary"; function formatDateTime(value: string | null, language: string): string { if (!value) return "—"; return new Intl.DateTimeFormat(language, { timeZone: APP_TIME_ZONE, dateStyle: "medium", timeStyle: "short" }).format(new Date(value)); } function formatDate(value: string | null, language: string): string { if (!value) return "—"; return new Intl.DateTimeFormat(language, { timeZone: APP_TIME_ZONE, dateStyle: "medium" }).format(new Date(value)); } function submitFields(submit: ProjectOperationSubmit, intent: string, fields: Record) { submit(intent, fields); } export function ProjectSubAreasPanel(props: ProjectSubAreasPanelProps) { const { t, i18n } = useTranslation(); const submit = props.actions.onSubmit; const [member, setMember] = useState({ principalType: "user", userId: "", groupId: "", rolle: "monteur" }); const [taskDraft, setTaskDraft] = useState(""); const [appointment, setAppointment] = useState({ titel: "", startAt: "", endAt: "", assignedToUserId: "" }); const [invoice, setInvoice] = useState({ betrag: "", datum: "", faelligAm: "", dateiId: "" }); const [checklist, setChecklist] = useState({ titel: "", typ: "generisch" }); const [itemDrafts, setItemDrafts] = useState>({}); const [invoiceStatuses, setInvoiceStatuses] = useState>({}); const [activeTab, setActiveTab] = useState("members"); if (props.vm.status !== "ready") return null; const data = props.vm.data; return (
{t("projects.operations.title")} {data.projectNumber} · {data.projectName}
{t("projects.actions.openAcceptance")}
{props.errorKey ? {t(props.errorKey)} : null} {data.requiresAcceptanceProtocol ? {t("projects.operations.acceptanceRequired")} : null} {data.openDefects > 0 ? {t("projects.subAreas.openDefects", { count: data.openDefects })} : null} {data.chat ? ( {t("projects.chat.title")} {t(data.chat.isArchived ? "projects.chat.archived" : data.chat.canWrite ? "projects.chat.active" : "projects.chat.readOnly")} ) : null} setActiveTab("members")} role="tab" aria-selected={activeTab === "members"}> {t("projects.subAreas.members")} setActiveTab("appointments")} role="tab" aria-selected={activeTab === "appointments"} > {t("projects.subAreas.appointments")} setActiveTab("invoices")} role="tab" aria-selected={activeTab === "invoices"}> {t("projects.subAreas.invoices")} setActiveTab("checklists")} role="tab" aria-selected={activeTab === "checklists"} > {t("projects.subAreas.checklists")} setActiveTab("tasks")} role="tab" aria-selected={activeTab === "tasks"}> {t("projects.subAreas.tasks")} setActiveTab("materials")} role="tab" aria-selected={activeTab === "materials"}> {t("projects.subAreas.materials")} setActiveTab("documents")} role="tab" aria-selected={activeTab === "documents"}> {t("projects.subAreas.documents")} {activeTab === "members" ? (
{t("projects.subAreas.members")} {data.members.length === 0 ? ( ) : ( {t("projects.fields.userId")} {t("projects.fields.role")} {t("projects.fields.actions")} {data.members.map((row) => ( {row.principal === "projects.fields.none" ? t(row.principal) : row.principal} {t(row.roleLabelKey)} ))}
)}
{t("projects.actions.addMember")} {member.principalType === "group" ? ( data.groupOptions.length > 0 ? ( ) : ( {t("projects.empty.groups.body")} ) ) : props.userOptions && props.userOptions.length > 0 ? ( ) : ( setMember((current) => ({ ...current, userId: event.target.value }))} /> )}
) : null} {activeTab === "appointments" ? (
{t("projects.subAreas.appointments")} {data.appointments.length === 0 ? ( ) : ( {t("projects.fields.appointmentTitle")} {t("projects.fields.owner")} {t("projects.fields.startDate")} {t("projects.fields.stage")} {data.appointments.map((row) => ( {row.title} {row.assignee === "projects.fields.none" ? t(row.assignee) : row.assignee} {formatDateTime(row.startAtIso, i18n.language)} {t(row.statusLabelKey)} ))}
)}
{t("projects.actions.createAppointment")} setAppointment((current) => ({ ...current, titel: event.target.value }))} /> setAppointment((current) => ({ ...current, assignedToUserId: event.target.value }))} /> setAppointment((current) => ({ ...current, startAt: event.target.value }))} /> setAppointment((current) => ({ ...current, endAt: event.target.value }))} />
) : null} {activeTab === "invoices" ? (
{t("projects.subAreas.invoices")} {data.invoices.length === 0 ? ( ) : ( {t("projects.fields.number")} {t("projects.fields.amount")} {t("projects.fields.stage")} {t("projects.fields.deadline")} {t("projects.actions.updateStatus")} {data.invoices.map((row) => ( {row.number} {row.amount} {t(row.statusLabelKey)} {formatDate(row.dueAtIso, i18n.language)}
))}
)}
{t("projects.actions.createInvoice")} setInvoice((current) => ({ ...current, betrag: event.target.value }))} /> setInvoice((current) => ({ ...current, datum: event.target.value }))} /> setInvoice((current) => ({ ...current, faelligAm: event.target.value }))} /> setInvoice((current) => ({ ...current, dateiId: event.target.value }))} />
) : null} {activeTab === "checklists" ? (
{t("projects.subAreas.checklists")} {data.checklists.length === 0 ? ( ) : (
{data.checklists.map((row) => (
{row.title} {row.itemCount} · {row.openItemCount} {t("projects.checklists.open")}
{t(row.typeLabelKey)}
{row.items.length === 0 ? {t("projects.empty.checklistItems.body")} : null} {row.items.map((item) => (
{item.title} {t(item.statusLabelKey)}
))}
setItemDrafts((current) => ({ ...current, [row.id]: event.target.value }))} />
))}
)}
{t("projects.actions.createChecklist")} setChecklist((current) => ({ ...current, titel: event.target.value }))} />
) : null} {activeTab === "tasks" ? (
{t("projects.subAreas.tasks")} {data.tasks.length === 0 ? ( ) : ( {t("projects.fields.taskTitle")} {t("projects.fields.stage")} {t("shared.priority")} {t("projects.fields.deadline")} {t("projects.fields.owner")} {data.tasks.map((task) => ( {task.title} {t(task.statusLabelKey)} {t(task.priorityLabelKey)} {formatDate(task.deadlineIso, i18n.language)} {task.assignee === "projects.fields.none" ? t(task.assignee) : task.assignee} ))}
)}
{t("projects.actions.createTask")} setTaskDraft(event.target.value)} />
) : null} {activeTab === "materials" ? ( {t("projects.subAreas.materials")} {data.materials.length === 0 ? ( ) : ( {t("projects.fields.position")} {t("projects.fields.description")} {t("projects.fields.quantity")} {t("projects.fields.unit")} {t("projects.fields.unitPrice")} {t("projects.fields.totalPrice")} {t("projects.fields.updatedAt")} {data.materials.map((position) => ( {position.positionNumber} {position.text} {position.longText ? {position.longText} : null} {position.quantity} {position.unit} {position.unitPrice} {position.totalPrice} {formatDate(position.lastSyncedAtIso, i18n.language)} ))}
)}
) : null} {activeTab === "documents" ? ( {t("projects.subAreas.documents")} {data.documents.length === 0 ? ( ) : ( {t("projects.fields.fileName")} {t("projects.fields.fileType")} {t("projects.fields.fileSize")} {t("projects.fields.startDate")} {data.documents.map((document) => ( {document.name} {t(fileTypeLabelKey(document.mimeType))} {document.sizeLabel} {formatDate(document.createdAtIso, i18n.language)} ))}
)}
) : null}
); }