import { inventoryApi, inventoryKeys } from "@metis/assets/client"; import { BrowserLink, useEntryState, useListQuery } from "@metis/core/client"; import { EntityListStatus } from "@repo/rdx-ui/components/entity-list-status"; import { useRef } from "react"; import { useTranslation } from "react-i18next"; import { z } from "zod/v4"; import type { CompanyDTO } from "../../../../common"; import { printerApi, printerKeys } from "../../../printer-configurations/shared/api"; import { useCompanyListState } from "../../../shared/hooks/use-company-list-state"; import { profileApi, profileKeys } from "../../../work-profiles/shared/api"; import { printerUsage } from "../helpers/printer-usage"; const HistorySchema = z.boolean(); export function AssetPrinterUsage({ company, assetId }: { company: CompanyDTO; assetId: string }) { const { t } = useTranslation("companies"); const printers = useListQuery({ queryKey: printerKeys.company(company.id), queryFn: () => printerApi.get(company.id), }); const profiles = useListQuery({ queryKey: profileKeys.company(company.id), queryFn: () => profileApi.get(company.id), }); const inventory = useListQuery({ queryKey: inventoryKeys.company(company.id), queryFn: () => inventoryApi.get(company.id), }); const rootRef = useRef(null); const [historyOpen, setHistoryOpen] = useEntryState({ scope: `/companies/${company.id}/assets/${assetId}`, key: "printer-usage-history", schema: HistorySchema, initialValue: false, }); const { state, denied } = useCompanyListState([printers, profiles, inventory]); const available = state.status !== "loading" && state.status !== "unavailable"; const usage = printerUsage( assetId, printers.data?.configurations ?? [], profiles.data?.profiles ?? [], inventory.data?.assets ?? [] ); return (
Promise.all([printers.refetch(), profiles.refetch(), inventory.refetch()]) } retryFocus={() => rootRef.current} retryLabel={t("retry")} state={state} /> {available && ( <> {!usage.groups.length && (

{t("infrastructureUsage.empty")}

)} {usage.unresolved.length > 0 &&

{t("infrastructureUsage.incomplete")}

}
    {usage.groups.map((group) => (
  • {group.name}
      {group.configurations.map(({ profile, configuration }) => (
    • {profile.values.name} {" "} ·{" "} {company.people.find((person) => person.id === profile.values.personId) ?.name ?? "—"}

      {t(`mail.${configuration.values.state}`)} ·{" "} {t(`printers.${configuration.values.connection}`)} {configuration.values.isDefault ? ` · ${t("infrastructureUsage.default")}` : ""}

      {(!profile.values.active || configuration.needsReview) && (

      {t("infrastructureUsage.review")}

      )}
    • ))}
  • ))}
{usage.history.length > 0 && (
setHistoryOpen(event.currentTarget.open)} open={historyOpen} > {t("infrastructureUsage.history", { count: usage.history.length })}
    {usage.history.map((item) => (
  • {profiles.data?.profiles.find( (profile) => profile.id === item.values.profileId )?.values.name ?? "—"} {" "} · {t(`mail.${item.values.state}`)}
  • ))}
)} )}

{t("infrastructureUsage.notice")}

); }