import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { FeatherArrowLeft, FeatherExternalLink, FeatherPhoneCall, FeatherSave, FeatherUserCheck, } from "@subframe/core"; import type { TelephonyCallDetailData, TelephonyCallDetailVM, TelephonyCallStatus, TelephonyMatchConfidence, TelephonyMatchOption, } 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 { Card } from "~/ui/components/Card"; import { EmptyState } from "~/ui/components/EmptyState"; import { Select } from "~/ui/components/Select"; import { TextArea } from "~/ui/components/TextArea"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell"; export type TelephonyCallDetailActions = { readonly onBack: (listHref: string) => void; readonly onOverrideMatch: (callId: string, option: TelephonyMatchOption | null) => void | Promise; readonly onSaveNote: (callId: string, noteMarkdown: string) => void | Promise; readonly onCallBack: (callId: string) => void; readonly onRetry?: () => void | Promise; }; export type TelephonyCallDetailPageProps = { readonly vm: TelephonyCallDetailVM; readonly actions: TelephonyCallDetailActions; readonly busy?: boolean; }; const NO_MATCH = "__no_match__"; type BadgeTone = "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-information" | "subtle-neutral"; function callStatusTone(status: TelephonyCallStatus): BadgeTone { if (status === "answered" || status === "completed") return "subtle-positive"; if (status === "ringing") return "subtle-information"; if (status === "missed" || status === "failed") return "subtle-negative"; 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 matchValue(call: TelephonyCallDetailData | null): string { if (!call?.matchedEntityType || !call.matchedEntityId) return NO_MATCH; return `${call.matchedEntityType}:${call.matchedEntityId}`; } function optionValue(option: TelephonyMatchOption): string { return `${option.type}:${option.id}`; } function formatDuration(seconds: number | null, emptyLabel: string): string { if (seconds === null) return emptyLabel; const minutes = Math.floor(seconds / 60); return `${minutes}:${String(seconds % 60).padStart(2, "0")}`; } function DetailShell({ children }: { readonly children: React.ReactNode }): React.JSX.Element { const { t } = useTranslation(); return ( } title={t("telephony.callDetail.title")} subtitle={t("telephony.callDetail.subtitle")} /> {children} ); } function MetaItem({ label, value }: { readonly label: string; readonly value: React.ReactNode }): React.JSX.Element { return (
{label} {value}
); } export function TelephonyCallDetailPage({ vm, actions, busy = false }: TelephonyCallDetailPageProps): React.JSX.Element { const { t } = useTranslation(); const formatters = useLocaleFormatters(); const call = vm.status === "ready" ? vm.data : null; const [noteDraft, setNoteDraft] = useState(call?.noteMarkdown ?? ""); const [selectedMatch, setSelectedMatch] = useState(matchValue(call)); useEffect(() => { setNoteDraft(call?.noteMarkdown ?? ""); setSelectedMatch(matchValue(call)); }, [call?.id, call?.matchedEntityId, call?.matchedEntityType, call?.noteMarkdown]); 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" || !call) { return ( ); } const selectedOption = call.matchOptions.find((option) => optionValue(option) === selectedMatch) ?? null; const noteChanged = noteDraft !== call.noteMarkdown; const matchChanged = selectedMatch !== matchValue(call); return (
{t(`telephony.call.direction.${call.direction}`)} {t(`telephony.call.status.${call.status}`)} {call.canCallBack ? ( ) : null}
{t("telephony.callDetail.summary.title")}
{t("telephony.callDetail.recording.title")} {call.recordingUrl ? (
{t("telephony.callDetail.recording.body")} {t("telephony.callDetail.recording.open")}
) : ( )}
{t("telephony.callDetail.match.title")} {t("telephony.callDetail.match.body")}
{t(`telephony.call.match.${call.matchConfidence}`)}
{call.canOverrideMatch ? (
) : ( {call.matchedEntityLabel ?? t("telephony.common.notAssigned")} )}
{t("telephony.callDetail.note.title")} {t("telephony.callDetail.note.markdownHint")}
{call.canEditNote ? ( <>
) : call.noteMarkdown ? (
            {call.noteMarkdown}
          
) : ( {t("telephony.callDetail.note.empty")} )}
); }