import { useTranslation } from "react-i18next"; import { FeatherPhoneCall } from "@subframe/core"; import type { TelephonyCallListRow, TelephonyCallListVM, TelephonyCallStatus, TelephonyMatchConfidence, } from "@hivegrid/view-models"; import { useLocaleFormatters } from "~/locale/format"; import { Alert } from "~/ui/components/Alert"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { EmptyState } from "~/ui/components/EmptyState"; import { LinkButton } from "~/ui/components/LinkButton"; import { Table } from "~/ui/components/Table"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell"; import { RefreshIconButton } from "../shared/RefreshIconButton"; export type TelephonyCallsActions = { readonly onOpenCall: (callId: string, detailHref: string) => void; readonly onRefresh?: () => void | Promise; readonly onRetry?: () => void | Promise; }; export type TelephonyCallsPageProps = { readonly vm: TelephonyCallListVM; readonly actions: TelephonyCallsActions; readonly busy?: boolean; }; type BadgeTone = "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-information" | "subtle-neutral"; function callStatusTone(status: TelephonyCallStatus): BadgeTone { switch (status) { case "answered": case "completed": return "subtle-positive"; case "ringing": return "subtle-information"; case "missed": case "failed": return "subtle-negative"; case "unknown": default: return "subtle-neutral"; } } function matchTone(confidence: TelephonyMatchConfidence): BadgeTone { if (confidence === "exact") return "subtle-positive"; if (confidence === "manual") return "subtle-information"; if (confidence === "soft") return "subtle-warning"; return "subtle-neutral"; } function formatDuration(seconds: number | null, emptyLabel: string): string { if (seconds === null) return emptyLabel; const minutes = Math.floor(seconds / 60); const remainder = seconds % 60; return `${minutes}:${String(remainder).padStart(2, "0")}`; } function CallsShell({ actions, busy, children }: { readonly actions: TelephonyCallsActions; readonly busy: boolean; readonly children: React.ReactNode; }): React.JSX.Element { const { t } = useTranslation(); return ( } title={t("telephony.calls.title")} subtitle={t("telephony.calls.subtitle")} actions={ actions.onRefresh ? ( void actions.onRefresh?.()} label={t("telephony.actions.refresh")} dataUiId="ui.telephony.calls.refresh" /> ) : undefined } /> {children} ); } function Counterpart({ row }: { readonly row: TelephonyCallListRow }): React.JSX.Element { return (
{row.counterpartLabel ?? row.phoneNumber} {row.counterpartLabel ? ( {row.phoneNumber} ) : null} {row.noteSnippet ? ( {row.noteSnippet} ) : null}
); } export function TelephonyCallsPage({ vm, actions, busy = false }: TelephonyCallsPageProps): React.JSX.Element { const { t } = useTranslation(); const formatters = useLocaleFormatters(); if (vm.status === "idle" || vm.status === "loading") { return ( ); } if (vm.status === "error") { return ( void actions.onRetry?.()}> {t("telephony.actions.retry")} ) : undefined } /> ); } if (vm.status === "empty" || !vm.data) { return ( ); } if (vm.data.rows.length === 0) { return ( ); } return (
{t("telephony.calls.columns.counterpart")}{t("telephony.calls.columns.direction")}{t("telephony.calls.columns.status")}{t("telephony.calls.columns.match")}{t("telephony.calls.columns.user")}{t("telephony.calls.columns.startedAt")}{t("telephony.calls.columns.duration")}{t("telephony.calls.columns.recording")}{t("telephony.calls.columns.action")} } > {vm.data.rows.map((row) => ( {t(`telephony.call.direction.${row.direction}`)} {t(`telephony.call.status.${row.status}`)}
{row.matchedEntityLabel ?? t("telephony.common.notAssigned")} {t(`telephony.call.match.${row.matchConfidence}`)}
{row.boundUserLabel ?? t("telephony.common.notAssigned")} {formatters.dateTime(row.startedAt)} {formatDuration(row.durationSeconds, t("telephony.common.notAvailable"))} {t(row.hasRecording ? "telephony.calls.recording.available" : "telephony.calls.recording.unavailable")} actions.onOpenCall(row.id, row.detailHref)} aria-label={t("telephony.calls.openAria", { number: row.phoneNumber })} > {t("telephony.actions.open")}
))}
); }