import { Button } from "@repo/shadcn-ui/components/button"; import { EntityTable } from "@repo/shadcn-ui/components/entity-list"; import { FormDialog } from "@repo/shadcn-ui/components/form-dialog"; import { entityListLabels } from "@repo/shadcn-ui/lib/entity-list"; import { useQuery } from "@tanstack/react-query"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import type { DashboardIssue } from "../../common/dashboard"; import type { DashboardNavigation } from "./dashboard-api"; import { dashboardApi, dashboardKeys, entryDestination } from "./dashboard-api"; import { BlockState } from "./dashboard-block"; export function DashboardDetailsDialog({ companyId, details, onClose, onNavigate, }: { companyId?: string | undefined; details: { kind?: "asset" | "resource"; issue?: DashboardIssue; work?: boolean; activity?: boolean; companies?: boolean; }; onClose: () => void; onNavigate: (destination: DashboardNavigation) => void; }) { const { t, i18n } = useTranslation("companies"); const [page, setPage] = useState(1); const block = details.work ? "work" : details.activity ? "activity" : "details"; const query = useQuery({ queryKey: dashboardKeys.block(block, companyId, page, details.issue ?? details.kind), enabled: !details.companies, queryFn: ({ signal }) => { if (details.work) { return dashboardApi.work(signal, companyId, page); } if (details.activity) { return dashboardApi.activity(signal, companyId); } return dashboardApi.details(signal, { companyId, page, kind: details.kind, issue: details.issue, }); }, }); const companies = useQuery({ queryKey: dashboardKeys.block("companies", companyId), enabled: Boolean(details.companies), queryFn: ({ signal }) => dashboardApi.companies(signal, companyId), }); const title = t(detailsTitle(details)); const labels = entityListLabels(i18n.language); const total = query.data?.total ?? 0; const range = labels.range( total ? (page - 1) * 10 + 1 : 0, Math.min(page * 10, total), total, t("dashboard.records") ); return ( { if (!open) { onClose(); } }} open title={title} wide > {details.companies ? ( item.name, render: (item) => ( ), }, { id: "tickets", label: t("dashboard.openTickets"), value: (item) => item.tickets, render: (item) => item.tickets, }, { id: "interventions", label: t("dashboard.pendingInterventions"), value: (item) => item.interventions, render: (item) => item.interventions, }, { id: "assets", label: t("dashboard.registeredAssets"), value: (item) => item.assets, render: (item) => item.assets, }, { id: "people", label: t("people"), value: (item) => item.people, render: (item) => item.people, }, ]} entityLabel={t("title")} items={companies.data?.items ?? []} locale={i18n.language} title={title} /> ) : ( <>

{range}

{details.activity && } {details.activity && } {details.work && } {query.data?.items.map((item) => ( {details.activity && ( )} {details.activity && } {details.work && ( )} ))}
{title}
{t("name")} {t("tickets.company")}{t("dashboard.recordedAt")}{t("dashboard.recordedBy")}{t("tickets.state")}
{item.companyName} {item.recordedAt ? new Intl.DateTimeFormat(i18n.language, { dateStyle: "medium", timeStyle: "short", }).format(new Date(item.recordedAt)) : "—"} {item.actorName ?? "—"} {t( (item.kind === "ticket" ? "tickets.states." : "interventions.states.") + item.state )}
{!details.activity && query.isSuccess && ( )} )}
); } function detailsTitle(details: { issue?: DashboardIssue; kind?: "asset" | "resource"; work?: boolean; activity?: boolean; }) { if (details.issue) { return `dashboard.issues.${details.issue}`; } if (details.kind === "asset") { return "dashboard.registeredAssets"; } if (details.kind === "resource") { return "dashboard.registeredResources"; } if (details.work) { return "dashboard.pendingWork"; } if (details.activity) { return "dashboard.recentActivity"; } return "dashboard.companySummary"; }