import LaunchIcon from "@mui/icons-material/Launch"; import { Box, Button, Chip, Divider, Grid, Paper, Stack, Typography } from "@mui/material"; import { useQuery } from "@tanstack/react-query"; import { useTranslation } from "react-i18next"; import { useParams, useNavigate } from "react-router-dom"; import { fetchContract, fetchPaperlessDocument } from "../api/contracts"; import { fetchServerConfig, ServerConfig } from "../api/config"; import PageHeader from "../components/PageHeader"; import { formatCurrency, formatDate } from "../utils/date"; import { translateCategoryName } from "../utils/categories"; export default function ContractDetail() { const { contractId } = useParams<{ contractId: string }>(); const navigate = useNavigate(); const id = Number(contractId); const { t, i18n } = useTranslation(); const { data: contract, isLoading } = useQuery({ queryKey: ["contracts", id], queryFn: () => fetchContract(id), enabled: Number.isFinite(id) }); const { data: serverConfig } = useQuery({ queryKey: ["server-config"], queryFn: fetchServerConfig }); const { data: paperlessDoc, error: paperlessError } = useQuery({ queryKey: ["contracts", id, "paperless"], queryFn: () => fetchPaperlessDocument(id), enabled: Number.isFinite(id) }); const paperlessAppUrl = serverConfig?.paperlessExternalUrl ?? serverConfig?.paperlessBaseUrl ?? null; const terminationValue = contract?.terminationNoticeDays !== undefined && contract?.terminationNoticeDays !== null ? t("deadlineList.daysLabel", { count: contract.terminationNoticeDays }) : "–"; const renewalValue = contract?.renewalPeriodMonths ? `${t("contractDetail.monthsLabel", { count: contract.renewalPeriodMonths })}${contract.autoRenew ? `, ${t("contractForm.fields.autoRenew")}` : ""}` : contract?.autoRenew ? t("contractForm.fields.autoRenew") : "–"; const notesValue = contract?.notes ?? t("contractDetail.noNotes"); const categoryValue = contract?.category ? translateCategoryName(contract.category, i18n.language) || contract.category : "–"; if (!Number.isFinite(id)) { return {t("contractForm.loadError")}; } if (isLoading || !contract) { return {t("contractForm.loading")}; } return ( <> navigate(`/contracts/${contract.id}/edit`)}> {t("contractDetail.edit")} } /> {t("contractDetail.details")} {t("contractDetail.tags")} {(contract.tags ?? []).length > 0 ? ( contract.tags!.map((tag) => ) ) : ( {t("contractDetail.noTags")} )} {t("contractDetail.document")} {paperlessError ? ( {t("contractDetail.documentError", { error: (paperlessError as Error).message })} ) : paperlessDoc ? ( {String(paperlessDoc.title ?? t("contractDetail.documentFallback"))} {t("contractDetail.created")}: {paperlessDoc.created ? formatDate(String(paperlessDoc.created)) : "–"} {t("contractDetail.updated")}: {paperlessDoc.modified ? formatDate(String(paperlessDoc.modified)) : "–"} {paperlessDoc.notes && ( {String(paperlessDoc.notes)} )} {!paperlessAppUrl && ( {t("contractDetail.configurePaperless")} )} ) : ( {t("contractDetail.documentMissing")} )} {t("contractDetail.metadata")} ); } function Detail({ label, value }: { label: string; value: string }) { return ( {label} {value} ); }